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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class Main {

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int L = sc.nextInt();
		List<Pair> pq = new ArrayList<>();
		for (int i = 0; i < K; i++) {
			int p = sc.nextInt() - 1;
			int q = sc.nextInt() - 1;

			pq.add(new Pair(p, q));
			pq.add(new Pair(q, p));
		}
		Map<Integer, List<Integer>> road = pq.stream()
				.collect(Collectors.groupingBy(Pair::getKey, Collectors.mapping(Pair::getValue, Collectors.toList())));

		List<Pair> rs = new ArrayList<>();
		for (int i = 0; i < L; i++) {
			int r = sc.nextInt() - 1;
			int s = sc.nextInt() - 1;

			rs.add(new Pair(r, s));
			rs.add(new Pair(s, r));
		}
		Map<Integer, List<Integer>> train = rs.stream()
				.collect(Collectors.groupingBy(Pair::getKey, Collectors.mapping(Pair::getValue, Collectors.toList())));

		Map<Integer, Set<Integer>> roadMap = new HashMap<>();
		for (int i = 0; i < N; i++) {
			if (roadMap.containsKey(i)) {
				continue;
			}

			Deque<Integer> queue = new ArrayDeque<>();
			Set<Integer> set = new HashSet<Integer>();
			queue.add(i);

			while (!queue.isEmpty()) {
				Integer index = queue.poll();
				if (set.contains(index)) {
					continue;
				}
				set.add(index);

				List<Integer> list = road.getOrDefault(index, Collections.emptyList());
				for (Integer next : list) {
					if (!set.contains(next)) {
						queue.add(next);
					}
				}
			}

			for (Integer index : set) {
				roadMap.put(index, set);
			}
		}

		Map<Integer, Set<Integer>> trainMap = new HashMap<>();
		for (int i = 0; i < N; i++) {
			if (trainMap.containsKey(i)) {
				continue;
			}

			Deque<Integer> queue = new ArrayDeque<>();
			Set<Integer> set = new HashSet<Integer>();
			queue.add(i);

			while (!queue.isEmpty()) {
				Integer index = queue.poll();
				if (set.contains(index)) {
					continue;
				}
				set.add(index);

				List<Integer> list = train.getOrDefault(index, Collections.emptyList());
				for (Integer next : list) {
					if (!set.contains(next)) {
						queue.add(next);
					}
				}
			}

			for (Integer index : set) {
				trainMap.put(index, set);
			}
		}

		String[] result = new String[N];
		for (int i = 0; i < N; i++) {
			Set<Integer> set = new HashSet<>(roadMap.getOrDefault(i, Collections.emptySet()));
			set.retainAll(trainMap.getOrDefault(i, Collections.emptySet()));

			result[i] = Integer.toString(set.size());
		}

		System.out.println(String.join(" ", result));
	}

	private static boolean isDebug = System.getProperty("sun.desktop") != null;

	private static void debug(Object... o) {
		if (isDebug) {
			System.err.println(Arrays.deepToString(o));
		}
	}

	public static class Pair {
		private int key;
		private int value;

		public Pair(int key, int value) {
			super();
			this.key = key;
			this.value = value;
		}

		public int getKey() {
			return key;
		}

		public int getValue() {
			return value;
		}

		public void setValue(int value) {
			this.value = value;
		}

		@Override
		public String toString() {
			return "{" + key + ", " + value + "}";
		}
	}

	public static class Scanner {
		private BufferedInputStream inputStream;

		public Scanner(InputStream in) {
			inputStream = new BufferedInputStream(in);
		}

		public int nextInt() throws IOException {
			int num = 0;

			int read = skip();
			do {
				num = num * 10 + (read - 0x30);
			} while ((read = inputStream.read()) > 0x20);

			return num;
		}

		public void fill(int[] a) throws IOException {
			for (int i = 0; i < a.length; i++) {
				a[i] = nextInt();
			}
		}

		public void fill(int[] a, int[] b) throws IOException {
			if (a.length != b.length) {
				throw new IllegalArgumentException();
			}

			for (int i = 0; i < a.length; i++) {
				a[i] = nextInt();
				b[i] = nextInt();
			}
		}

		public long nextLong() throws IOException {
			long num = 0;

			int read = skip();
			do {
				num = num * 10 + (read - 0x30);
			} while ((read = inputStream.read()) > 0x20);

			return num;
		}

		public void fill(long[] a) throws IOException {
			for (int i = 0; i < a.length; i++) {
				a[i] = nextLong();
			}
		}

		public void fill(long[] a, long[] b) throws IOException {
			if (a.length != b.length) {
				throw new IllegalArgumentException();
			}

			for (int i = 0; i < a.length; i++) {
				a[i] = nextLong();
				b[i] = nextLong();
			}
		}

		public long[] nextLong(int n) throws IOException {
			long[] array = new long[n];
			for (int i = 0; i < n; i++) {
				array[i] = nextLong();
			}

			return array;
		}

		public String next() throws IOException {
			StringBuilder builder = new StringBuilder();

			int read = skip();
			do {
				builder.append((char) read);
			} while ((read = inputStream.read()) > 0x20);

			return builder.toString();
		}

		private int skip() throws IOException {
			int read;
			while ((read = inputStream.read()) <= 0x20)
				;

			return read;
		}
	}
}