E. Connected Components?

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

input

5 5

1 2

3 4

3 2

4 2

2 5

output

2

1 4

题意

给你n个点的完全图,告诉你有m条边是不可连的。问你里面一共有多少个联通块,输出每个块的大小。

题解

https://www.cnblogs.com/qscqesze/p/11813351.html 一摸一样

经验get

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n,m;
set<int>S[maxn];
set<int>vis;
int v[maxn];
int dfs(int x){
int now = 1;
vector<int> ret;
for(int v:vis){
if(!S[x].count(v))
ret.push_back(v);
}
for(int i=0;i<ret.size();i++){
vis.erase(ret[i]);
}
for(int i=0;i<ret.size();i++){
v[ret[i]]=1;
now+=dfs(ret[i]);
}
return now;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int x,y;cin>>x>>y;
x--,y--;
S[x].insert(y);
S[y].insert(x);
}
vector<int> ans;
for(int i=0;i<n;i++){
vis.insert(i);
}
for(int i=0;i<n;i++){
if(!v[i]){
ans.push_back(dfs(i));
}
}
cout<<ans.size()<<endl;
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();i++){
cout<<ans[i]-1<<" ";
}
cout<<endl;
}

最新文章

  1. [LeetCode] Odd Even Linked List 奇偶链表
  2. Javascript格式化json返回的时间(/Date(1482747413000)/)
  3. One or more types required to compile a dynamic expression cannot be found.
  4. JS-006-表格元素操作
  5. python 文件系统
  6. Linux UDP严重丢包问题的解决
  7. http发送post请求
  8. Java进阶04 RTTI
  9. Java之String、StringBuffer、StringBuilder
  10. Jmeter运行后出现乱码
  11. python学习:条件语句if、else
  12. 学习Linux下的文件目录管理
  13. 最小生成树模板【kruskal &amp; prim】
  14. Tomcat7.0/8.0 详细安装配置图解,以及UTF-8编码配置
  15. VxWorks Fuzzing 之道:VxWorks 工控实时操作系统漏洞挖掘调试与利用揭秘
  16. linux centOS服务器部署ssh,免密码登陆linux
  17. selenium+iframe 如何定位元素(实战)
  18. VS中去掉空格虚点
  19. EM算法及其推广
  20. 向Docx4j生成的word文档添加图片和布局--第一部分

热门文章

  1. 基于webpack实现多html页面开发框架七 引入第三方库如jquery
  2. springboot传值踩坑
  3. 最近几周,写了个微信好友检测助手App
  4. Test Case:: 12C ASMCMD New feature (Doc ID 1589249.1)
  5. 解决无法修改日志时间的问题(Local time zone must be set--see zic manual page 2019 )
  6. DynamicList
  7. 上手OrangePi Zero+
  8. Go语言常见坑
  9. 解决root无法登陆
  10. java架构之路(Sharding JDBC)mysql5.7yum安装和主从