Discription
You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.
You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.
Input
The first line contains two integers n and m (1 ≤ n ≤ 200000, ).
Then m lines follow, each containing a pair of integers x and y (1 ≤ x, y ≤ n, x ≠ y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices.
Output
Firstly print k — the number of connected components in this graph.
Then print k integers — the sizes of components. You should output these integers in non-descending order.
Example
5 5
1 2
3 4
3 2
4 2
2 5
2
1 4 我们发现对于N个点的完全图来说,删去M条边的影响是有限的(因为N和M最大都是2*10^5)。
所以可以猜测的是不会有很多联通块。那么就可以暴力BFS一下,用一个链表来维护当前还没有被放入联通块的节点。
当我们从一个节点扩展的时候,就在链表里找它所能到达的点,然后把它们从链表中删去,假如BFS的队列中。 我们在链表中扫到一个元素还不删除的次数之和最多是2*M,因为只有和当前点有连边的链表中的点才不会被删除。
这样就保证了时间复杂度,也就是每个点最多进一次队列,链表中的所有点被扫到的次数之和<=2*M+N。
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<map>
#define ll long long
#define maxn 200005
using namespace std;
map<int,int> mmp[maxn];
int ans[maxn],tot=0,n,m;
int hd,ne[maxn]; inline void solve(int tmp){
queue<int> q;
ans[tmp]=0;
int x,now,pre; q.push(hd),hd=ne[hd];
while(!q.empty()){
x=q.front(),q.pop(),ans[tmp]++; for(now=hd,pre=0;now;now=ne[now])
if(!mmp[x][now]){
q.push(now);
if(hd==now) hd=ne[hd];
ne[pre]=ne[now];
}
else pre=now;
}
} int main(){
int uu,vv;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&uu,&vv);
mmp[uu][vv]=mmp[vv][uu]=1;
} for(int i=1;i<=n;i++) ne[i]=hd,hd=i; while(hd) solve(++tot); printf("%d\n",tot);
sort(ans+1,ans+tot+1);
for(int i=1;i<=tot;i++) printf("%d ",ans[i]);
puts(""); return 0;
}