Educational Codeforces Round 37-E.Connected Components?题解
一、题目

二、题目链接
http://codeforces.com/contest/920/problem/E
三、题意
给定一个$N$和$M$。$N$表示有$N$个点,$M$表示,在一个$N$个点组成的无向完全图中,接下来的$M$条无向边不存在。
问你在这个图中有多少个连通分量,并且从小到大输出每个连通分量的顶点个数。
四、思路
求无向图的连通分量个数,只需要跑一边bfs就OK了。其实dfs也可以,只是太多的“函数压栈现场保护”浪费一丢丢时间而已,慢一点点,影响其实并不大。
要注意的是,无论跑bfs还是dfs,不能枚举边,因为这题的边数太多了,会超时。只能枚举点,枚举所有未访问的点。
然后,在跑bfs的过程中,统计这次bfs访问的点的个数,返回就OK了。枚举每一个点,如果是未访问的,就跑一遍bfs,记录这次bfs访问的点的个数,问题就解决了。
五、源代码
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
;
template <class T> inline void read(T &x) {
int t;
bool flag = false;
')) ;
';
+ t - ';
if(flag) x = -x;
}
int n, m;
unordered_set<int> g[MAXN], invis;
vector<int> ans, tmp;
queue<int> que;
int bfs(int s) {
;
while(!que.empty())que.pop();
que.push(s);
invis.erase(s);
while(!que.empty()) {
int t = que.front();
que.pop();
tmp.clear();
for(auto x : invis) {//注意不要边迭代、边移除。
if(g[t].count(x))continue;
else {
que.push(x);
tmp.push_back(x);
res++;
}
}
for(auto x : tmp)invis.erase(x);
}
return res;
}
void init() {
invis.clear();
; i <= n; ++i)invis.insert(i);
; i < MAXN; ++i)g[i].clear();
ans.clear();
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Einput.txt", "r", stdin);
#endif // ONLINE_JUDGE
int a, b;
scanf("%d%d", &n, &m);
init();
; i < m; ++i) {
read(a), read(b);
g[a].insert(b);
g[b].insert(a);
}
; i <= n && invis.size() > ; ++i) {
if(invis.count(i))ans.push_back(bfs(i));
}
sort(ans.begin(), ans.end());
printf("%d\n", ans.size());
for(auto x : ans)printf("%d ", x);
;
}
Educational Codeforces Round 37-E.Connected Components?题解的更多相关文章
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- 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) E. Connected Components? 图论
E. Connected Components? You are given an undirected graph consisting of n vertices and edges. Inste ...
- codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 37 A B C D E F
A. water the garden Code #include <bits/stdc++.h> #define maxn 210 using namespace std; typede ...
- 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 ...
- [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)
Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...
- 【Educational Codeforces Round 37 E】Connected Components?
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs. 用一个链表来记录哪些点已经确定在某一个联通快里了. 一开始每个点都能用. 然后从第一个点开始进行bfs. 然后对于它的所有 ...
- Educational Codeforces Round 37 (Rated for Div. 2) 920E E. Connected Components?
题 OvO http://codeforces.com/contest/920/problem/E 解 模拟一遍…… 1.首先把所有数放到一个集合 s 中,并创建一个队列 que 2.然后每次随便取一 ...
随机推荐
- Python XML解析和处理
movies.xml <collection shelf = "New Arrivals"> <movie title = "Enemy Behind& ...
- ubuntu 14.04 (desktop amd 64) 查看配置参数
硬盘型号 sudo hdparm -i /dev/sda |grep "Model" 硬盘数量大小 sudo fdisk -l |grep "Disk /dev/sd ...
- python 生成zip压缩包
import zipfile file_name="a.txt" f = zipfile.ZipFile('test.zip','w',zipfile.ZIP_STORED) f. ...
- 雷林鹏分享:Ruby Dir 类和方法
Ruby Dir 类和方法 Dir 是一个表示用于给出操作系统中目录中的文件名的目录流.Dir 类也拥有与目录相关的操作,比如通配符文件名匹配.改变工作目录等. 类方法 序号方法 & 描述 1 ...
- JS级联下拉框
//Ajax级联获取SDKfunction GetDropDownList(parent_ddlID, fill_dllID, url, param) { this.pId = parent_d ...
- uva11551矩阵快速幂
题目看了半天没看懂,,就是把一个数列更新r次,每次更新就是计算和,就是每一个数,只要出现了的表号都要加上去,具体看代码 矩阵快速幂实现加速 #include<map> #include&l ...
- Java——抽象类、接口
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- New Concept English Two 17 43
$课文41 你把那个叫帽子吗? 422. 'Do you call that a hat?' I said to my wife. “你把那个叫帽子吗?”我对妻子说. 423. 'You needn ...
- ConfigurationManager 引用
即使在代码中添加了using System.Configuration 也不会自动出来ConfigurationManager, 需要到项目的Reference手动引用Configuration的dl ...
- Yahoo! Finance财经数据PYTHON临时读取方法
本篇文章转自简书:http://www.jianshu.com/p/85d563d326a9 这段时间在看量化策略,找到了一个比较不错的开源项目,但是yahoo金融的数据源一直没有找到,在网上找到了这 ...