[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 ...
随机推荐
- [转帖]TPC-C解析系列01_TPC-C benchmark测试介绍
TPC-C解析系列01_TPC-C benchmark测试介绍 http://www.itpub.net/2019/10/08/3334/ 学习一下. 自从蚂蚁金服自研数据库OceanBase获得TP ...
- [SQL] - Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 问题之解决
场景: 使用 Oracle.DataAccess.dll 访问数据库时,OracleDataAdapter 执行失败. 异常: System.AccessViolationException was ...
- Java的设计模式(5)-- 策略模式
定义一系列算法,把它们一个个封装起来,并且使它们可以相互替换,本模式使得算法可以独立于使用它的客户而变化.策略模式包括以下三种角色 策略(Strategy):策略是一个接口,该接口定义若干个算法标识, ...
- P1308(字符串类,处理字符串查找)
题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给 ...
- Apache Rewrite 规则详解知识大全
Rewrite是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能. 1.Rewrite标志 R[=code](for ...
- Django项目配置参数大全
数据库的配置 配置文件: settings.pyDATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NA ...
- Git学习(一)——熟悉git操作流程
本篇笔记前面都是仔细介绍使用方法,如果想跳过这些直接熟悉怎么使用,跳到最后一个知识点完整流程介绍. git 了解:特点.优点 1.git用户版和服务版整合在一起,任何机器上都安装了两个版本 2.git ...
- NOP法破解
目录 步骤 步骤 OD载入目标软件,汇编窗口右键搜索字符串,发现错误类提示字符串,双击该字符串来到该段代码位置. 向上寻找到跳转到本段错误提示代码的跳转指令,用NOP指令填充跳转指令. 保存修改后的代 ...
- kafka的生产者配置以及发送信息的三种方式
1.Fire-and-forget 这种方式是不管发送成功与否,客户端都会返回成功.尽管大多数的时候Kafka 在发送失败后,会自己重新自动再一次发送消息,但是也会存在丢失消息的风险 Producer ...
- Python之Django
一.Django是一个开放源代码的Web应用框架,由Python写成.采用了MTV的框架模式,即模型M,视图V和模版T.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是CM ...