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 ...
随机推荐
- UI简述
UI的全称是user interface,即是用户界面.UI设计是指对软件的人机交互,操作编辑,界面美观的整体设计,从简单的角度来说就是,UI是视觉上的东西,包括logo.软件.网页的按钮.网页导航, ...
- oracle查询所有初始化参数(含隐含参数)
年龄大了,感觉记性不是很好了,还是重新做笔记了.最近在整理些稿子,顺便在记录下oracle查询所有初始化参数(含隐含参数): SELECT i.ksppinm name, i.ksppdesc des ...
- 【题解】Luogu P1204 [USACO1.2]挤牛奶Milking Cows
原题传送门:P1204 [USACO1.2]挤牛奶Milking Cows 实际是道很弱智的题目qaq 但窝还是觉得用珂朵莉树写会++rp(窝都初二了,还要考pj) 前置芝士:珂朵莉树 窝博客里对珂朵 ...
- Magnum Kubernetes源码分析(二)
Kubernetes Master Stack kubernetes master的stack的resources主要分为三个部分. master wait handle wait handle主要用 ...
- Codeforces 835F Roads in the Kingdom - 动态规划
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗基环树,要求删去其中一条边,使得剩下的图形是一棵树,并且最长路的长度最短,求最长路的最短长度. 路径可以分为两部分:跨过环 和 在树内 ...
- 2018年11月22日 字典 E18灯翼平整度 D&G is SB
如果创建的东西需要增加修改的,则用list 如果不能修改就用元祖,如果需要修改这需要转成list 字典 字典的value是任意值 info= {"k1":'v1',"k2 ...
- 那些离不开的 Chrome 扩展插件
虽然Chrome浏览器是个吃内存的怪兽,但是,它却因为启动速度.调试功能等成为了程序猿的必备浏览器!今天有时间,整理一下自己最常用的一些Chrome扩展吧: 常用网页浏览非开发类扩展: Tamperm ...
- 【做题】HDU6331 Walking Plan——矩阵&分块
题意:给出一个有\(n\)个结点的有向图,边有边权.有\(q\)组询问,每次给出\(s,t,k\),问从\(s\)到\(t\)至少经过\(k\)条边的最短路. \(n \leq 50, \, q \l ...
- SQL Server (MSSQLSERVER) 服务因 2148081668 服务性错误而停止。
https://zhidao.baidu.com/question/151448005.html 具体步骤: 运行-> 输入:“services.msc” ->找到 “SQL Server ...
- java 偏向锁,轻量锁,重量级锁
synchronized的执行过程: 1. 检测Mark Word里面是不是当前线程的ID,如果是,表示当前线程处于偏向锁 2. 如果不是,则使用CAS将当前线程的ID替换Mard Word,如果成功 ...