E. Connected Components?
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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 ≤ nx ≠ 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
input

Copy
5 5
1 2
3 4
3 2
4 2
2 5
output

Copy
2
1 4
题意:给出一个图的补图,求原图有几个连通块。
题解:用set维护没被访问过的节点,在dfs每一层用vector存储每个与节点相邻的节点,并从set里面删除这些节点,每个节点只会被访问一次,所以复杂度不会太大,尤其注意set删除迭代器位置元素的方法
 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+;
#define debug(x) cout<<"["<<#x<<"]"<<x<<endl;
struct edge{
int fr;
int to;
int nex;
}e[maxn<<];
set<int>st;
int cnt,colo[maxn],head[maxn],col[maxn],coloo[maxn];
void adde(int x,int y){
e[cnt].fr=x;
e[cnt].to=y;
e[cnt].nex=head[x];
head[x]=cnt++;
}
void dfs(int u,int fa,int c){
colo[u]=c;
coloo[c]++;
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].to;
col[v]=u;
}
vector<int>g;
for(auto it=st.begin();it!=st.end();){
if(col[(*it)]!=u){
g.push_back((*it));
st.erase(it++);
}
else{
it++;
}
}
for(int i=;i<g.size();i++){
if(!colo[g[i]]){
dfs(g[i],u,c);
}
}
}
int main(){
int n,m;
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)st.insert(i);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
adde(a,b);
adde(b,a);
}
int ans=;
for(int i=;i<=n;i++){
if(!colo[i]){
ans++;
dfs(i,-,ans);
}
}
printf("%d\n",ans);
sort(coloo+,coloo++ans);
for(int i=;i<=ans;i++){
printf("%d",coloo[i]);
char cc=(i==ans)?'\n':' ';
printf("%c",cc);
}
return ;
}

set正确的删除元素的方法

     for(auto it=st.begin();it!=st.end();){
if(col[(*it)]!=u){
g.push_back((*it));
st.erase(it++);
}
else{
it++;
}
}

错误的删除方法

     for(auto it=st.begin();it!=st.end();it++){
if(col[(*it)]!=u){
g.push_back((*it));
st.erase(it);
}
}

[cf920E][set+dfs]的更多相关文章

  1. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  2. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  3. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  4. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  7. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  8. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  9. 【BZOJ-1146】网络管理Network DFS序 + 带修主席树

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3495  Solved: 1032[Submi ...

随机推荐

  1. [转帖]linux操作系统测试工具

    linux操作系统测试工具 http://cfdtesting.com/879156.html 作者: minions_222      来源: CFDTesting.com采编      发布于:  ...

  2. Python19之函数和过程

    一.函数和过程 函数和过程都是指一段实现特定功能的代码段,如果该代码段有返回值则称为函数,否则称为过程. 注:Python中只有函数而没有过程,就算是函数体内没有return语句返回一个值,Pytho ...

  3. vagrant root 登录虚拟机

    这个问题本来觉得是个特别简单的问题,昨天弄的时候折腾了半晚上.所以打算记录下过程,主要也被网上的各种信息误导了. 1 先看下我这vagrant配置信息 Vagrant.configure(" ...

  4. websocket 协议简述

    WebSocket 是一种网络通信协议,RFC 6455 定义了它的通信标准,属于服务器推送技术的一种 由于 HTTP 无状态.无连接.单向通信的特性,导致 HTTP 协议无法实现服务器主动向客户端发 ...

  5. git 修改注释

    原文:https://www.jianshu.com/p/098d85a58bf1 修改最后一条注释: git commit --amend 如果已经推送到远程,强制push到远程仓库: git pu ...

  6. C# mailKit 发邮件 简单代码

    public static async Task<bool> SendMailAsync22(string Name, string receive, string sender, str ...

  7. C#正则表达式根据分组命名取值

    string[] regexList = new string[] { @"^(?<TickerPart1>[0-9A-Z])[ 0_]?(?<TickerPart2> ...

  8. ORA-01790 错误处理 SQL同一数据库中,两个查询结果数据类型不同时的union all 合

    转自: 出现这种错误,要先看一下是不是sql中有用到连接:union,unio all之类的,如果有,需要注意相同名称字段的数据类型一定要相同. 所以在union 或者union all 的时候造成了 ...

  9. iOS 如何判断一个点在某个指定区域中

    在iOS 开发中会遇到 判断位置的情况 iOS 自己都有函数实现的这些功能. 判断一个点是否在这个rect区域中 bool CGRectContainsPoint(CGRect rect,CGPoin ...

  10. 部署vue项目到阿里云服务器(Ubuntu16.04 64位)

    上传文件 1.通过Xftp将vue项目文件上传至云服务器:由于node_modules这个依赖包体积较大,上传较慢,上传时跳过,在云服务器上重新进行npm install安装依赖包即可: 2.也可通过 ...