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 ...
随机推荐
- Markdown语法学习(Github/git.oschina.net上README.md书写规范)(转)
晚上在更新git.oschina.net项目时,突然想知道README.md后缀的来源,于是搜了下,发现README.md使用了一种小标记语言Markdown的语法,于是简单的看了一个,特转载如下,为 ...
- JAVA的内存模型及结构
所有的Java开发人员可能会遇到这样的困惑?我该为堆内存设置多大空间呢?OutOfMemoryError的异常到底涉及到运行时数据的哪块区域?该怎么解决呢? Java内存模型 Java内存模型在JVM ...
- 16 级高代 II 思考题十的多种证明
16 级高代 II 思考题十 设 $V$ 是数域 $\mathbb{K}$ 上的 $n$ 维线性空间, $\varphi$ 是 $V$ 上的线性变换, 证明: $\varphi$ 的极小多项式 $m ...
- 对应web的常用flutter应用
例如,创建一个Text widget: new Text('Hello World', style: new TextStyle(fontSize: 32.0)) 创建一个 Image widget: ...
- linux内核中的hisi_sas是什么?
答: 是一个HISILICON SAS 控制器驱动(HISILICON SAS controller driver)
- linux内核中宏likely和unlikely到底做了些什么?
1. 先看看它们长啥样吧!(它们有两种定义,第一种是使能了程序trace功能的宏定义,第二种是普通的宏定义,咱们分析普通宏定义吧) # define likely(x) __builtin_expec ...
- vi中如何使用cscope来查找函数的定义
答:进入命令行模式输入:cs f g <function_name>
- WebLogic调用WebService提示Failed to localize、Failed to create WsdlDefinitionFeature
在本地Tomcat环境下调用WebService正常,但是部署到WebLogic环境中,则提示警告:[Failed to localize] MEX0008.PARSING_MDATA_FAILURE ...
- Tutorial: Implementation of Siamese Network on Caffe, Torch, Tensorflow
Tutorial: Implementation of Siamese Network with Caffe, Theano, PyTorch, Tensorflow Updated on 2018 ...
- 用yarn代替cnpm,cnpm漏包有点严重
npm 的方式 npm install -g yarn 安装完成后,你可以测试下自己的版本 yarn --version 开始使用 单独安装包的方式add 不是install,后面不用加 ...