Problem:
Score : 400 points

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 p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-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*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)

When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

Input

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

N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

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

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define MIN(a,b) ((a)>(b)? (b): (a))
#define MAX(a,b) ((a)<(b)? (b): (a))

const long long INF = 1LL << 60;

typedef unsigned long long ll;

const long long MOD = 1000000000 + 7;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}


//Union-Find tree
struct UnionFind{
    vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
    vector<int> tree_size;  // treeの大きさ

    UnionFind(int N) : par(N),tree_size(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N;  i++) par[i] = i;
        for(int i = 0; i < N;  i++) tree_size[i] = 1;
    }  

    int root(int x) { // データxが属する木の根を再帰で得る : root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
    
    void unite(int x, int y) { //xとyの木を併合
        int rx = root(x); //xの木の根をrx
        int ry = root(y); //yの木の根をyx
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)ときはそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時 : xの根rxをyの根ryにつける
        tree_size[ry] += 1; //根をつけた場合はtree sizeを1上げる
        tree_size[rx] = -1; // 同じ木になったため、xの根は-1
        //tree_size[x] = -1;  //子になった木はtree_sizeを持たない
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

    int size(int x){ // データxが属する木のサイズを返す
        int rx = root(x); //xの木の根をrx
        return tree_size[rx];
    }
};


int main(){
    int N,K,L;
    cin >> N >> K >> L;
    int p,q;
    UnionFind road(N+1);
    UnionFind line(N+1);
    rep(i,K){
        cin >> p >> q;
        road.unite(p,q);
    }
    rep(i,L){
        cin >> p >> q;
        line.unite(p,q);
    }
    vector<set<int>> road_set(N+1);
    vector<set<int>> line_set(N+1);
    set<int> parent;
    for(int i=1; i<N+1; i++){
        road_set[road.root(i)].insert(i);
        line_set[line.root(i)].insert(i);
        parent.insert(road.root(i));
    }
    int town[N+1];
    rep(i,N+1) town[i] = 1;

    bool seen[N+1] = {false};
    UnionFind pairs(N+1);
    for(int i = 1; i<N+1; i++){
        if(seen[i] == true) continue;
        set<int> ls;
        ls = line_set[line.root(i)];
        for(auto itr_l = ls.begin(); itr_l != ls.end(); itr_l++){
            if (i == *itr_l) continue;
            if(road.root(i) == road.root(*itr_l)) {
                pairs.unite(i,*itr_l);
                seen[i] = true;
                seen[*itr_l] = true;
            }
        }
    }
    for(int i = 1;i<N+1;i++){
        cout << pairs.tree_size[pairs.root(i)]  << " ";
    }
    cout << endl;
    return 0;
}