【并查集】【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 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- 怎么利用idea自带的工具,不需要 重启tomcat或则其他服务,js代码自动生效
idea中有一个工具:可以直接upload,能让你修改的界面直接可以看到,不需要重启服务. 依次点击的按钮如下: 点击进入的界面这个填的只是一个示例,在各位的电脑上肯定不行,大家依据实际情况填写.
- missing blocks错误
Datanode的日志中看到: 10/12/14 20:10:31 INFO hdfs.DFSClient: Could not obtain block blk_XXXXXXXXXXXXXXXXXX ...
- Linux基础(1)
一.Linux的安装及相关配置 1.VMware Workstation安装CentOS7.0 图文版 详细步骤可以看连接:http://blog.csdn.net/alex_my/article/d ...
- Flume的安装,配置及使用
1,上传jar包 2,解压 3,改名 4,更改配置文件 将template文件重镜像 root@Ubuntu-1:/usr/local/apache-flume/conf# cat flume-env ...
- 单源最短路模板_SPFA_Dijkstra(堆优化)_C++
随手一打就是标准的SPFA,默认1号节点为出发点,当然不用 f 判断是否在队里也可以,只是这样更优化一点 void spfa() { int i,x,k; ;i<=n;i++) { d[i]=o ...
- Runtime类 调用windows程序。
import java.io.*; public class webcyz { /** * @param args */ public static void main(String[] args) ...
- IC卡的传输协议(1)-字符传输协议T=0【转】
转自:http://bbs.ednchina.com/BLOG_ARTICLE_172022.HTM 在异步半双工传输协议中,主要定义了终端为实现传输控制和特殊需要发出的命令及这些命令的处理过程. 在 ...
- eclipse断点调试时不能进入断点调试
页面JavaScript代码有错误!!!F12调试.
- 【 Python 】函数的参数
一.默认参数: 默认参数可以简化函数的调用,设置默认参数时,有几点要注意: 1,必选参数在前,默认参数在后,否则python的解释器会报错. 2,如何设置默认参数. 当函数有多个参数时,把变化大的参数 ...