Educational Codeforces Round 37 E. Connected Components?(图论)
2 seconds
256 megabytes
standard input
standard output
You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.
You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.
The first line contains two integers n and m (1 ≤ n ≤ 200000, ).
Then m lines follow, each containing a pair of integers x and y (1 ≤ x, y ≤ n, x ≠ y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices.
Firstly print k — the number of connected components in this graph.
Then print k integers — the sizes of components. You should output these integers in non-descending order.
5 5
1 2
3 4
3 2
4 2
2 5
2
1 4
题意:一个完全图去掉m条边,求剩下的图的联通块数和每个联通块的大小。
解析:我们先把所有的节点挂链,将当前第一个节点入队,遍历其在原图上相邻的点并做上标记,那么这时没有打上标记的点在补图上和当前节点一定有边相连因而一定在同一个联通块中,所以将没标记的点入队,并且在链表中除去,继续这个过程,直到队列为空时这个联通块就找出来了。把链接的点再删除标记,再取链表上还存在的点入队寻找一个新的联通块,直到删掉所有点为止,复杂度降为了O(n + m)。
代码:
#include "bits/stdc++.h"
#define db double
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define inf 0x3f3f3f3f
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
int n,m,cnt;
int hea[N];
int a[N];
int pre[N],nex[N],ti[N];
int id=;
struct P{
int fr,to,nxt;
};
P e[N];
void add(int fr,int to){//前向星
e[cnt].fr=fr,e[cnt].to=to,e[cnt].nxt=hea[fr];
hea[fr]=cnt++;
}
void init()//初始化
{
memset(a,, sizeof(a));
memset(nex,, sizeof(nex));
memset(ti,, sizeof(ti));
memset(pre,, sizeof(pre));
memset(hea,-,sizeof(hea));
cnt=;
}
int main()
{
init();
ci(n),ci(m);
for(int i=;i<m;i++)
{
int x,y;
ci(x),ci(y);
add(x,y),add(y,x);
}
nex[]=;
for(int i=;i<=n;i++) pre[i]=i-,nex[i]=i+;//链表
pre[n+]=n;
int tot=;
while(nex[]!=n+)
{
queue<int>q;
int sum=;
q.push(nex[]);//链表当前第一个节点入队
nex[]=nex[nex[]];
pre[nex[]]=;
while(q.size())
{
id++;
int u=q.front();
q.pop();
for(int i=hea[u];i!=-;i=e[i].nxt){//标记与u点直接相连的点
int to=e[i].to;
ti[to]=id;
}
for(int i=nex[];i!=n+;i=nex[i]){//若点未被标记则在补图中直接相连,对此点继续同样的操作。
if(ti[i]!=id) nex[pre[i]]=nex[i],pre[nex[i]]=pre[i],q.push(i),sum++;
}
}
a[++tot]=sum;//联通块大小
}
sort(a+,a+tot+);
pi(tot);
for(int i=;i<=tot;i++) printf("%d%c",a[i],i==tot?'\n':' ');
return ;
}
Educational Codeforces Round 37 E. Connected Components?(图论)的更多相关文章
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)
Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...
- 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 ...
- Educational Codeforces Round 37 (Rated for Div. 2)
我的代码应该不会被hack,立个flag A. Water The Garden time limit per test 1 second memory limit per test 256 mega ...
- codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 37 A B C D E F
A. water the garden Code #include <bits/stdc++.h> #define maxn 210 using namespace std; typede ...
- [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)
Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...
- Educational Codeforces Round 37 (Rated for Div. 2) 920E E. Connected Components?
题 OvO http://codeforces.com/contest/920/problem/E 解 模拟一遍…… 1.首先把所有数放到一个集合 s 中,并创建一个队列 que 2.然后每次随便取一 ...
- 【Educational Codeforces Round 37 E】Connected Components?
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs. 用一个链表来记录哪些点已经确定在某一个联通快里了. 一开始每个点都能用. 然后从第一个点开始进行bfs. 然后对于它的所有 ...
随机推荐
- Python列表类型及常用操作
Python列表类型 1.用途: 存放多个值,可以根据索引存取值 2.定义方式: 在[ ]内用逗号分割开多个任意类型的值 l=['yven','law','lyf'] #l=list(['yven', ...
- html 01前沿-web介绍
1. 认识网页 网页主要由文字.图像和超链接等元素构成.当然,除了这些元素,网页中还可以包含音频.视频以及Flash等. 2. 浏览器(显示代码) 浏览器是网页显示.运行的平台,常用的浏览器有IE.火 ...
- jquery 滑块导航菜单
带滑块的导航菜单,鼠标悬浮时,滑块会移动至鼠标位置,离开时,滑块会回到原来的位置,点击菜单之后滑块会停留在被点击菜单位置,等待下一次的鼠标悬浮事件或者点击事件,效果图: 图片效果不行,直接上代码: & ...
- sharepoint国内网站一览表(转发)
中国石油化工集团公司http://www.sinopecgroup.com/Pages/index.aspx () 中国南方航空http://group.csair.com/_layouts/grou ...
- CKEditor插件开发
以前做过一个教育项目,是有关在线考试的.其中对编辑器CKEditor做了扩充,增加了插入客观题.主观题.选择题和判断题的功能.这里记述下CKEditor插件开发的过程. CKEditor以前叫FCKE ...
- C++ POD类型
POD( Plain Old Data)概念: Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to m ...
- 如何查看某个用户指定时间段的ABAP开发记录
输入用户名和想查询的时间段: 执行得到结果.双击可查看具体代码: 工具源代码: REPORT tool_dev_history. PARAMETERS: name TYPE usr02-bname O ...
- hdu-2642 Stars---二维树状数组(细节处理)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2642 题目大意: B x y:将星星x y点亮 D x y:将星星x y熄灭 Q x1 x2 y1 ...
- 【洛谷2605】[ZJOI2010] 基站选址(线段树维护DP)
点此看题面 大致题意: 有\(n\)个村庄,每个村庄有\(4\)个属性:\(D_i\)表示与村庄\(1\)的距离,\(C_i\)表示建立基站的费用,\(S_i\)表示能将其覆盖的建基站范围,\(W_i ...
- node.js 练习2 (调用函数)
1. 调用本地 函数 (1) 创建 n2-1.js ,并输入代码 (2) 运行localhhost:8000 (3)在浏览器中 查看 (4)在cmd中查看 2.调用外部 函数 (1) 创建n2-2. ...