CF774A. Hongcow Builds A Nation 题解 简单dfs+组合计数
题目链接: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;
}