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.然后每次随便取一 ...
随机推荐
- Notepad++ 管理工程--转载
http://blog.csdn.net/cashey1991/article/details/7001385 @1.首先从下面这个菜单打开工程panel @2.在工程panel的“Workspace ...
- javascript debugger
打开调试工具,刷新,可以看到脚本被暂停 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- Java中的注解基础
一.元注解 元注解的作用就是负责注解其他注解. 1.@Target @Target用来指明注解所修饰的目标,包括packages.types(类.接口.枚举.Annotation类型).类型成员(方法 ...
- Java回顾之序列化
在这篇文章里,我们关注对象序列化. 首先,我们来讨论一下什么是序列化以及序列化的原理:然后给出一个简单的示例来演示序列化和反序列化:有时有些信息是不应该被序列化的,我们应该如何控制:我们如何去自定义序 ...
- LINUX QQ2(转载)
关于这个话题,小编写过多次文章,也是很多朋友关心的问题. 前几日,由于小编手贱,升级Wordpress后不满意,只得重装旧版本的Wordpress,却忘了备份网站图片,导致损失惨重.近日都没有写新文章 ...
- HTML5-canvas实例:2D折线数据图与2D扇形图
基础知识: <canvas id="demo" width="400" height="400"></canvas> ...
- ablout unbuntu default mysql
http://www.ghostchina.com/how-to-reset-mysqls-root-password/ http://blog.csdn.net/u010603691/article ...
- java并发编程:线程安全管理类--原子包--java.util.concurrent.atomic
java.util.concurrent.atomic 的描述 AtomicBoolean 可以用原子方式更新的 boolean 值. AtomicInteger 可以用原子方式更新的 int 值. ...
- lister.ora配置
SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = PLSExtProc) (ORACLE_HOME = D:\ ...
- SGU 219 Synchrograph tarjian找环,理解题意,图论 难度:3
http://acm.sgu.ru/problem.php?contest=0&problem=219 题目大意: 如果指向某个点的边权全都为正数,那么这个点就是可点燃的,点燃操作把入弧权值- ...