[cf920E][set+dfs]

https://codeforc.es/contest/920/problem/E
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
Copy
5 5
1 2
3 4
3 2
4 2
2 5
output
Copy
2
1 4
题意:给出一个图的补图,求原图有几个连通块。
题解:用set维护没被访问过的节点,在dfs每一层用vector存储每个与节点相邻的节点,并从set里面删除这些节点,每个节点只会被访问一次,所以复杂度不会太大,尤其注意set删除迭代器位置元素的方法
 1 #include
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=2e5+5;
 5 #define debug(x) cout<<"["<<#x<<"]"< 6 struct edge{
 7     int fr;
 8     int to;
 9     int nex;
10 }e[maxn<<1];
11 set<int>st;
12 int cnt,colo[maxn],head[maxn],col[maxn],coloo[maxn];
13 void adde(int x,int y){
14     e[cnt].fr=x;
15     e[cnt].to=y;
16     e[cnt].nex=head[x];
17     head[x]=cnt++;
18 }
19 void dfs(int u,int fa,int c){
20     colo[u]=c;
21     coloo[c]++;
22     for(int i=head[u];i!=-1;i=e[i].nex){
23         int v=e[i].to;
24         col[v]=u;
25     }
26     vector<int>g;
27     for(auto it=st.begin();it!=st.end();){
28         if(col[(*it)]!=u){
29             g.push_back((*it));
30             st.erase(it++);
31         }
32         else{
33             it++;
34         }
35     }
36     for(int i=0;i){
37         if(!colo[g[i]]){
38             dfs(g[i],u,c);
39         }
40     }
41 }
42 int main(){
43     int n,m;
44     memset(head,-1,sizeof(head));
45     scanf("%d%d",&n,&m);
46     for(int i=1;i<=n;i++)st.insert(i);
47     while(m--){
48         int a,b;
49         scanf("%d%d",&a,&b);
50         adde(a,b);
51         adde(b,a);
52     }
53     int ans=0;
54     for(int i=1;i<=n;i++){
55         if(!colo[i]){
56             ans++;
57             dfs(i,-1,ans);
58         }
59     }
60     printf("%d\n",ans);
61     sort(coloo+1,coloo+1+ans);
62     for(int i=1;i<=ans;i++){
63         printf("%d",coloo[i]);
64         char cc=(i==ans)?'\n':' ';
65         printf("%c",cc);
66     }
67     return 0;
68 }
View Code

set正确的删除元素的方法

1     for(auto it=st.begin();it!=st.end();){
2         if(col[(*it)]!=u){
3             g.push_back((*it));
4             st.erase(it++);
5         }
6         else{
7             it++;
8         }
9     }

错误的删除方法

1     for(auto it=st.begin();it!=st.end();it++){
2         if(col[(*it)]!=u){
3             g.push_back((*it));
4             st.erase(it);
5         }
6     }

 

转载于:https://www.cnblogs.com/MekakuCityActor/p/10915909.html

你可能感兴趣的:(数据结构与算法)