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. Asp.net MVC Combres的简单用法

    第一步:添加nuget包 [1]添加 nuget包后,会自动在 webconfig里面 添加配置文件(不用改) <section name="dotless" type=&q ...

  2. POJ2240:Arbitrage(最长路+正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29374   Accepted: 12279 题目链接: ...

  3. Codeforces Round #524 (Div. 2) D. Olya and magical square

    D. Olya and magical square 题目链接:https://codeforces.com/contest/1080/problem/D 题意: 给出一个边长为2n的正方形,每次可以 ...

  4. HDU 多校对抗第三场 L Visual Cube

    Problem L. Visual Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java ...

  5. VC++使用CImage在内存中Bmp转换Jpeg图片

    之前写了一篇<VC++使用CImage在内存中Jpeg转换Bmp图片>,通过CImage实现了在内存中Jpeg转Bmp. 既然Jpeg能转Bmp,那CImage也支持Bmp转Jpeg,与上 ...

  6. fscanf函数的应用

    转摘自:http://blog.csdn.net/mxgsgtc/article/details/13005675 以前老是被从文本里读取文件,然后逐个的进行字符解析,感觉非常的慢,自从知道了fsca ...

  7. PHP设计模式-代理模式

    概念理解: 代理模式,是对简单处理程序(或指针)的增强,用于引用一个对象:这个指针被代理对象取代,代理对象位于客户端和真实程序之间,指针有一个可被多个目标利用的钩子. 参与者: client(参与者) ...

  8. C# 程序Hello World

    先创建一个工程文件->选择的是console application. 然后开始写代码如下: using System; using System.Collections.Generic; us ...

  9. c语言几个字符串处理函数的简单实现

    直接贴代码: char* strcpy(char *a,char*b){//把字符串b全部拷贝到a中 assert(a != nullptr&&b != nullptr); char ...

  10. [BZOJ1010][HNOI2008]玩具装箱toy 解题报告

    Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...