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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::collections::*;
use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = CpReader::new(i);
    let (n, k, l) = i.read3::<usize, usize, usize>();
    let mut pqs = Vec::new();
    for _ in 0..k {
        let (p, q) = i.read2::<usize, usize>();
        pqs.push((p - 1, q - 1));
    }
    let mut rss = Vec::new();
    for _ in 0..l {
        let (r, s) = i.read2::<usize, usize>();
        rss.push((r - 1, s - 1));
    }

    let r = solve(n, &pqs, &rss);
    for i in 0..r.len() {
        if i != 0 {
            write!(o, " ").unwrap();
        }
        write!(o, "{}", r[i]).unwrap();
    }
    writeln!(o, "").unwrap();
}
fn solve(n: usize, pqs: &[(usize, usize)], rss: &[(usize, usize)]) -> Vec<usize> {
    let g1 = grouping(n, pqs);
    let g2 = grouping(n, rss);

    let mut rs = vec![0; n];

    let mut s = HashSet::new();
    let mut r = Vec::new();

    for i in 0..n {
        if rs[i] != 0 {
            continue;
        }

        let mut j = i;
        loop {
            s.insert(j);
            j = g1[j];
            if j == i {
                break;
            }
        }

        let mut j = i;
        loop {
            if s.remove(&j) {
                r.push(j);
            }
            j = g2[j];
            if j == i {
                break;
            }
        }
        for &ri in &r {
            rs[ri] = r.len();
        }
        s.clear();
        r.clear();
    }
    rs
}

// 各駅の(同一グループの次の駅番号)を返す
fn grouping(n: usize, edges: &[(usize, usize)]) -> Vec<usize> {
    let mut r = vec![0; n];
    let mut t = vec![Vec::new(); n];
    let mut v = vec![false; n];
    for &e in edges {
        t[e.0].push(e.1);
        t[e.1].push(e.0);
    }

    let mut q = Vec::new();
    for i in 0..n {
        if v[i] {
            continue;
        }
        v[i] = true;
        q.clear();
        q.push(i);
        let mut qi = 0;
        while qi < q.len() {
            for &j in &t[q[qi]] {
                if v[j] {
                    continue;
                }
                v[j] = true;
                q.push(j);
            }
            qi += 1;
        }
        for qi in 0..q.len() {
            r[q[qi]] = q[(qi + 1) % q.len()];
        }
    }
    r
}

mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        { $f:ident($($t:ident),*) } => {
            #[allow(dead_code)]
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                ($(read_next::<$t,_>(i)),*)
            }
        };
    }

    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        fn_read! { read2(T1, T2) }
        fn_read! { read3(T1, T2, T3) }
        fn_read! { read4(T1, T2, T3, T4) }
        fn_read! { read5(T1, T2, T3, T4, T5) }
    }
    fn read_next<'a, T: FromStr, I: Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

}