【并查集】【set】AtCoder - 2159 - 連結 / Connectivity
Problem Statement
There are N cities. There are also K roads and L railways, extending between the cities. The i-th road bidirectionally connects the pi-th and qi-th cities, and the i-th railway bidirectionally connects the ri-th and si-th cities. No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.
We will say city A and B are connected by roads if city B is reachable from city Aby traversing some number of roads. Here, any city is considered to be connected to itself by roads. We will also define connectivity by railways similarly.
For each city, find the number of the cities connected to that city by both roads and railways.
Constraints
- 2≦N≦2*105
- 1≦K,L≦105
- 1≦pi,qi,ri,si≦N
- pi<qi
- ri<si
- When i≠j, (pi,qi)≠(pj,qj)
- When i≠j, (ri,si)≠(rj,sj)
Input
The input is given from Standard Input in the following format:
N K L
p1 q1
:
pK qK
r1 s1
:
rL sL
Output
Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.
Sample Input 1
4 3 1
1 2
2 3
3 4
2 3
Sample Output 1
1 2 2 1
All the four cities are connected to each other by roads.
By railways, only the second and third cities are connected. Thus, the answers for the cities are 1,2,2 and 1, respectively.
Sample Input 2
4 2 2
1 2
2 3
1 4
2 3
Sample Output 2
1 2 2 1
Sample Input 3
7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7
Sample Output 3
1 1 2 1 2 2 2
就用并查集暴力预处理出两张图的连通情况,然后每个并查集开个set,暴力枚举每个点,在两个图中查交集就行。注意每次查出来的交集里面的点一并记录答案并删除。
#include<cstdio>
#include<set>
using namespace std;
int fa[2][200010],__rank[2][200010];
int findroot(bool op,int x)
{
return x==fa[op][x] ? x : fa[op][x]=findroot(op,fa[op][x]);
}
void Union(bool op,int U,int V)
{
if(__rank[op][U]<__rank[op][V])
fa[op][U]=V;
else
{
fa[op][V]=U;
if(__rank[op][U]==__rank[op][V])
++__rank[op][U];
}
}
int n,m,K;
bool vis[200010];
int anss[200010];
set<int>S[2][200010];
typedef set<int>::iterator ITER;
int path[200010],e;
int main()
{
int x,y;
scanf("%d%d%d",&n,&m,&K);
for(int i=1;i<=n;++i)
fa[0][i]=fa[1][i]=i;
for(int i=1;i<=m;++i)
{
scanf("%d%d",&x,&y);
int f1=findroot(0,x),f2=findroot(0,y);
if(f1!=f2)
Union(0,f1,f2);
}
for(int i=1;i<=K;++i)
{
scanf("%d%d",&x,&y);
int f1=findroot(1,x),f2=findroot(1,y);
if(f1!=f2)
Union(1,f1,f2);
}
for(int i=0;i<=1;++i)
for(int j=1;j<=n;++j)
S[i][findroot(i,j)].insert(j);
for(int i=1;i<=n;++i) if(!vis[i])
{
e=0;
int rt[2];
bool o=0;
rt[0]=findroot(0,i);
rt[1]=findroot(1,i);
if(S[0][rt[0]].size()>S[1][rt[1]].size())
o=1;
set<int> tS=S[o][rt[o]];
for(ITER it=tS.begin();it!=tS.end();++it)
if(S[o^1][rt[o^1]].find(*it)!=S[o^1][rt[o^1]].end())
{
S[o][rt[o]].erase(*it);
S[o^1][rt[o^1]].erase(*it);
path[++e]=(*it);
vis[*it]=1;
}
for(int j=1;j<=e;++j)
anss[path[j]]=e;
}
for(int i=1;i<n;++i)
printf("%d ",anss[i]);
printf("%d\n",anss[n]);
return 0;
}
【并查集】【set】AtCoder - 2159 - 連結 / Connectivity的更多相关文章
- Atcoder 2159 連結 / Connectivity(并查集+map乱搞)
問題文N 個の都市があり.K 本の道路と L 本の鉄道が都市の間に伸びています. i 番目の道路は pi 番目と qi 番目の都市を双方向に結び. i 番目の鉄道は ri 番目と si 番目の都市を双 ...
- AtCoder Beginner Contest 049 & ARC065 連結 / Connectivity AtCoder - 2159 (并查集)
Problem Statement There are N cities. There are also K roads and L railways, extending between the c ...
- D - 連結 / Connectivity 并查集
http://abc049.contest.atcoder.jp/tasks/arc065_b 一开始做这题的时候,就直接蒙逼了,n是2e5,如果真的要算出每一个节点u能否到达任意一个节点i,这不是f ...
- AtCoder Beginner Contest 120 D - Decayed Bridges(并查集)
题目链接:https://atcoder.jp/contests/abc120/tasks/abc120_d 题意 先给m条边,然后按顺序慢慢删掉边,求每一次删掉之后有多少对(i,j)不连通(我应该解 ...
- AtCoder NIKKEI Programming Contest 2019 E. Weights on Vertices and Edges (并查集)
题目链接:https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_e 题意:给出一个 n 个点 m 条边的无向图,每个点和每 ...
- AtCoder Beginner Contest 247 F - Cards // dp + 并查集
原题链接:F - Cards (atcoder.jp) 题意: 给定N张牌,每张牌正反面各有一个数,所有牌的正面.反面分别构成大小为N的排列P,Q. 求有多少种摆放方式,使得N张牌朝上的数字构成一个1 ...
- XJOI 3578 排列交换/AtCoder beginner contest 097D equal (并查集)
题目描述: 你有一个1到N的排列P1,P2,P3...PN,还有M对数(x1,y1),(x2,y2),....,(xM,yM),现在你可以选取任意对数,每对数可以选取任意次,然后对选择的某对数(xi, ...
- AtCoder Beginner Contest 177 D - Friends (并查集)
题意:有\(n\)个人,给你\(m\)对朋友关系,朋友的朋友也是朋友,现在你想要将他们拆散放到不同的集合中,且每个集合中的人没有任何一对朋友关系,问最少需要多少集合. 题解:首先用并查集将朋友关系维护 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- Visaul Studio 常用快捷键动画演示
从本篇文章开始,我将会陆续介绍提高 VS 开发效率的文章,欢迎大家补充~ 在进行代码开发的时候,我们往往会频繁的使用键盘.鼠标进行协作,但是切换使用两种工具会影响到我们的开发速度,如果所有的操作都可以 ...
- 关于JAVA正则匹配空白字符的问题(全角空格与半角空格)
今天遇到一个字符串,怎么匹配空格都不成功!!! 我把空格复制到test.properties文件 显示“\u3000” ,这是什么? 这是全角空格!!! 查了一下 \s 不支持全角 1.& ...
- nginx+webpy+uswgi+jwplayer组合搭建流媒体服务器
转载自:http://blog.csdn.net/cjsafty/article/details/7892392 目前,由于Flash的流行,网络上绝大多数的微视频网站都采用了Flv格式来播放视频. ...
- 计算机网络中七层,五层,四层协议;IP 地址子网划分
七层协议: 7 应用层(http) 6 表示层(上层用户可以相互识别的数据:jpg) 5 会话层(不同主机不同线程间的通信) 4 运输层(tcp/ip:传输层提供端到端的透明数据服务)/差错控制和流量 ...
- 之江学院第0届校赛 qwb去面试 (找规律)
Description 某一天,qwb去WCfun面试,面试官问了他一个问题:把一个正整数n拆分成若干个正整数的和,请求出这些数乘积的最大值. qwb比较猥琐,借故上厕所偷偷上网求助,聪明的你能帮助他 ...
- bzoj1053 搜索
2013-11-16 17:43 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1053 因为使pi(prime[i])<20亿的i不 ...
- python基础之函数(自定义函数)
函数: 函数的定义: 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因变量,y是x的函数. ...
- 连接Linux服务器:Win免费SSH客户端工具
连接Linux服务器:Win免费SSH客户端工具 http://blog.csdn.net/jiangdou88/article/details/51585555
- Linux 的源码安装工具 CheckInstall
Linux 的源码安装工具 CheckInstall Checkinstall 是一个能从 tar.gz 类的 https://www.ibm.com/developerworks/cn/linux/ ...
- jQuery重置单选框和input框
取消选中单选框radio,第一种,第二种方式是使用jQuery实现的,第三种方式是基于JS和DOM实现的,大家可以看看下面的示例 本文提供了三种取消选中radio的方式,代码示例如下: 本文依赖于jQ ...