Problem:
Score : 100 points

Problem Statement

You are given an undirected graph with N vertices and 0 edges. Process Q queries of the following types.

0 u v: Add an edge (u, v).

1 u v: Print 1 if u and v are in the same connected component, 0 otherwise.

Constraints

1 \leq N \leq 200,000

1 \leq Q \leq 200,000

0 \leq u_i, v_i \lt N

Input

Input is given from Standard Input in the following format:

N Q
t_1 u_1 v_1
t_2 u_2 v_2
:
t_Q u_Q v_Q

出力

For each query of the latter type, print the answer.

Sample Input 1

4 7
1 0 1
0 0 1
0 2 3
1 0 1
1 1 2
0 0 2
1 1 3

Sample Output 1

0
1
0
1

Solution in Rust (1.42.0) (judge verdict: Accepted)

#![allow(dead_code)]
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}
fn read_vec2<T: std::str::FromStr>(n: usize) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}
#[derive(Debug)]
struct UnionFind {
    par: Vec<usize>,
    sizes: Vec<usize>
}
impl UnionFind {
    fn new(n: usize) -> UnionFind {
        let par = (0..n).collect();
        let sizes = vec![1; n];
        UnionFind{par, sizes}
    }
    fn find(&mut self, x: usize) -> usize {
        if x == self.par[x] { x } else {
            self.par[x] = self.find(self.par[x]);
            self.par[x]
        }
    }
    fn unite(&mut self, x: usize, y: usize) {
        let mut x = self.find(x);
        let mut y = self.find(y);
        if x == y { return; }
        if self.sizes[x] < self.sizes[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.par[y] = x;
        self.sizes[x] += self.sizes[y];
    }
    fn same(&mut self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }
    fn size(&mut self, x: usize) -> usize {
        let t = self.find(x);
        self.sizes[t]
    }
}
fn main() {
    let (n, q) = {
        let t: Vec<usize> = read_vec();
        (t[0], t[1])
    };
    let mut uf = UnionFind::new(n);
    for _ in 0..q {
        let t: Vec<usize> = read_vec();
        if t[0] == 0 {
            uf.unite(t[1], t[2]);
        }
        else {
            println!("{}", uf.same(t[1], t[2]) as i32);
        }
    }
}