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;
}

Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论的更多相关文章

  1. Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)

    Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...

  2. Educational Codeforces Round 37 (Rated for Div. 2) 920E E. Connected Components?

    题 OvO http://codeforces.com/contest/920/problem/E 解 模拟一遍…… 1.首先把所有数放到一个集合 s 中,并创建一个队列 que 2.然后每次随便取一 ...

  3. Educational Codeforces Round 37 (Rated for Div. 2)

    我的代码应该不会被hack,立个flag A. Water The Garden time limit per test 1 second memory limit per test 256 mega ...

  4. Educational Codeforces Round 37 (Rated for Div. 2) G

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  5. [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)

    Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...

  6. Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

    Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ​ 给你 ...

  7. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  9. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

随机推荐

  1. Angular(06)- 为什么数据变化,绑定的视图就会自动更新了?

    这里提一点,前端三大框架(Angular,React,Vue)的数据驱动来更新视图的原理,即 MVVM 的实现. 为什么数据发生变化,绑定的视图就会刷新了呢? 以下是我的个人理解,仅供参考: 在还是 ...

  2. Android UI开发之开源控件项目整理

    一.Banner 1.https://github.com/youth5201314/banner Android广告图片轮播控件,支持无限循环和多种主题,可以灵活设置轮播样式.动画.轮播和切换时间. ...

  3. svn忽略文件不提交至服务器的方法

    在提交界面,右键加入忽略提交列表.可以实现忽略本地文件不提交,且不删除服务器上的文件.

  4. SpringCloudGateWay修改请求路径,从注册中心获得服务

    SpringCloudGateWay修改请求路径,从注册中心获得服务 @Resource    private DiscoveryClient disClient;    @Resource    p ...

  5. Mac中创建一个OpenGL项目

    配置: OS X10.10 + Xcode 6.0 一.基本步骤 新建一个命令窗口项目 更改目标设备版本号为 OS X 8.0 添加库文件 GLUT.framework 和 OpenGL.framew ...

  6. JVM基础回顾记录(一):JVM的内存模型

    JVM的内存模型&垃圾收集算法 JVM内存模型 JAVA程序执行的基本流程(基于HotSpot): 图1 1.程序计数器 程序计数器是一块较小的内存空间,是当前线程执行字节码的行号指示器,字节 ...

  7. Java类BufferedImage

    https://docs.oracle.com/javase/7/docs/api/java/awt/image/BandedSampleModel.html

  8. Java程序远程无法执行nohup命令

    问题的上下文: 由于生产无法使用 jenkins 发布,所以采用 ch.ethz.ssh2 或叫 ganymed-ssh2 的开源 java 的 ssh api 进行远程发布. 在发起重启时,远程执行 ...

  9. acwing 851. spfa求最短路 模板

    地址 https://www.acwing.com/problem/content/description/853/ 给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你求出 ...

  10. IT兄弟连 HTML5教程 CSS3揭秘 CSS3属性4

    7  多列布局属性 通过CSS3,开发人员能够创建多列来对文本进行布局.在CSS2时代,对于多列布局的设计,大多采用浮动布局和绝对定位布局两种方式.浮动布局比较灵活,但是需要编写大量的附加样式代码,而 ...