[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 ...
随机推荐
- 判断List集合为空还是null的正确打开方式
事故场景还原 最近在写一个项目的时候遇到一个这样一个问题,我简单的还原一下场景,这是模拟一个简单的管理系统 ① 一张简单的客户表 CREATE TABLE customer( id INT(11) N ...
- composer 无法配置命令行写入配置文件问题
composer config repo.packagist composer https://packagist.phpcomposer.com 这条命令无法修改composer.json 添加中国 ...
- TypeScript 命名空间
随着代码的不断增加,我们需要有组织的组合代码.TypeScript在1.x版本中提供了命名空间的方式进行代码组织,这也是TypeScript官方代码的组织方式.同时,TypeScript还实现了Jav ...
- k8s 证书反解
k8s证书反解 1.将k8s配置文件(kubelet.kubeconfig)中client-certificate-data:内容拷贝 2.echo "client-certificate- ...
- 学习RadonDB源码(三)
1. 所谓第四代语言 SQL是一种典型的第四代语言,即4GL,这种语言的突出特点是编写者不需要关注怎么做,只需要告诉系统我要什么就可以. 虽然4GL是这样的一种语言,大大简化了编写者的编写难度,其实底 ...
- SQLSERVER远程链接Oracle数据库
原文地址: http://blog.sina.com.cn/s/blog_45eaa01a0102ywuk.html 使用SQL链接服务器远程访问Oracle数据库 在本机上通过SQL数据库的链接 ...
- nodejs模块fs——文件操作api
// fs模块常用api // 读取文件 .写入文件 .追加文件. 拷贝文件 .删除文件 // 读取文件 // fs.readFile(path[, options], callback) // fs ...
- VS.NET(C#)--2.8_CCS样式
CSS样式 文件命名 StyleSheet.css body { font-size:12px } .button { color:Red; text-decoration:none; ...
- WPF不同方式快捷键判断
private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { //单个按键e.Key方式判断 if (e.Key == Key ...
- iis安装ssl证书
在证书控制台下载IIS版本证书,下载到本地的是一个压缩文件,解压后里面包含.pfx文件是证书文件,pfx_password.txt是证书文件的密码. 友情提示: 每次下载都会产生新密码,该密码仅匹配本 ...