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; 
const int dx[]={1,0,-1,0,1,-1,-1,1}; 
const int dy[]={0,1,0,-1,1,1,-1,-1}; 
const int INF = 1e9; 
const long long LINF = 1e18; 
const double EPS = 1e-8; 
#define pb push_back 
#define mk make_pair 
#define fr first 
#define sc second 
#define ll long long 
#define reps(i,j,k) for(int i = (j); i < (k); ++i) 
#define rep(i,j) reps(i,0,j) 
#define all(a) (a).begin(),(a).end() 
#define MOD 1000000007 
typedef pair<int,int> Pii;
typedef pair<Pii,int> P;
typedef vector<int> vi;
struct UnionFind{
    std::vector < int > rank;
    std::vector < int > parent;
    std::vector < int > size;
    UnionFind(int N){
        this->rank.resize(N);
        this->parent.resize(N);
        this->size.resize(N);
        for (int i = 0; i < N; ++i) {
            this->rank[i] = 1;
            this->parent[i] = i;
            this->size[i] = 1;
        }
    }
    ~UnionFind(){
        this->parent.clear();
        this->rank.clear();
        this->size.clear();
        std::vector<int>().swap(this->rank);
        std::vector<int>().swap(this->parent);
        std::vector<int>().swap(this->size);
    }
    int find(int a){
        if (this->parent[a] == a) {
            return a;
        }
        return this->parent[a] = find(this->parent[a]);
    }
    
    bool unite(int a, int b){
        int pa = this->find(a);
        int pb = this->find(b);
        
        if (pa == pb) {
            return false;
        }
        if (this->rank[pa] < this->rank[pb]) {
            this->parent[pa] = pb;
            this->size[pb] += this->size[pa];
        }
        else {
            this->parent[pb] = pa;
            this->size[pa] += this->size[pb];
            if (this->rank[pa] == this->rank[pb]) {
                this->rank[pa]++;
            }
        }
        return true;
    }
    
    bool isSame(int a,int b){
        return (this->parent[a] == this->parent[b]);
    }
    
    int getSize(int a){
        return this->size[find(a)];
    }
};
vi ga[210000];
vi gb[210000];
int memo[210000];
void solve(int now,int type, UnionFind &uf){
    /*if(memo[now]){
        return ;
    }*/
    memo[now] = 1;
    if(type == 0){
        rep(i,ga[now].size()){
            if(memo[ga[now][i]]){
                continue;
            }
            uf.unite(now,ga[now][i]);
            solve(ga[now][i],type,uf);
        }
    }
    else{
        rep(i,gb[now].size()){
            if(memo[gb[now][i]]){
                continue;
            }
            memo[gb[now][i]] = 1;
            uf.unite(now,gb[now][i]);
            solve(gb[now][i],type,uf);
        }
    }
    return ;
}
map < Pii, int > x;
int main(){
    int N,K,L;
    cin >> N >> K >> L;
    UnionFind P = UnionFind(N);
    rep(i,K){
        int p,q;
        cin >> p >> q;
        --p;--q;
        P.unite(p,q);
    }
    
    rep(i,L){
        int r,s;
        cin >> r >> s;
        --r;--s;
        if(P.isSame(r,s)){
            gb[r].pb(s);
            gb[s].pb(r);
        }
    }
    
    UnionFind uf = UnionFind(N);
    memset(memo,0,sizeof(memo));
    rep(i,N){
        solve(i,1,uf);
    }
    
    rep(i,N){
        if(i == N-1){
            cout << uf.getSize(i)<< endl;
        }
        else{
            cout << uf.getSize(i) << " ";
        }
    }
    
    return 0;
}