D - 連結 / Connectivity(并查集)

题目链接:https://atcoder.jp/contests/arc065/tasks/arc065_b

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 400400 points

Problem Statement

There are NN cities. There are also KK roads and LL railways, extending between the cities. The ii-th road bidirectionally connects the pipi-th and qiqi-th cities, and the ii-th railway bidirectionally connects the riri-th and sisi-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 AA and BB are connected by roads if city BB is reachable from city AA 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∗1052≦N≦2∗105
  • 1≦K,L≦1051≦K,L≦105
  • 1≦pi,qi,ri,si≦N1≦pi,qi,ri,si≦N
  • pi<qipi<qi
  • ri<siri<si
  • When i≠ji≠j, (pi,qi)≠(pj,qj)(pi,qi)≠(pj,qj)
  • When i≠ji≠j, (ri,si)≠(rj,sj)(ri,si)≠(rj,sj)

Input

The input is given from Standard Input in the following format:

NN KK LL
p1p1 q1q1
:
pKpK qKqK
r1r1 s1s1
:
rLrL sLsL

Output

Print NN integers. The ii-th of them should represent the number of the cities connected to the ii-th city by both roads and railways.


Sample Input 1 Copy

Copy

4 3 1
1 2
2 3
3 4
2 3

Sample Output 1 Copy

Copy

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,21,2,2 and 11, respectively.


Sample Input 2 Copy

Copy

4 2 2
1 2
2 3
1 4
2 3

Sample Output 2 Copy

Copy

1 2 2 1

Sample Input 3 Copy

Copy

7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7

Sample Output 3 Copy

Copy

1 1 2 1 2 2 2

题意:n个城市有k条公路,r条铁路。问你对于每个城市,通过铁路和公路都能到达的城市有多少个(其中自身算一个),

思路:两次并查集。

代码:

#include<bits/stdc++.h>
using namespace std;
int fa1[200009];
int fa2[200009];
int findset1(int u)
{
    return fa1[u]==u?u:fa1[u]=findset1(fa1[u]);
}
int findset2(int u)
{
   return fa2[u]==u?u:fa2[u]=findset2(fa2[u]);
}
int main()
{
    int n,k,r;
    cin>>n>>k>>r;
    for(int i=1;i<=n;i++)
    {
        fa1[i]=i;
        fa2[i]=i;
    }
    int u,v;
    for(int i=1;i<=k;i++)
    {
        cin>>u>>v;
        int fu=findset1(u);
        int fv=findset1(v);
        if(fu!=fv)
        {
            fa1[fu]=fv;
        }
    }
    for(int i=1;i<=r;i++)
    {
        cin>>u>>v;
        int fu=findset2(u);
        int fv=findset2(v);
        if(fu!=fv)
        {
            fa2[fu]=fv;
        }
    }
    map<pair<int,int>,int>mp;
    for(int i=1;i<=n;i++)  //让fa1[i],fa2[i]表示i的祖先。
    {
        findset1(i);
        findset2(i);
    }
    for(int i=1;i<=n;i++)
    {
        mp[make_pair(fa1[i],fa2[i])]++;//自身也包括至少一个。
    }
    for(int i=1;i<=n;i++)
    {
        cout<<mp[make_pair(fa1[i],fa2[i])]<<" ";
    }

}



 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值