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)

//  ____            _                  _                       _       _
// |  _ \ _   _ ___| |_    ___ _ __   | |_ ___ _ __ ___  _ __ | | __ _| |_ ___
// | |_) | | | / __| __|  / __| '_ \  | __/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
// |  _ <| |_| \__ \ |_  | (__| |_) | | ||  __/ | | | | | |_) | | (_| | ||  __/
// |_| \_\\__,_|___/\__|  \___| .__/___\__\___|_| |_| |_| .__/|_|\__,_|\__\___|
//                            |_| |_____|               |_|

//https://github.com/manta1130/Competitive_Programming_Template_Rust

mod union_find {

///素集合データ構造
pub struct UnionFind {
    v: Vec<usize>,
    h: Vec<usize>,
}

impl UnionFind {
    ///要素数nの素集合データ構造を構築する。
    pub fn new(n: usize) -> UnionFind {
        let mut v = Vec::new();
        for i in 0..n {
            v.push(i);
        }
        let h = vec![1; n];
        UnionFind { v: v, h: h }
    }

    ///親を探索する。
    pub fn find(&mut self, a: usize) -> usize {
        if a == self.v[a] {
            return a;
        }

        let buf = self.v[a];
        let parent = self.find(buf);
        self.v[a] = parent;
        parent
    }

    ///親が同じか判定する。
    pub fn same(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }

    ///結合する。
    pub fn union(&mut self, a: usize, b: usize) {
        let parent_a = self.find(a);
        let parent_b = self.find(b);
        if self.h[parent_a] < self.h[parent_b] {
            self.v[parent_a] = parent_b;
        } else {
            self.v[parent_b] = parent_a;

            if self.h[parent_a] == self.h[parent_b] {
                self.h[parent_a] += 1;
            }
        }
    }
}
}

#[allow(unused_imports)]
use proconio::{
    fastout, input,
    marker::{Bytes, Chars, Isize1, Usize1},
};

use union_find::*;

#[fastout]
fn main() {
    input! {
        n:usize,q:usize,
    }
    let mut uf = UnionFind::new(n);
    for _ in 0..q {
        input! {
            t:usize,u:usize,v:usize
        }
        if t == 0 {
            uf.union(u, v);
        } else {
            println!("{}", if uf.same(u, v) { "1" } else { "0" });
        }
    }
}