Educational Codeforces Round 37 E. Connected Components?(图论)

E. Connected Components?
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ nx ≠ 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
input
5 5
1 2
3 4
3 2
4 2
2 5
output
2
1 4

题意:一个完全图去掉m条边,求剩下的图的联通块数和每个联通块的大小。

解析:我们先把所有的节点挂链,将当前第一个节点入队,遍历其在原图上相邻的点并做上标记,那么这时没有打上标记的点在补图上和当前节点一定有边相连因而一定在同一个联通块中,所以将没标记的点入队,并且在链表中除去,继续这个过程,直到队列为空时这个联通块就找出来了。把链接的点再删除标记,再取链表上还存在的点入队寻找一个新的联通块,直到删掉所有点为止,复杂度降为了O(n + m)。

代码:

 1 #include "bits/stdc++.h"
 2 #define db double
 3 #define ll long long
 4 #define vec vector<ll>
 5 #define Mt  vector<vec>
 6 #define ci(x) scanf("%d",&x)
 7 #define cd(x) scanf("%lf",&x)
 8 #define cl(x) scanf("%lld",&x)
 9 #define pi(x) printf("%d
",x)
10 #define pd(x) printf("%f
",x)
11 #define pl(x) printf("%lld
",x)
12 #define inf 0x3f3f3f3f
13 #define rep(i, x, y) for(int i=x;i<=y;i++)
14 const int N   = 1e6 + 5;
15 const int mod = 1e9 + 7;
16 const int MOD = mod - 1;
17 const db  eps = 1e-10;
18 const db  PI  = acos(-1.0);
19 using namespace std;
20 int n,m,cnt;
21 int hea[N];
22 int a[N];
23 int pre[N],nex[N],ti[N];
24 int id=0;
25 struct P{
26     int fr,to,nxt;
27 };
28 P e[N];
29 void add(int fr,int to){//前向星
30     e[cnt].fr=fr,e[cnt].to=to,e[cnt].nxt=hea[fr];
31     hea[fr]=cnt++;
32 }
33 void init()//初始化
34 {
35     memset(a,0, sizeof(a));
36     memset(nex,0, sizeof(nex));
37     memset(ti,0, sizeof(ti));
38     memset(pre,0, sizeof(pre));
39     memset(hea,-1,sizeof(hea));
40     cnt=0;
41 }
42 int main()
43 {
44     init();
45     ci(n),ci(m);
46     for(int i=0;i<m;i++)
47     {
48         int x,y;
49         ci(x),ci(y);
50         add(x,y),add(y,x);
51     }
52     nex[0]=1;
53     for(int i=1;i<=n;i++) pre[i]=i-1,nex[i]=i+1;//链表
54     pre[n+1]=n;
55     int tot=0;
56     while(nex[0]!=n+1)
57     {
58         queue<int>q;
59         int sum=1;
60         q.push(nex[0]);//链表当前第一个节点入队
61         nex[0]=nex[nex[0]];
62         pre[nex[0]]=0;
63         while(q.size())
64         {
65             id++;
66             int u=q.front();
67             q.pop();
68             for(int i=hea[u];i!=-1;i=e[i].nxt){//标记与u点直接相连的点
69                 int to=e[i].to;
70                 ti[to]=id;
71             }
72             for(int i=nex[0];i!=n+1;i=nex[i]){//若点未被标记则在补图中直接相连,对此点继续同样的操作。
73                 if(ti[i]!=id) nex[pre[i]]=nex[i],pre[nex[i]]=pre[i],q.push(i),sum++;
74             }
75         }
76         a[++tot]=sum;//联通块大小
77     }
78     sort(a+1,a+tot+1);
79     pi(tot);
80     for(int i=1;i<=tot;i++) printf("%d%c",a[i],i==tot?'
':' ');
81     return 0;
82 }
原文地址:https://www.cnblogs.com/mj-liylho/p/8412915.html