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 ...
随机推荐
- TensorFlow——深度学习笔记
深度学习与传统机器学习的区别 传统机器学习输入的特征为人工提取的特征,例如人的身高.体重等,深度学习则不然,它接收的是基础特征,例如图片像素等,通过多层复杂特征提取获得. 深度学习.人工智能.机器学习 ...
- Elasticsearch查询优化总结
查询优化 1 从提高查询精确度进行优化: 本部分主要针对全文搜索进行探究. 1.1 倒排索引 1.1.1 什么是倒排索引: 一个倒排索引由文档中所有不重复词的列表构成,对于其中每个词,有一个包含它的文 ...
- java窗口程序初学组件小总结
容器(可以放组件)JPanel默认的布局管理器是FlowLayout:JPanel panel=new JPanel(); 按钮JButton(可以为汉字 也可以是图片):JButton button ...
- Could not automatically select an Xcode project. Specify one in your Podfile like so
需要将Podfile文件放置在根目录下,而不能放置在项目的里面. 更改路径即可
- 01、dos命令行的常用命令
cd 进入指定目录cd.. 返回上一级目录cd\ 退回盘符根目录dir 列出当前目录下的文件以及文件夹md 创建目录rd 删除目录del 删除文件cls ...
- 【bzoj3530】[Sdoi2014]数数 AC自动机+数位dp
题目描述 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3223不是幸运 ...
- Tomcat源码浅析
最近在学习tomcat源码,算是把tomcat的整个流程梳理通了. 从上图来看,tomcat把模块化使用到了极致,配合组件生命周期的管理,让代码看起来结构清晰,而且很容易进行业务扩展. 1.上图的接口 ...
- C++中include<> 与 include" " 的区别
<>时先去系统目录中找头文件,如果没有再到当前目录下找.所以像标准的头文件 stdio.h, stdlib.h等都用<>; ""则首先到当前目录下找,如果找 ...
- [GDOI2016] 疯狂动物园 [树链剖分+可持久化线段树]
题面 太长了,而且解释的不清楚,我来给个简化版的题意: 给定一棵$n$个点的数,每个点有点权,你需要实现以下$m$个操作 操作1,把$x$到$y$的路径上的所有点的权值都加上$delta$,并且更新一 ...
- hdu 1512
思路:用并查集即可,每次合并的时候将小的集合合并到大的集合上去.理论上的平均复杂度是n*lgn*lgn. #include<map> #include<queue> #incl ...