[cf920E][set+dfs]
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
题意:给出一个图的补图,求原图有几个连通块。
题解:用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]的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
随机推荐
- 020 Android 常用颜色对应表
1.Android colors.xml常用颜色汇总 <?xml version="1.0" encoding="utf-8"?> <reso ...
- [.Net Core] - 当 .Net Core 版本由 1.x 升级至 2.x 后,Cookie 使用方式变更
背景 Asp.Net Core 项目升级至 2.x 版本后,Cookie 验证方式需要进行更新. 升级前:.Net Core 1.x Startup.cs public void Configure( ...
- K8S从入门到放弃系列-(13)Kubernetes集群mertics-server部署
集群部署好后,如果我们想知道集群中每个节点及节点上的pod资源使用情况,命令行下可以直接使用kubectl top node/pod来查看资源使用情况,默认此命令不能正常使用,需要我们部署对应api资 ...
- C++ 获取系统当前时间(日历时)
获取系统当前时间(日历时) //Linux & C++11 #include <chrono> #include <ctime> using namespace std ...
- AVR单片机教程——烧写hex文件
每一次build项目,编译器都会生成多个文件,其中有一个就是hex文件.之前在IDE中配置的external tools,就是把这个hex文件烧写到单片机中去的. 然而,有些时候你想运行别人的程序,但 ...
- Cortex_m7内核cache深入了解和应用
一,cache概述 从下图可以看出,从M7内核才开始有的cache,这对于从M0,M3,M4一路走来的小伙伴来说,多了一个cache就多了一个障碍. Cortex-M7 core with 32K/3 ...
- win10 idea springboot上传镜像到远程docker服务器
1. 开启2375端口,供外部访问docker vim /usr/lib/systemd/system/docker.service 修改ExecStart为下面一行内容 #ExecStart=/us ...
- 关于Mybatis中mapper.xml的传入参数简单技巧
由于在做项目的时候,我看见同事使用的传入参数类型各式各样,感觉没规律可言,闲暇的时候我就自己搭建了项目做了一些传入参数的测试(当然其实更好的方式是看源码,但是博主能力有限,毕竟入行没多久,看起来很吃力 ...
- springboot笔记03——quickstart程序原理
一.前言 一个quickstart程序仅仅让我们初步了解一个框架,我们还需要透过现象看本质才能学好一个框架.所以这篇文章分析一下我上次写的springboot的入门程序. 二.原理分析 1.依赖分析 ...
- IE hack大全
IE hack大全:http://blog.csdn.net/freshlover/article/details/12132801