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 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

#define NEW(p,n){p=malloc((n)*sizeof(p[0]));if(p==NULL){printf("not enough memory\n");exit(1);};}
//pの型の変数n個の要素分のメモリを確保し、そのアドレスをpに代入するマクロ

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

typedef struct slobj_{
    struct slobj_ *next;
    int v; //uから枝が伸びている節点v
}* slobj;

//slobj型のリストの定義
typedef struct{
    slobj head;
    slobj tail;
}* slist;

//グラフの構造体の定義
typedef struct{
    int n; //節点数
    slist* E; //枝リストの配列E[0]~E[n-1],未ソート単方向
}* graph;

//vにn,dにxを持つslobj型のリスト要素のメモリ確保+作成する関数
slobj slobj_new(int n){
    slobj p;
    NEW(p,1);
    p->v=n;
    p->next=NULL;
    return p;
}

//slist型のメモリを1個確保し、空リスト1つを作る関数
slist slist_new(void){
    slist L;
    NEW(L,1);
    L->head=NULL;
    L->tail=NULL;
    return L;
}

//各slistを空リストにした,節点nのgraph型のメモリを確保する関数(Eは0カウント)
graph graph_new(int n){
    graph G;
    NEW(G,1);
    
    G->n=n;
    
    NEW(G->E,G->n);
    int i;
    for(i=0;i<G->n;i++){
        G->E[i]=slist_new();
    }
    
    return G;
}

//リストLの末尾に要素rを追加する関数
void slist_append_tail(slist L,slobj r){
    if(L->head==NULL){
        L->head=r;
        L->tail=r;
    }
    else{
        L->tail->next=r;
        L->tail=r;
    }
    
    return;
}

//節点i(0~n-1)から節点j(0~n-1)に、重さxの枝を追加する関数
void add_edge(graph G, int i, int j){
    //枝データを追加
    slobj p=slobj_new(j);
    slist_append_tail(G->E[i],p);

    return;
}

//根(節点i)からの長さをdfs
void dfs(graph G, int i, int* d){
	slobj p=G->E[i]->head;
	while(p!=NULL){
		if(d[p->v]==-1){ //未探索なら
			d[p->v]=d[i];
			dfs(G,p->v,d);
		}
		p=p->next;
	}
	return;
}

int main(void){
    int N,K,L;
    scanf("%d%d%d",&N,&K,&L);

    graph Road=graph_new(N);
    graph Train=graph_new(N);

    for(int i=0;i<K;i++){
        int p,q;
        scanf("%d%d",&p,&q);
        add_edge(Road,p-1,q-1);
        add_edge(Road,q-1,p-1);
    }

    for(int i=0;i<L;i++){
        int r,s;
        scanf("%d%d",&r,&s);
        add_edge(Train,r-1,s-1);
        add_edge(Train,s-1,r-1);
    }

    int* d1;
    int* d2;
    NEW(d1,N);
    NEW(d2,N);
    for(int i=0;i<N;i++){
        d1[i]=-1;
        d2[i]=-1;
    }

    //roadのSCC
    int c1=0;
    for(int i=0;i<N;i++){
        if(d1[i]==-1){
            d1[i]=c1;
            c1++;
            dfs(Road,i,d1);
        }
    }

    //trainのSCC
    int c2=0;
    for(int i=0;i<N;i++){
        if(d2[i]==-1){
            d2[i]=c2;
            c2++;
            dfs(Train,i,d2);
        }
    }

    int grp[c1][c2];
    for(int i=0;i<c1;i++){
        for(int j=0;j<c2;j++) grp[i][j]=0;
    }

    for(int i=0;i<N;i++) grp[d1[i]][d2[i]]++;

    for(int i=0;i<N;i++){
        printf("%d ",grp[d1[i]][d2[i]]);
    }
    printf("\n");

    return 0;
}