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 Map<Integer, List<Integer>> road = new HashMap<>();
  private Map<Integer, List<Integer>> rail = new HashMap<>();

  private List<Integer> idList;

  private Set<Integer> usedRoad = new HashSet<>();
  private Set<Integer> usedRail = new HashSet<>();

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

    idList = new ArrayList<>(N);
    for (int i = 0; i < N; ++i) {
      road.put(i, new ArrayList<>(K));
      rail.put(i, new ArrayList<>(L));
      idList.add(0);
    }

    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) {
      findRoad(i, i);
    }

    count = new long[N];

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

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

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

  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();
  }
}