Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论
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? 图论的更多相关文章
- 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 ...
- Educational Codeforces Round 37 (Rated for Div. 2) 920E E. Connected Components?
题 OvO http://codeforces.com/contest/920/problem/E 解 模拟一遍…… 1.首先把所有数放到一个集合 s 中,并创建一个队列 que 2.然后每次随便取一 ...
- 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 ...
- 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 ...
- [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)
Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...
- Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] 给你 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
随机推荐
- js-08-数组学习
一.数组语法格式 var name=[item1,item2,......] 二.数组的声明创建 var arr=new Aarray( ) //声明一个空数组对象 var arr=new Array ...
- Android RecyclerView SearchView基本用法1
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/82 背景: 做了很多年的app开发,貌似没见过没有搜索功能 ...
- Go语言nil:空值/零值
在 Go 语言中,布尔类型的零值(初始值)为 false,数值类型的零值为 0,字符串类型的零值为空字符串"",而指针.切片.映射.通道.函数和接口的零值则是 nil. nil 是 ...
- 使用Apollo动态修改线上数据源
前言 最近需要实现一个功能,动态刷新线上数据源环境,下面来使用Apollo配置中心和Spring提供的AbstractRoutingDataSource来实现. 具体实现 Apollo是携程开源的统一 ...
- Dockerfile优化
总结: 1.编写.dockerignore文件 2.容器只运行单个应用 3.将多个RUN指令合并为一个 4.基础镜像的标签不要用latest 5.每个RUN指令后删除多余文件 6.选择合适的基础镜像( ...
- 《推送开发全面盘点当前Android后台保活方案的真实运行效果》
登录 立即注册 TCP/IP详解 资讯 动态 社区 技术精选 首页 即时通讯网›专项技术区›推送开发全面盘点当前Android后台保活方案的真实运行效果(截止2 ... 帖子 打赏 分 ...
- 2019阿里天猫团队Java高级工程师面试题之第二面
2019阿里天猫团队Java高级工程师面试题之第一面 2019阿里天猫团队Java高级工程师面试题之第三面 1.Tomcat的基本架构是什么? https://blog.csdn.net/xlgen1 ...
- C sharp #006# 委托与事件
饮水思源:金老师的自学网站 索引 委托(Delegate) Click事件探究 委托(Delegate) “不学会委托(Delegate),等于没学.NET编程!” 例程1-委托类型变量 using ...
- Python:有参装饰器与多个装饰器装饰一个函数
有参装饰器 def timmerout(flag1): #flag1 =flag def timmer(f): def inner(*args,**kwargs): if flag1: start_t ...
- centOS如何灵活管理服务进程
1.将程序跑起来,且关闭会话时程序仍在运行. nohup node index.js & (头部加了nohup,表示在你退出帐户或关闭终端之后继续运行相应的进程;尾部的&表示在后台执 ...