Connectivity

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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 A by 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)

输入

The input is given from Standard Input in the following format:
N K L
p1 q1
:
pK qK
r1 s1
:
rL sL

输出

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.

样例输入

4 3 1
1 2
2 3
3 4
2 3

样例输出

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.

来源

meaning

有N个点,K条公路,L条铁路,每条路连接两个点,输出每个点既能只走公路到达,又能只走铁路到达的点的数量。

solution

对公路,铁路分别建并查集。

统计答案时hash一下,如果两个点之间既能走公路到达,又能走铁路到达,他们的答案肯定是一样的,因为属于同一个交集。

所以我们只需用一个map统计每个交集中点的数量即可。

code

#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ld INF = 1e37;
const int maxn = 200005; int pre1[maxn],pre2[maxn]; void init(int number_p,int *setName){
for(int i=1;i<=number_p;i++){
setName[i] = i;
}
} int query(int x,int *setName){
return setName[x]==x?setName[x]:setName[x] = query(setName[x],setName);
} void combine(int x,int y,int *setName){
query(x,setName)!=query(y,setName)?setName[query(x,setName)]=query(y,setName):0;
} int main() {
// IN_LB();
// ios::sync_with_stdio(false);
int n,k,l;
scanf("%d%d%d",&n,&k,&l);
init(n,pre1);
init(n,pre2);
for(int i=0;i<k;i++){
int p,q;
scanf("%d%d",&p,&q);
combine(p,q,pre1);
}
for(int i=0;i<l;i++){
int r,s;
scanf("%d%d",&r,&s);
combine(r,s,pre2);
}
for(int i=1;i<=n;i++){
query(i,pre1);
query(i,pre2);
}
map<pair<int,int>,int> mp;
for(int i=1;i<=n;i++){
mp[{pre1[i],pre2[i]}]++;
}
for(int i=1;i<=n;i++){
printf("%d%s",mp[{pre1[i],pre2[i]}],i<n?" ":"\n");
}
return 0;
}

【并查集】Connectivity @ABC049&amp;ARC065/upcexam6492的更多相关文章

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

  2. 【并查集】【set】AtCoder - 2159 - 連結 / Connectivity

    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 2159 連結 / Connectivity(并查集+map乱搞)

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

  5. Union-Find 并查集算法

    一.动态连通性(Dynamic Connectivity) Union-Find 算法(中文称并查集算法)是解决动态连通性(Dynamic Conectivity)问题的一种算法.动态连通性是计算机图 ...

  6. [leetcode] 并查集(Ⅰ)

    预备知识 并查集 (Union Set) 一种常见的应用是计算一个图中连通分量的个数.比如: a e / \ | b c f | | d g 上图的连通分量的个数为 2 . 并查集的主要思想是在每个连 ...

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

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

  8. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  9. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

随机推荐

  1. bzoj 4621: Tc605 动态规划

    题解: 一道比较简单的题目 想着想着就把题目记错了..想成了可以把某段区间覆盖为其中一个数 其实是比较简单的 每个点的贡献一定是一个区间(就跟zjoi2018那题一样) 然后问题就变成了给你n个区间让 ...

  2. Note for "Some Remarks on Writing Mathematical Proofs"

    John M. Lee is a famous mathematician, who bears the reputation of writing the classical book " ...

  3. 原 HTML5 requestFullScreen&exitFullscreen全屏兼容方案

                         摘要: html5 video全屏实现方式 首先来说,这个标题具有误导性,但这样设置改标题也是主要因为video使用的比较多 在html5中,全屏方法可以适用 ...

  4. 在启用了“编辑并继续”时,修改包含 lambda 表达式的“method”将会阻止调试会话继续进行

    将所有的引用的“复制到本地”属性都设置成false就可以了

  5. php通过CURL模拟post提交请求

    <?php header("Content-type:text/html;charset=utf-8"); class Test{ public function reque ...

  6. appium---第四个脚本,进入app,有权限弹窗的方法

    1.以淘宝为例:进入首页,会弹出好几个权限弹窗 无法使用id定位 用xpath定位

  7. react学习三

    三点运算符  (...)的用法 1:展开运算符 let a=[1,2,3]; let b=[0,...a,4];//[0,1,2,3,4] let obj ={a:1,b:2}; let obj2 = ...

  8. waf python build 工具使用流程

    waf python build 工具使用流程 waf 的 build 理念 build 了之后,可以跟踪到 ${SRC} 和 ${TGT} 有关联的文件,只有 ${SRC} 被修改过,在下次buil ...

  9. 图解安装Debian 9.5全过程

    本文将为你带来安装Debian 9.5 GNU/Linux的教程,安装全过程图文并茂讲解.安装Debian 9.5可以把ISO文件下载下来刻录成DVD安装或者采用虚拟机安装等等方法. 一.下载Debi ...

  10. CSS3 animation 练习

    css3 的动画让 html 页面变得生机勃勃,但是如何用好动画是一门艺术,接下来我来以一个demo为例,来练习css3 animation. 我们先详细了解一下animation 这个属性. ani ...