Codeforces 920 E Connected Components?
Discription
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.
Input
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.
Output
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.
Example
5 5
1 2
3 4
3 2
4 2
2 5
2
1 4 我们发现对于N个点的完全图来说,删去M条边的影响是有限的(因为N和M最大都是2*10^5)。
所以可以猜测的是不会有很多联通块。那么就可以暴力BFS一下,用一个链表来维护当前还没有被放入联通块的节点。
当我们从一个节点扩展的时候,就在链表里找它所能到达的点,然后把它们从链表中删去,假如BFS的队列中。 我们在链表中扫到一个元素还不删除的次数之和最多是2*M,因为只有和当前点有连边的链表中的点才不会被删除。
这样就保证了时间复杂度,也就是每个点最多进一次队列,链表中的所有点被扫到的次数之和<=2*M+N。
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<map>
#define ll long long
#define maxn 200005
using namespace std;
map<int,int> mmp[maxn];
int ans[maxn],tot=0,n,m;
int hd,ne[maxn]; inline void solve(int tmp){
queue<int> q;
ans[tmp]=0;
int x,now,pre; q.push(hd),hd=ne[hd];
while(!q.empty()){
x=q.front(),q.pop(),ans[tmp]++; for(now=hd,pre=0;now;now=ne[now])
if(!mmp[x][now]){
q.push(now);
if(hd==now) hd=ne[hd];
ne[pre]=ne[now];
}
else pre=now;
}
} int main(){
int uu,vv;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&uu,&vv);
mmp[uu][vv]=mmp[vv][uu]=1;
} for(int i=1;i<=n;i++) ne[i]=hd,hd=i; while(hd) solve(++tot); printf("%d\n",tot);
sort(ans+1,ans+tot+1);
for(int i=1;i<=tot;i++) printf("%d ",ans[i]);
puts(""); return 0;
}
Codeforces 920 E Connected Components?的更多相关文章
- Educational Codeforces Round 37-E.Connected Components?题解
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/E 三.题意 给定一个$N$和$M$.$N$表示有$N$个点,$M$表示,在一个$N$个点组 ...
- Educational Codeforces Round37 E - Connected Components?
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> ...
- Codeforces E - Connected Components?
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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 ...
- [Redux] Using withRouter() to Inject the Params into Connected Components
We will learn how to use withRouter() to inject params provided by React Router into connected compo ...
随机推荐
- appium-在页面点击一下处理(一般处理提示蒙层)
在写用例的时候,经常会发现某些第一次进去的页面会有一个蒙层提示.我们只有处理掉这个蒙层,才能继续我们的用例: 这边我们使用的是TouchAction的tap方法 TouchAction action ...
- 爬虫:Scrapy7 - Scrapy终端(Scrapy shell)
Scrapy 终端是一个交互终端,可以在未启动 spider 的情况下尝试及调试你的爬取代码.其本意是用来测试提取数据的代码,不过可以将其作为正常的 Python 终端,在上面测试任何 Python ...
- 创建虚拟机流程nova
这篇博文借鉴于http://www.cnblogs.com/yjbjingcha/p/6977741.html,感谢博友提供. 本文试图具体地描写叙述openstack创建虚拟机的完整过程.从用户发起 ...
- tomcat调优(Mark)
http://blog.csdn.net/jinwanmeng/article/details/7756591
- 【bzoj4325】NOIP2015 斗地主(&“加强”版) 搜索
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- hdu 1811 Rank of Tetris (拓扑 & 并查集)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu1007 平面最近点对(暴力+双线程优化)
突发奇想,用双线程似乎可以优化一些暴力 比如说平面最近点对这个题目,把点复制成2份 一份按照x排序,一份按照y排序 然后双线程暴力处理,一份处理x,一份处理y 如果数据利用x递减来卡,那么由于双线程, ...
- IPv4地址分类及特征
IPv4地址分类及特征 IP地址后斜杠和数字代表的意思 其中有这样一个IP地址的格式:IP/数字,例如:111.222.111.222/24 这种格式平时在内网中用的不多,所以一下子看不懂,最后查了资 ...
- MFC 屏幕截图(libjpeg bmp转jpg)
项目中需要用到的功能. void Screenshot() { CDC *pDC; pDC = CDC::FromHandle(GetDC(GetDesktopWindow())); if(pDC = ...
- TortoiseSVN里checkout depth各选项的含义
代表四种检出深度: 1.Fully recursive——全递归:检出完整的目录树,包含所有的文件或子目录.2.Immediate children,including folders——直接子节点, ...