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?的更多相关文章

  1. [Codeforces 920E]Connected Components?

    Description 题库链接 给你一个 \(n\) 个点 \(m\) 条边的无向图,求其补图的连通块个数及各个连通块大小. \(1\leq n,m\leq 200000\) Solution 参考 ...

  2. CodeForces 292D Connected Components (并查集+YY)

    很有意思的一道并查集  题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问.对每个询问的两个值xi yi,表示在从m条边内删除[xi,yi]的边后 ...

  3. Codeforces 920E Connected Components? 补图连通块个数

    题目链接 题意 对给定的一张图,求其补图的联通块个数及大小. 思路 参考 ww140142. 维护一个链表,里面存放未归入到任何一个连通块中的点,即有必要从其开始进行拓展的点. 对于每个这样的点,从它 ...

  4. Educational Codeforces Round 37 E. Connected Components?(图论)

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces 920 E Connected Components?

    Discription You are given an undirected graph consisting of n vertices and  edges. Instead of giving ...

  6. 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 ...

  7. [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), ...

  8. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  9. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

随机推荐

  1. P1297 [国家集训队]单选错位(期望)

    P1297 [国家集训队]单选错位 期望入门 我们考虑涂到第$i$道题时的情况 此时题$i$答案有$a[i]$种,我们可能涂$a[i+1]$种 分类讨论: 1.$a[i]>=a[i+1]$: 可 ...

  2. JavaWeb中的资源映射

    一./与/* <url-pattern>/</url-pattern>  会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url< url- ...

  3. Vue 中的动画特效

    Vue 中的动画特效 CSS 实现标签显隐 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  4. "添加与删除程序"报rundll32错误

    无法启动"添加与删除程序"系统报rundll32错误 系统反馈以下信息: rundll32.exe应用程序错误"0x00310030"指令 解决方法: 1.启动 ...

  5. android拨号

    android:textColor="#A0ff1400" A0表示透明度00完全透明FF完全不透明,后面6位是RGB问题:类中类的findViewById方法为何用不了?解:类中 ...

  6. forever 用法

    安装过Node.js后再安装forever,需要加-g参数,因为forever要求安装到全局环境下: npm install forever -g 常用指令: npm install forever ...

  7. 浅析alsa声卡驱动snd_interval结构体openmin,openmax和integer含义

    // openmin和openmax表示开集,如果2个全为1,那么就表示,range范围为(min,max)即2个开区间// openmin为1,openmax为0,range范围为(min,max] ...

  8. js导读,js引入,js选择器,事件,操作页面文档,计算后样式,数据类型

    js导读 ''' js属于编写运行在浏览器上的脚本语言 js采用ECMAScript语法 操作BOM:浏览器对象模型 eg:浏览器上下滑动,浏览器历史记录 操作DOM:文档对象模型 ''' js引入 ...

  9. git初学

    git在团队合作开发时是很有用的,SVN是集中式的代表,而git是分布式的代表,它分为代码区.暂存区.和本地库.在同一个团队中开发时,在代码存储中心(例如,码云.github)上创建一个库,用于储存和 ...

  10. word2010使用记录

    安装时提示: installer无法更新被保护的一个或多个文件, 解决方法是: 将三个dll文件,放在 c:\program files\common files\ms shared\web serv ...