[CF920E] Connected Components?
Description
一句话题意:求一张图的补图的连通块数。
给定一张 \(n\) 个点,\(\frac{n\times (n-1)}{2}-m\) 条边的无向图。
读入 \(m\) 对点,表示不存在 \(u\) 到 \(v\) 这条边。
问这张图中有多少个连通块,并且将连通块的个数按不降序输出。
数据范围 \(1\le n\le 200000, 0\le m\le min(\frac{n\times (n-1)}{2}, 200000)\)
Solution
由抽屉原理知,必定存在一个点,与它相关的删去的边不超过\(\frac{m}{n}\)条。
我们找到这个点,然后将所有与它存在连边的点相连。显然,此时只剩下\(\frac{m}{n}\)个点还没有被匹配过。
对于剩下这些点,我们暴力枚举它们所连向的边即可。
复杂度 \(O(\frac{m}{n} \times n) = O(n)\)
Code
// Author: wlzhouzhuan
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define rint register int
#define rep(i, l, r) for (rint i = l; i <= r; i++)
#define per(i, l, r) for (rint i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
inline int read() {
int x = 0, neg = 1; char op = getchar();
while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
return neg * x;
}
inline void print(int x) {
if (x < 0) { putchar('-'); x = -x; }
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
const int N = 200005;
vector <int> adj[N];
int deg[N], n, m;
int f[N], sz[N], vis[N];
int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
void Union(int x, int y) {
x = find(x), y = find(y);
if (x != y) {
sz[y] += sz[x];
f[x] = y;
}
}
void add(int x) {
for (auto v: adj[x]) {
vis[v] = 1;
}
}
void del(int x) {
for (auto v: adj[x]) {
vis[v] = 0;
}
}
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) f[i] = i, sz[i] = 1;
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
adj[u].pb(v);
adj[v].pb(u);
deg[u]++, deg[v]++;
}
int ch = min_element(deg + 1, deg + n - 1) - deg;
add(ch);
vector <int> unc;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
Union(ch, i);
} else {
unc.push_back(i);
}
}
del(ch);
for (auto i: unc) {
add(i);
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
Union(i, j);
}
}
del(i);
}
vector <int> block;
for (int i = 1; i <= n; i++) if (find(i) == i) {
block.push_back(sz[find(i)]);
}
sort(block.begin(), block.end());
printf("%d\n", block.size());
for (auto v: block) {
printf("%d ", v);
}
puts("");
return 0;
}