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. JS截取字符串中数字

    今天项目中需要在一个字符串中截取一个数字,然后数字参与运算.搜了一下,有好多好用的方式截取字符串. 1,使用parseInt() var str ="4500元"; var num ...

  2. HTTP小幺鸡接口管理工具安装与配置说明

    http://www.xiaoyaoji.cn/doc/TxybXPTdx 小幺鸡接口管理工具安装说明 使用可以参考:https://blog.csdn.net/qincidong/article/d ...

  3. 2018-2019-1 20189206 vim.c插件安装

    vim插件安装 vim插件安装 由于今天在安装vim.c插件耗费了很多时间,配置文件一直不生效,特此记录以下安装插件的方法. 安装vim.c按照博客的方法 第一步:创建目录~/.vim 这个目录是用来 ...

  4. 51Nod 1667 概率好题 - 容斥原理

    题目传送门 无障碍通道 有障碍通道 题目大意 若$L_{i}\leqslant x_{i} \leqslant R_{i}$,求$\sum x_{i} = 0$以及$\sum x_{i} < 0 ...

  5. bzoj1566: [NOI2009]管道取珠 DP

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1566 思路 n个球,第i个球颜色为ai,对于颜色j,对答案的贡献为颜色为j的球的个数的平 ...

  6. Docker 使用Dockerfile构建redis镜像

    Dockerfile实现: FROM centos: MAINTAINER hongdada "hongdaqi159505@gmail.com" WORKDIR /home RU ...

  7. (zhuan) How to Train Neural Networks With Backpropagation

    this blog from: http://blog.demofox.org/2017/03/09/how-to-train-neural-networks-with-backpropagation ...

  8. Images之multi-stage builds

    原文链接 Use multi-stage builds Multi-stage builds are a new feature requiring Docker 17.05 or higher on ...

  9. 如何判断一个单向链表是否为回文链表(Palindrome Linked List)

    题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案 ...

  10. js精度误差

    之前虽然有看到过 js 精度相关的文章.但也都没有“印象深刻” ,但是今天"有幸"遇到了. 做一个项目,进行页面调试的时候, 当数量增加到3时总价格变得好长好长 立马在控制台验证了 ...