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>

typedef struct {
	int x;
	int y;
}pair;

typedef struct node_sub{
	pair val;
	int num;
	int height;
	struct node_sub *left;
	struct node_sub *right;
}node;

void swap(int *a, int *b){
	int tmp;
	tmp = *a;
	*a = *b;
	*b = tmp;
}

int root(int a, int *array){
	if(array[a] == a){
		return a;
	}
	else{
		array[a] = root(array[a], array);
		return array[a];
	}
}

void combine(int a, int b, int *array, int *array_rank){
	int a_root = root(a, array);
	int b_root = root(b, array);
	if(array_rank[a_root] < array_rank[b_root]){
		swap(&a_root, &b_root);
	}
	array[b_root] = a_root;
}

int compare(pair a, pair b){
	if(a.x < b.x){
		return 1;
	}
	if(a.x > b.x){
		return -1;
	}
	if(a.y < b.y){
		return 1;
	}
	if(a.y > b.y){
		return -1;
	}
	return 0;
}

int max(int a, int b){
	if(a > b){
		return a;
	}
	else{
		return b;
	}
}

int node_height(node *t){
	if(t == NULL){
		return 0;
	}
	else{
		return t->height;
	}
}

node *build_node(pair val, int num, node *left, node *right){
	node *newr;
	int left_h = node_height(left);
	int right_h = node_height(right);
	if(left_h > right_h + 1){
		node *ll = left->left;
		node *lr = left->right;
		if(node_height(ll) < node_height(lr)){
			newr = build_node(lr->val, lr->num, build_node(left->val, left->num, ll, lr->right), build_node(val, num, lr->right, right));
			free(lr);
		}
		else{
			newr = build_node(left->val, left->num, ll, build_node(val, num, lr, right));
		}
		free(left);
	}
	else if(right_h > left_h + 1){
		node *rr = right->right;
		node *rl = right->left;
		if(node_height(rr) < node_height(rl)){
			newr = build_node(rl->val, rl->num, build_node(val, num, left, rl->left), build_node(right->val, right->num, rl->left, rr));
			free(rl);
		}
		else{
			newr = build_node(right->val, right->num, build_node(val, num, rl, left), rr);
		}
		free(right);
	}
	else{
		newr = (node *)malloc(sizeof(node));
		newr->val = val;
		newr->num = num;
		newr->height = max(left_h, right_h) + 1;
		newr->left = left;
		newr->right = right;
	}
	return newr;
}

node *add_val(pair val, node *t){
	if(t == NULL){
		return build_node(val, 1, NULL, NULL);
	}
	pair t_val = t->val;
	int t_num = t->num;
	node *t_left = t->left;
	node *t_right = t->right;
	free(t);
	int comp = compare(val, t_val);
	if(comp == 0){
		return build_node(val, t_num + 1, t_left, t_right);
	}
	else if(comp == 1){
		return build_node(t_val, t_num, add_val(val, t_left), t_right);
	}
	else{
		return build_node(t_val, t_num, t_left, add_val(val, t_right));
	}
}

int return_num(pair val, node *t){
	if(t == NULL){
		return 0;
	}
	int comp = compare(val, t->val);
	if(comp == 0){
		return t->num;
	}
	else if(comp == 1){
		return return_num(val, t->left);
	}
	else{
		return return_num(val, t->right);
	}
}

int main(){
	int N, K, L, i;
	scanf("%d%d%d", &N, &K, &L);
	int *load = (int *)malloc(sizeof(int) * N);
	int *load_rank = (int *)malloc(sizeof(int) * N);
	int *train = (int *)malloc(sizeof(int) * N);
	int *train_rank = (int *)malloc(sizeof(int) * N);
	node *ans = NULL;
	for(i = 0; i < N; i++){
		load[i] = i;
		load_rank[i] = 0;
		train[i] = i;
		train_rank[i] = 0;
	}
	int p, q;
	for(i = 0; i < K; i++){
		scanf("%d%d", &p, &q);
		combine(p - 1, q - 1, load, load_rank);
	}
	int r, s;
	for(i = 0; i < L; i++){
		scanf("%d%d", &r, &s);
		combine(r - 1, s - 1, train, train_rank);
	}
	free(load_rank);
	free(train_rank);
	pair P;
	for(i = 0; i < N; i++){
		P.x = root(i, load);
		P.y = root(i, train);
		ans = add_val(P, ans);
	}
	for(i = 0; i < N; i++){
		P.x = root(i, load);
		P.y = root(i, train);
		printf("%d ", return_num(P, ans));
	}
	return 0;
}