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.*;
import java.util.*;

public class Main {
  private static boolean debug = false;
  private static boolean elapsed = false;

  private static PrintWriter _err = new PrintWriter(System.err);

  private int N;
  private int K;
  private int L;

  private long[] count;
  private List<List<Integer>> road;
  private List<List<Integer>> rail;

  private int[] idList;
  private int id;

  private boolean[] usedRoad;
  private boolean[] usedRail;

  private Map<Integer, Integer> map;
  private List<Integer> list;

  private void solve(Scanner sc, PrintWriter out) {
    N = sc.nextInt();
    K = sc.nextInt();
    L = sc.nextInt();
    sc.nextLine();

    usedRoad = new boolean[N];
    usedRail = new boolean[N];
    idList = new int[N];
    road = new ArrayList<>(N);
    rail = new ArrayList<>(N);
    for (int i = 0; i < N; ++i) {
      road.add(new ArrayList<>());
      rail.add(new ArrayList<>());
    }

    for (int i = 0; i < K; ++i) {
      int p = sc.nextInt() - 1;
      int q = sc.nextInt() - 1;

      road.get(p).add(q);
      road.get(q).add(p);
    }

    for (int i = 0; i < L; ++i) {
      int r = sc.nextInt() - 1;
      int s = sc.nextInt() - 1;

      rail.get(r).add(s);
      rail.get(s).add(r);
    }

    for (int i = 0; i < N; ++i) {
      if (!usedRoad[i]) {
        findRoad(i);
        ++id;
      }
    }

    count = new long[N];

    map = new HashMap<>();
    list = new ArrayList<>(N);
    for (int i = 0; i < N; ++i) {
      if (!usedRail[i]) {
        map.clear();
        list.clear();
        findRail(i);
        for (Integer j : list) {
          count[j] += map.get(idList[j]);
        }
      }
    }

    boolean isFirst = true;
    for (long l : count) {
      if (!isFirst) {
        out.print(" ");
      } else {
        isFirst = false;
      }
      out.print(l);
    }
    out.println();
  }

  private void findRoad(int p) {
    usedRoad[p] = true;
    idList[p] = id;
    List<Integer> list = road.get(p);
    for (Integer q : list) {
      if (!usedRoad[q]) {
        findRoad(q);
      }
    }
  }
  private void findRail(int r) {
    usedRail[r] = true;
    list.add(r);
    Integer key = idList[r];
    if (!map.containsKey(key)) {
      map.put(key, 1);
    } else {
      map.put(key, map.get(key) + 1);
    }
    List<Integer> list2 = rail.get(r);
    for (Integer s : list2) {
      if (!usedRail[s]) {
        findRail(s);
      }
    }
  }

  private long C(long n, long r) {
    long res = 1;
    for (long i = n; i > n - r; --i) {
      res *= i;
    }
    for (long i = r; i > 1; --i) {
      res /= i;
    }
    return res;
  }
  private long P(long n, long r) {
    long res = 1;
    for (long i = n; i > n - r; --i) {
      res *= i;
    }
    return res;
  }
  private long ceil2pow(long n) {
    if (n == 0) {
      return 1;
    }
    n--;
    n |= (n >>> 1);
    n |= (n >>> 2);
    n |= (n >>> 4);
    n |= (n >>> 8);
    n |= (n >>> 16);
    n++;
    return n;
  }
  /*
   * 10^10 > Integer.MAX_VALUE = 2147483647 > 10^9
   * 10^19 > Long.MAX_VALUE = 9223372036854775807L > 10^18
   */
  public static void main(String[] args) {
    long S = System.currentTimeMillis();

    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    new Main().solve(sc, out);
    out.flush();

    long G = System.currentTimeMillis();
    if (elapsed) {
      _err.println((G - S) + "ms");
    }
    _err.flush();
  }
}