CF774A. Hongcow Builds A Nation 題解 簡單dfs+組合計數

quanjun發表於2024-10-25

題目連結:https://codeforces.com/problemset/problem/744/A

影片講解:https://www.bilibili.com/video/BV1tZ1FYPELp?p=5

假設 \(c_i\) 所在的連通塊大小(即包含節點個數)位 \(sz_i\)

\(sum = \sum\limits_{i=1}^k sz_i\)\(mx = \max\limits_{1 \le i \le k} sz_i\)

則可以新增的最多邊數為 \(\sum\limits_{i=1}^k \frac{ sz_i \times (sz_i - 1) }{2} + \frac{ (n - sum) \times (n - sum - 1) }{2} + mx \times (n - sum) - m\)(具體分析可以參考影片講解)

示例程式:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;

int n, m, k, c[maxn], sz[maxn], sum, mx, ans;
bool vis[maxn];
vector<int> g[maxn];

int dfs(int u) {
	if (vis[u])
		return 0;
	vis[u] = true;
	int res = 1;
	for (auto v : g[u])
		res += dfs(v);
	return res;
}

int main() {
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= k; i++)
		scanf("%d", c+i);
	for (int i = 0; i < m; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for (int i = 1; i <= k; i++) {
		sz[i] = dfs(c[i]);
		sum += sz[i];
		mx = max(mx, sz[i]);
		ans += sz[i] * (sz[i] - 1) / 2;
	}
	ans += (n - sum) * (n - sum - 1) / 2 + mx * (n - sum) - m;
	printf("%d\n", ans);
	return 0;
}

相關文章