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,siN
  • pi<qi
  • ri<si
  • When ij(pi,qi)≠(pj,qj)
  • When ij(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的更多相关文章

  1. Atcoder 2159 連結 / Connectivity(并查集+map乱搞)

    問題文N 個の都市があり.K 本の道路と L 本の鉄道が都市の間に伸びています. i 番目の道路は pi 番目と qi 番目の都市を双方向に結び. i 番目の鉄道は ri 番目と si 番目の都市を双 ...

  2. 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 ...

  3. D - 連結 / Connectivity 并查集

    http://abc049.contest.atcoder.jp/tasks/arc065_b 一开始做这题的时候,就直接蒙逼了,n是2e5,如果真的要算出每一个节点u能否到达任意一个节点i,这不是f ...

  4. AtCoder Beginner Contest 120 D - Decayed Bridges(并查集)

    题目链接:https://atcoder.jp/contests/abc120/tasks/abc120_d 题意 先给m条边,然后按顺序慢慢删掉边,求每一次删掉之后有多少对(i,j)不连通(我应该解 ...

  5. AtCoder NIKKEI Programming Contest 2019 E. Weights on Vertices and Edges (并查集)

    题目链接:https://atcoder.jp/contests/nikkei2019-qual/tasks/nikkei2019_qual_e 题意:给出一个 n 个点 m 条边的无向图,每个点和每 ...

  6. AtCoder Beginner Contest 247 F - Cards // dp + 并查集

    原题链接:F - Cards (atcoder.jp) 题意: 给定N张牌,每张牌正反面各有一个数,所有牌的正面.反面分别构成大小为N的排列P,Q. 求有多少种摆放方式,使得N张牌朝上的数字构成一个1 ...

  7. XJOI 3578 排列交换/AtCoder beginner contest 097D equal (并查集)

    题目描述: 你有一个1到N的排列P1,P2,P3...PN,还有M对数(x1,y1),(x2,y2),....,(xM,yM),现在你可以选取任意对数,每对数可以选取任意次,然后对选择的某对数(xi, ...

  8. AtCoder Beginner Contest 177 D - Friends (并查集)

    题意:有\(n\)个人,给你\(m\)对朋友关系,朋友的朋友也是朋友,现在你想要将他们拆散放到不同的集合中,且每个集合中的人没有任何一对朋友关系,问最少需要多少集合. 题解:首先用并查集将朋友关系维护 ...

  9. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

随机推荐

  1. 【BZOJ2338】【HNOI2011】数矩形 [计算几何]

    数矩形 Time Limit: 20 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 最近某歌手在研究自己的全国巡回演出, ...

  2. 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [算法]并查集+平衡树+数学+扫描线 [题解] 经典曼哈顿距离转切比雪夫距离. 曼哈顿距离:S=|x1-x2|+|y1-y2|<=c 即:max(x1-x2+y1-y2,x1-x2-y1+y2, ...

  3. Splunk笔记

    学习Splunk Fundamentals Part 2 (IOD) 和 Splunk Fundamentals Part 1课程的笔记. Chart Over By Tips: ….|chart c ...

  4. EffectiveJava读书笔记

    less, but is more. 创建和销毁对象 避免创建不必要对象 消除过期的对象引用 使可变性最小 泛型 用标记接口定义类型 检查参数有效性 返回零长度的数组或集合,而不是null 需要精确答 ...

  5. python3 面向对象补充

    f = People('egon',18,'male') 非函数hasattr # hasattr(f,'name')getattr # getattr(f,'name')setattr # seta ...

  6. Linux进程冻结技术【转】

    转自:http://blog.csdn.net/zdy0_2004/article/details/50018843 http://www.wowotech.net/ 1 什么是进程冻结 进程冻结技术 ...

  7. JS计算两个时间差的问题

    计算两个时间差的问题 function getDateIsMatching(){ var pactbegindate=$("#loanbegindate").datetimebox ...

  8. Python实现图片转字符画

    from PIL import Image def get_char(r, g, b, alpha=256): ascii_char = '''$@B%8&WM#*oahkbdpqwmZO0Q ...

  9. H5中使用Web Storage来存储结构化数据

    在上一篇对Web Storage的介绍中,可以看到,使用Storage保存key—value对时,key.value只能是字符串,这对于简单的数据来说已经够了,但是如果需要保存更复杂的数据,比如保存类 ...

  10. hdu 1062(DFS||dijkstra)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44870   Accepted: 13259 Descripti ...