【CodeForces - 920E】Connected Components? (dsu,补图连通块,STLset+map,bfs 或bitset)

题干:

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

5 5
1 2
3 4
3 2
4 2
2 5

Output

2
1 4 

题目大意:

无向图中给定n个顶点,m条不存在的边(除了这m条边,其余都存在),求图的连通分量,及每个连通分量的大小。

解题报告:

官方题解:

别从给定的边入手,而是直接对所有顶点bfs就可以了,可以证明复杂度是线性的,顶多加上容器自带的log。

当然, 其实用不到upperbound,因为bfs就是边删除边进行的,不会涉及迭代器危机。但是dfs就比较危险了,下面代码就有对应官方题解的dfs。

AC代码1:(240ms)

vector维护当前还剩多少点没有被访问到(注意巧妙的O1维护方式,正常的维护需要On的时间)。map记录边的信息

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
map<int,bool> mp[MAX];
vector<int> vv,ans;
int bfs(int x) {int res = 0;queue<int> q;q.push(x);while(q.size()) {int cur = q.front();q.pop();res++;
//		int up = vv.size();这里不能这样写,因为size是动态的。for(int i = 0; i<(int)vv.size(); i++) {int v = vv[i];if(mp[cur].count(v) == 0) {swap(vv[i],vv.back());//用来实现set的erase操作,减少常数。 vv.pop_back();//用来实现set的erase操作 i--;q.push(v);}}}return res;
}
int main()
{int n,m;cin>>n>>m;for(int i = 1; i<=n; i++) vv.pb(i);for(int x,y,i = 1; i<=m; i++) {scanf("%d%d",&x,&y);mp[x][y]=1;mp[y][x]=1;}while(!vv.empty()) {int v = vv.back();vv.pop_back();int cnt = bfs(v);ans.push_back(cnt);}sort(ans.begin(),ans.end());printf("%d\n",ans.size());for(int i = 0; i<(int)ans.size(); i++) {printf("%d ",ans[i]);}return 0 ;
}

 

AC代码2:

用set维护未被访问的点集,bfs中对于每一个点,先一遍操作,同时记录一个vector,然后再更新到set中(删除元素),这样避免了边操作边更新所带来的迭代器的变化,避免了 不好控制 的情况发生。

 

AC代码3:

用map<int,set<int> > mp  来减小内存。(实际上也无所谓,直接开2e5个set一样的)

注意dfs中,用pair来接受一个map,也就是这里ip其实代表的就是一个pair。

这里也掌握一种姿势:就算是遇到AC代码2那种  set迭代器的问题的时候,第一可以用AC代码2那种处理方式,第二可以改用map:如果需要删除it,那就先将it记录到tmp中,然后删除it,然后再upperbound一下就可以了。

AC代码4:

bitset维护未访问的点集。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#include<bitset>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
set<int> ss[MAX];
bitset<MAX> bs;
int n,m,ans[MAX],tot;
void dfs(int x) {bs[x] = 0;ans[tot]++;for(int i = bs._Find_first(); i<bs.size(); i = bs._Find_next(i)) {if(ss[x].count(i) == 0) dfs(i);}
}
int main()
{cin>>n>>m;for(int i = 1; i<=n; i++) bs[i] = 1;for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);ss[u].insert(v);ss[v].insert(u);}for(int i = 1; i<=n; i++) {if(bs[i]) tot++,dfs(i);}printf("%d\n",tot);sort(ans+1,ans+tot+1);for(int i = 1; i<=tot; i++) {printf("%d%c",ans[i],i == tot ? '\n' : ' ');}return 0 ;
}

贴一个更快的:

AC代码5:

