Connected Components? CodeForces - 920E (bfs)
大意:给定无向图, 求补图的连通块数
bfs模拟即可, 用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back using namespace std; const int N = 4e5+, INF = 0x3f3f3f3f;
int n, m;
map<int,int> a[N];
set<int> s;
int sz[N]; int main() {
scanf("%d%d", &n, &m);
REP(i,,m) {
int u, v;
scanf("%d%d", &u, &v);
a[u][v]=a[v][u]=;
}
REP(i,,n) s.insert(i);
REP(i,,n) if (s.count(i)) {
queue<int> q;
q.push(i);
int cnt = ;
while (!q.empty()) {
int i=q.front();q.pop();
vector<int> tmp;
for (int j:s) if (!a[i][j]) {
++cnt, q.push(j), tmp.pb(j);
}
for (int j:tmp) s.erase(j);
}
sz[++*sz] = cnt;
}
printf("%d\n", *sz);
sort(sz+,sz++*sz);
REP(i,,*sz) printf("%d ",sz[i]);
puts("");
}
Connected Components? CodeForces - 920E (bfs)的更多相关文章
- Connected Components? Codeforces - 920E || 洛谷 P3452 &&bzoj1098 [POI2007]BIU-Offices
https://codeforces.com/contest/920/problem/E https://www.luogu.org/problemnew/show/P3452 https://www ...
- 线段树 C - Connected Components? CodeForces - 920E
这个题目居然可以用线段树写,好震惊,如果不是在线段树专题肯定想不到,但是就算在线段树的专题里面,我也不太会怎么写. 这个题目大意是,给你n m n代表n个点,m代表m条边,然后就是m行,每行两个数字, ...
- Codeforces E - Connected Components?
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...
- Codeforces 920 E Connected Components?
Discription You are given an undirected graph consisting of n vertices and edges. Instead of giving ...
- 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 (Rated for Div. 2) E. Connected Components? 图论
E. Connected Components? You are given an undirected graph consisting of n vertices and edges. Inste ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- Cyclic Components CodeForces - 977E(DFS)
Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
随机推荐
- 谷歌发布"自动机器学习"技术 AI可自我创造
谷歌发布"自动机器学习"技术 AI可自我创造 据Inverse报道,今年5月份,谷歌宣布其人工智能(AI)研究取得重大进展,似乎帮助科幻小说中最耸人听闻的末日预言成为现实.谷歌推出 ...
- php mysqli query 查询数据库后读取内容的方法
php mysqli query 查询数据库后读取内容的方法 <?php$mysqli = new mysqli("localhost", "my_user&quo ...
- Kotlin语言Web库又添一虎将:Kweb
Kweb是一个库,您可以利用它使用Kotlin编程语言轻松构建Web应用程序.从程序员的角度来看,它基本上消除了浏览器和服务器之间的分离.这意味着只操纵DOM的事件不需要进行服务器往返.由于Kweb是 ...
- Finalize和Dispose的区别
https://www.cnblogs.com/Jessy/articles/2552839.html
- Hadoop MapReduce执行过程实例分析
1.MapReduce是如何执行任务的?2.Mapper任务是怎样的一个过程?3.Reduce是如何执行任务的?4.键值对是如何编号的?5.实例,如何计算没见最高气温? 分析MapReduce执行过程 ...
- Mysql管理工具 SqlYog快捷键大全
Ctrl+M 创建一个新的连接Ctrl+N 使用当前设置新建连接Ctrl+F4 断开当前连接 对象浏览器F5 刷新对象浏览器(默认)Ctrl+B 设置焦点于对象浏览器 SQL 窗口 ...
- 20145303刘俊谦 《网络对抗》Exp9 Web安全基础实践
20145303刘俊谦 <网络对抗>Exp9 Web安全基础实践 基础问题回答 1.SQL注入原理,如何防御 SQL注入 就是通过把SQL命令插入到"Web表单递交"或 ...
- mongodb 有一个坑 报错 no mongos proxies found in seed list
mongoose 的报当我从 mongoose@4.5.2 升级到 mongoose@4.6.5的时候,出现了一个问题: Unhandled rejection MongoError: no mong ...
- SQL Over
与over函数结合的几个函数 create table #tab(A varchar(), B varchar()) insert into #tab select 'A1', 'B1' union ...
- Is it bad to rely on foreign key cascading? 外键 级联操作
Is it bad to rely on foreign key cascading? I'll preface前言 this by saying that I rarely delete rows ...