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

import java.io.*;
import java.io.IOException;
import java.util.*;

class Main {
    final static int INF = 1 << 28;
    final static long LNF = 1L << 60;
    final static double EPS = 1e-9;
    final static double GOLDEN_RATIO = (1.0 + Math.sqrt(5)) / 2.0;

    static final long MOD = 1_000_000_007;

    public static void main(String[] args) throws Exception {
        final MyScanner sc = new MyScanner();
        long N;
        N = sc.nextLong();
        long K;
        K = sc.nextLong();
        long L;
        L = sc.nextLong();
        long[] p = new long[(int) (K)];
        long[] q = new long[(int) (K)];
        for (int i = 0; i < K; i++) {
            p[i] = sc.nextLong();
            q[i] = sc.nextLong();
        }
        long[] r = new long[(int) (L)];
        long[] s = new long[(int) (L)];
        for (int i = 0; i < L; i++) {
            r[i] = sc.nextLong();
            s[i] = sc.nextLong();
        }
        solve(N, K, L, p, q, r, s);
    }

    static void solve(long N, long K, long L, long[] p, long[] q, long[] r, long[] s) {
        UnionFind kuf = new UnionFind((int)N);
        UnionFind luf = new UnionFind((int)N);

        for (int i = 0; i < K; i++){
            p[i]--; q[i]--;
        }
        for (int i = 0; i < L; i++){
            r[i]--; s[i]--;
        }

        for (int i = 0; i < K; i++) {
            kuf.union((int)p[i], (int)q[i]);
        }
        for (int i = 0; i < L; i++) {
            luf.union((int)r[i], (int)s[i]);
        }
        StringBuffer sb = new StringBuffer();
        HashMap<ArrayList, Integer> hm = new HashMap<>();
        
        for (int i = 0; i < N; i++) {
            ArrayList al = new ArrayList<>();
            al.add(kuf.find(i));
            al.add(luf.find(i));
            if(!hm.containsKey(al)) hm.put(al, 1);
            else hm.put(al, hm.get(al)+1);
        }
        for (int i = 0; i < N; i++) {
            ArrayList al = new ArrayList<>();
            al.add(kuf.find(i));
            al.add(luf.find(i));
            sb.append(hm.get(al));
            sb.append(" ");
        }

        System.out.println(new String(sb));
    }

    static public class UnionFind {
        int[] parents;
        int[] rank;
    
        public UnionFind(int n) {
            parents = new int[n];
            rank = new int[n];
            for (int i = 0; i < n; i++) {
                parents[i] = i;
                rank[i] = 0;
            }
        }
    
        public int find(int i) {
            return root(i);
        }
    
        public boolean isSameSet(int i, int j) {
            return this.find(i) == this.find(j);
        }
    
        public void union(int i, int j) {
            int ri = root(i);
            int rj = root(j);
            if(ri == rj) return;
            else {
                if(rank[ri] < rank[rj]) parents[ri] = rj;
                else parents[rj] = ri;
                // Increment rank of root(i);
                if(rank[ri] == rank[rj]) rank[ri]++;
            }
        }
    
        private int root(int n) {
            if (parents[n] == n) {
                return n;
            } else {
                // Path compresseion.
                parents[n] = root(parents[n]);
                return parents[n];
            }
        }
    }

    static class MyScanner {

        BufferedReader br;
        StringTokenizer st;

        MyScanner(FileReader fileReader) {
            br = new BufferedReader(fileReader);
        }

        MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String nn() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        String next() {
            String ans = "";
            try {
                ans = br.readLine();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return ans;
        }

        char nextChar() {
            return nn().charAt(0);
        }

        int nextInt() {
            return Integer.parseInt(nn());
        }

        long nextLong() {
            return Long.parseLong(nn());
        }

        double nextDouble() {
            return Double.parseDouble(nn());
        }

        Integer[] niArr0(int n) {
            Integer[] ar = new Integer[n];
            for (int i = 0; i < n; i++) {
                ar[i] = nextInt();
            }
            return ar;
        }

        Integer[] niArr1(int n) {
            Integer[] ar = new Integer[n + 1];
            for (int i = 1; i <= n; i++) {
                ar[i] = nextInt();
            }
            return ar;
        }

        Long[] nlArr0(int n) {
            Long[] ar = new Long[n];
            for (int i = 0; i < n; i++) {
                ar[i] = nextLong();
            }
            return ar;
        }

        Long[] nlArr1(int n) {
            Long[] ar = new Long[n + 1];
            for (int i = 1; i <= n; i++) {
                ar[i] = nextLong();
            }
            return ar;
        }
    }

}