//别人的代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
set<int>st;
vector<int> mp[MAX];
int ans[MAX];
int del[MAX];
int n,m,tot;
void Bfs(int x) {tot++;ans[tot]=1;st.erase(x);queue<int>s;s.push(x);while(!s.empty()) {int u=s.front();s.pop();int cnt=0;for(set<int>::iterator it=st.begin(); it!=st.end(); it++) {int to=*it;if(binary_search(mp[u].begin(),mp[u].end(),to) == 0) {s.push(to);ans[tot]++;del[++cnt]=to;}}for(int i=1; i<=cnt; i++)st.erase(del[i]);}
}
int main() 
{scanf("%d%d",&n,&m);tot=0;st.clear();for(int i=1; i<=m; i++) {int x,y;scanf("%d%d",&x,&y);mp[x].push_back(y);mp[y].push_back(x);}for(int i=1; i<=n; i++)st.insert(i),sort(mp[i].begin(),mp[i].end());for(int i=1; i<=n; i++) {if(st.count(i))Bfs(i);}printf("%d\n",tot);sort(ans+1,ans+tot+1);for(int i=1; i<=tot; i++) {printf("%d ",ans[i]);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440487.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【牛客 - 551F】CSL 的神奇序列(推公式,猜结论,母函数)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/551/F 来源&#xff1a;牛客网 题目描述 CSL 有一个神奇的无穷实数序列&#xff0c;他的每一项满足如下关系&#xff1a; 对于任意的正整数 n &#xff0c;有 , 并且 。 CSL 很清楚这样的序列是唯…

java什么时候创建进程,Java创建进程

Java创建进程1 进程的概念 11.1 进程的概念 11.2 进程的特征 11.3 进程与线程区别 12 进程的创建 12.1 JAVA进程的创建 12.1.1 ProcessBuilder 22.1.2 Runtime 32.1.3 Process 42.2 实例 52.2.1 创建子进程 52.2.2 进程阻塞问题 72.2.3 在java中执行java程序 111 进程的概念1.1…

【牛客 - 157E】青蛙(floyd最短路,建图)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/157/E 来源&#xff1a;牛客网 题目描述 有一只可爱的老青蛙&#xff0c;在路的另一端发现了一个黑的东西&#xff0c;想过去一探究竟。于是便开始踏上了旅途 一直这个小路上有很多的隧道&#xff0…

php移动端url,什么是PC和移动端URL路径规范化

什么叫PC和手机端URL途径规范性在网址百度搜索引擎提升的全过程中&#xff0c;会牵涉到途径方位的难题。网址中的同一个网页页面只相匹配一个网站地址。一个规范化和简易的网站地址有利于检索和捕捉客户的记忆力&#xff0c;回绝好几条途径&#xff0c;偏向同一个网页页面&…

matlab的diray在哪,matlab笔记

matlab笔记 目录 P5第一章——matlab 概述与格式 P10eps 浮点相对精度inf 无穷大i 或 j 虚数单位pi 圆周率nan 非数nargin 函数输入变量数目nargout 函数输出变量数目realmax 最大正实数realmin 最小正实数real( ) 实部imag( ) 虚部abs( ) 绝对值angle( ) 复数的相位角**matlab…

【CodeForces - 190E】Counter Attack (补图bfs,卡常,dsu)

题干&#xff1a; 无向图中给定n个顶点&#xff0c;m条不存在的边(除了这m条边&#xff0c;其余都存在)&#xff0c;求图的连通分量&#xff0c;及每个连通分量的大小。 解题报告&#xff1a; https://codeforces.com/blog/entry/4556 AC代码&#xff1a; #include<cstd…

matlab 多径 时变 信道 冲击响应,无线信道—时变冲激响应

图1无线信道的作用可以分成大尺度效应和小尺度效应。大尺度的效应就是改变了信号的平均功率&#xff0c;即B点的功率是A点的1/L。因此可以将图1等效成图2图2其中C点的平均功率等于B点的平均功率。L的数值可根据传播模型确定。影响接收机性能的只是信噪比&#xff0c;因此&#…

matlab中均线交易策略,【每日一策】Matlab量化交易策略之 均线选股策略

策略名称&#xff1a;均线选股策略策略说明&#xff1a;对沪深300全市场扫描买入条件&#xff1a;1 短均线大于长均线2 最近N个交易日短均线大于长均线的次数满足某个阈值3 当前交易日的长均线值处于某个高位出场条件&#xff1a;止损&#xff1a;价格跌破入场价的一定百分比止…

docker php 乱码,如何解决docker安装zabbix5.0界面乱码

如何解决docker安装zabbix5.0界面乱码&#xff1f;zabbix图形界面乱码如下&#xff1a;解决&#xff1a;docker部署zabbix-web和源码安装zabbix-web一样&#xff0c;字体都是存储在/usr/share/zabbix/assets/fonts/1、从windown拷贝simkai.ttf(楷体)文件到docker的zabbix-web里…

【POJ - 1330】Nearest Common Ancestors(lca,模板题)

题干&#xff1a; A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor o…

java redis 流水线,Redis系列(1) —— 流水线

写在前面去年下半年&#xff0c;出于学习Redis的目的&#xff0c;在看完《Redis in Action》一书后&#xff0c;开始尝试翻译Redis官方文档。尽管Redis中文官方网站有了译本&#xff0c;但是看别人翻译好的和自己翻译英文原文毕竟还是有很大的不同。这一系列文章之前发布在GitB…

【HDU - 6187】Destroy Walls(思维,最大生成树)

题干&#xff1a; Long times ago, there are beautiful historic walls in the city. These walls divide the city into many parts of area. Since it was not convenient, the new king wants to destroy some of these walls, so he can arrive anywhere from his castl…

【HDU - 6184】Counting Stars(三元环计数,二分,优化暴力,O(m*sqrt(m)),图论)

题干&#xff1a; Little A is an astronomy lover, and he has found that the sky was so beautiful! So he is counting stars now! There are n stars in the sky, and little A has connected them by m non-directional edges. It is guranteed that no edges connec…

php 取oracle图片,在PHP中将图片存放ORACLE中_php

我这里提供一个用php操纵blob字段的例子给你&#xff0c;希望能有所帮助&#xff01;这个例子是把用户上传的图片文件存放到BLOB中。假设有一个表&#xff0c;结构如下&#xff1a;CREATE TABLE PICTURES (ID NUMBER,http://www.gaodaima.com/44856.html在PHP中将图片存放oracl…

【HDU - 6183】Color it(CDQ分治 或 动态开点线段树)

题干&#xff1a; Do you like painting? Little D doesnt like painting, especially messy color paintings. Now Little B is painting. To prevent him from drawing messy painting, Little D asks you to write a program to maintain following operations. The speci…

php create()方法,ThinkPHP中create()方法自动验证实例

ThinkPHP中create()方法自动验证实例2020-06-16 04:24:32自动验证是ThinkPHP模型层提供的一种数据验证方法&#xff0c;可以在使用create创建数据对象的时候自动进行数据验证。原理&#xff1a;create()方法收集表单($_POST)信息并返回&#xff0c;同时触发表单自动验证&#x…

【蓝桥杯官网试题 - 历届试题】格子刷油漆(dp)

题干&#xff1a; 问题描述 X国的一段古城墙的顶端可以看成 2*N个格子组成的矩形&#xff08;如下图所示&#xff09;&#xff0c;现需要把这些格子刷上保护漆。   你可以从任意一个格子刷起&#xff0c;刷完一格&#xff0c;可以移动到和它相邻的格子&#xff08;对角相邻也…

oracle软件静默安装程序,【oracle】静默安装 oracle 11gr2

【序言】oracle 提供了静默安装方法在不适用图形界面的情况下安装 oracle 软件 ,创建db,配置netca,快速完成oracle 的部署。在以下情形中可以使用静默安装a OUI 的 GUI 界面远程交互比较慢 .b 数据库服务器无法使用图形界面访问.c 批量部署oracle (标准环境统一情况下可以使用o…

【2050 Programming Competition - 2050 一万人码 】非官方部分题解(HDU)

1001 开场白 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12147 Accepted Submission(s): 3502 Problem Description 来自世界各地的年青人在 https://2050.org.cn 握手团聚&#xff0c; 他们是航空…

oracle数据库建表视频,Oracle数据库的创建表全

CREATE TABLE "库名"."表名" ("FEE_ID" VARCHAR2(10 BYTE) constraint ABS_FEE_ID primary key,--主键&#xff0c;必须要有序列"BANK_GROUP_ID" VARCHAR2(5 BYTE),"ABS_PRODUCT_ID" VARCHAR2(30 BYTE))TABLESPACE "表…