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: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#define swap(type,a,b) do{type t=a;a=b;b=t;}while(0);
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define ll long long
#define INF 100000000
#define FOR(i,a,n) for(i=(a);i<(n);i++)
void fill(int a[],int b,int c){
	int i;
	FOR(i,0,b) a[i]=c;
	return;
}
typedef struct{
	int *par,*rank;
}Union;
void init_union(Union *u,int size){
	int i;
	u->par = (int*)calloc(size,sizeof(int));
	u->rank = (int*)calloc(size,sizeof(int));
	FOR(i,0,size) u->par[i]=i;
	return;
}
int root(Union *u,int a){
 	if(u->par[a]==a) return a;
 	else{
 		u->par[a]=root(u,u->par[a]);
 		return u->par[a];
 	}
}
int is_same(Union *u,int a,int b){
 	return root(u,a)==root(u,b);
} 
void unite(Union *u,int a,int b){
	a=root(u,a);
	b=root(u,b);
	if(a==b) return;
	if(u->rank[a]>u->rank[b])
	    u->par[b]=a;
	else{
		u->par[a]=b;
		if(u->rank[a]==u->rank[b]) u->rank[b]++;
	}
	return;
}
void delete_union(Union *u){
	free(u->par);
	free(u->rank);
	return;
}
typedef struct{
	int x,y,num;
}City;
int comp(const void *a,const void *b){
	City s=*(City*)a,t=*(City*)b;
	return s.x!=t.x?s.x-t.x:s.y-t.y;
}
int lower_bound(City a[],int n,City k){
	int left=-1; int right=n;
	while(right-left>1){
		int mid=(left+right)/2;
		if(comp((void*)&a[mid],(void*)&k)<0) left=mid;
		else right=mid;
	}
	return right;
}
int upper_bound(City a[],int n,City k){
	int left=-1; int right=n;
	while(right-left>1){
		int mid=(left+right)/2;
		if(comp((void*)&a[mid],(void*)&k)<=0) left=mid;
		else right=mid;
	}
	return right;
}
City city[200000];
int main(void)
{
	int n,k,l,a,b,i;
	Union p,q;
	City c;
	scanf("%d%d%d",&n,&k,&l);
	init_union(&p,n);
	init_union(&q,n);
	while(k-->0){
		scanf("%d%d",&a,&b);
		unite(&p,--a,--b);
	}
	while(l-->0){
		scanf("%d%d",&a,&b);
		unite(&q,--a,--b);
	}
	FOR(i,0,n) city[i].x=root(&p,i),city[i].y=root(&q,i);
	qsort(city,n,sizeof(City),comp);
	FOR(i,0,n){
		//printf("%d %d\n",city[i].x,city[i].y);
		c.x=root(&p,i),c.y=root(&q,i);
		printf("%d%c",upper_bound(city,n,c)-lower_bound(city,n,c),i==n-1?'\n':' ');
	}
	delete_union(&p);
	delete_union(&q);
	return 0;
}