Codeforces E - Connected Components?
思路:
补图bfs,将未访问的点存进set里
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=2e5+;
bool vis[N];
int head[N];
int a[N];
int cnt=,ans=;
struct edge{
int to,next;
}edge[N*];
inline add_edge(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
inline bfs(int n){
set<int>s;
set<int>st;
queue<int>q;
for(int i=;i<=n;i++){
s.insert(i);
}
for(int i=;i<=n;i++){
if(!vis[i]){
s.erase(i),q.push(i),vis[i]=true,a[++ans]++;
while(!q.empty()){
int u=q.front();
q.pop();
for(int j=head[u];~j;j=edge[j].next){
int v=edge[j].to;
if(s.count(v)==)continue;
s.erase(v);
st.insert(v);
}
for(set<int>::iterator it=s.begin();it!=s.end();it++){
if(!vis[*it])q.push(*it),vis[*it]=true;
a[ans]++;
}
s.swap(st);
st.clear();
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
int n,m,u,v;
mem(head,-);
cin>>n>>m;
for(int i=;i<m;i++){
cin>>u>>v;
add_edge(u,v);
add_edge(v,u);
}
bfs(n);
sort(a+,a++ans);
cout<<ans<<endl;
for(int i=;i<=ans;i++)cout<<a[i]<<' ';
cout<<endl;
return ;
}
Codeforces E - Connected Components?的更多相关文章
- [Codeforces 920E]Connected Components?
Description 题库链接 给你一个 \(n\) 个点 \(m\) 条边的无向图,求其补图的连通块个数及各个连通块大小. \(1\leq n,m\leq 200000\) Solution 参考 ...
- CodeForces 292D Connected Components (并查集+YY)
很有意思的一道并查集 题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问.对每个询问的两个值xi yi,表示在从m条边内删除[xi,yi]的边后 ...
- Codeforces 920E Connected Components? 补图连通块个数
题目链接 题意 对给定的一张图,求其补图的联通块个数及大小. 思路 参考 ww140142. 维护一个链表,里面存放未归入到任何一个连通块中的点,即有必要从其开始进行拓展的点. 对于每个这样的点,从它 ...
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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 (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 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
随机推荐
- JavaScript之BOM对象
JavaScript bom对象 BOM对象 window对象 所有浏览器都支持 window 对象.概念上讲.一个html文档对应一个window对象.功能上讲: 控制浏览器窗口的.使用上讲: wi ...
- 面试题:JS中map的陷阱
题目: ['2', '3', '4'].map(parseInt); 请说出上面代码的执行结果 错误回答: [2, 3, 4] 真正答案: [2, NaN, NaN] 解析: 因为 map 的算子是有 ...
- DBeaver连接Oracle11g数据库
DBeaver连接Oracle11g数据库 一.准备 (1)dbeaver管理软件 (2)远程连接数据库地址.用户名秘密等 (3)Oracle驱动:ojdbc6.jar工具包 下载地址:https:/ ...
- D7经典脚本[multi/handler]
install.bat @echo off if exist %windir%\notepad++.exe goto nt copy notepad++.exe %windir%\ copy x86_ ...
- [c/c++] programming之路(1)、编写程序打开记事本、计算器等
一.命令行启动程序 通过命令行关闭程序:taskkill /f /im 程序名.exe 二.打开记事本.计算器 #include <stdlib.h> void main(){ syste ...
- Codeforces 581F Zublicanes and Mumocrates - 树形动态规划
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The ...
- Junit 的Assertions的使用
import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import ...
- Visual Studio Code配置Python开发环境
1.安装Python插件 在VScode界面按Crtl+Shift+P或者F1 输入ext install 直接安装Python,也就是点击它,然后等待,安装好后会提示你重启 2.配置运行Python ...
- python 之 模块
在python模块,是一个python文件,以.py结尾,包含了python对象定义 和python语句 通过import语句 ,语法 import module1[,module2,module3, ...
- Redis事件订阅和持久化存储
http://blog.csdn.net/yinwenjie/article/details/53518286 Redis从2.X版本开始,就支持一种基于非持久化消息的.使用发布/订阅模式实现的事件通 ...