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)

// Notes:
// - Translated to Rust from https://codeforces.com/blog/entry/82400

pub struct Dsu {
    n: usize,
    // root node: -1 * (component size)
    // otherwise: parent
    parent_or_size: Vec<i32>,
}

impl Dsu {
    pub fn new(size: usize) -> Self {
        assert!(size < 1_000_000_000);

        Self {
            n: size,
            parent_or_size: vec![-1; size],
        }
    }

    pub fn merge(&mut self, a: usize, b: usize) -> usize {
        assert!(a < self.n);
        assert!(b < self.n);

        let (mut x, mut y) = (self.get_root(a), self.get_root(b));
        if x == y {
            return x;
        }

        // Make sure component of x is larger.
        if -self.parent_or_size[x] < -self.parent_or_size[y] {
            std::mem::swap(&mut x, &mut y);
        }

        // Parent component -> x
        self.parent_or_size[x] += self.parent_or_size[y];
        self.parent_or_size[y] = x as i32;
        x
    }

    // Return root of connected-component containing a.
    pub fn get_root(&mut self, a: usize) -> usize {
        assert!(a < self.n);
        if self.parent_or_size[a] < 0 {
            return a;
        }
        self.parent_or_size[a] = self.get_root(self.parent_or_size[a] as usize) as i32;
        self.parent_or_size[a] as usize
    }

    // Returns true if a and b belongs to same connected component.
    pub fn same(&mut self, a: usize, b: usize) -> bool {
        assert!(a < self.n);
        assert!(b < self.n);
        self.get_root(a) == self.get_root(b)
    }

    // Returns size of connected component containing a.
    pub fn size(&mut self, a: usize) -> usize {
        assert!(a < self.n);
        let root = self.get_root(a);
        -self.parent_or_size[root] as usize
    }

    pub fn groups(&mut self) -> Vec<Vec<usize>> {
        let mut leader_buf = vec![0; self.n];
        let mut group_size = vec![0; self.n];
        for i in 0..self.n {
            leader_buf[i] = self.get_root(i);
            group_size[leader_buf[i]] += 1;
        }
        let mut result = vec![Vec::new(); self.n];
        for i in 0..self.n {
            result[i].reserve(group_size[i]);
        }
        for i in 0..self.n {
            result[leader_buf[i]].push(i);
        }
        result.into_iter().filter(|x| !x.is_empty()).collect::<Vec<Vec<usize>>>()
    }
}

#[derive(Default)]
struct Scanner {
	buffer: Vec<String>
}
impl Scanner {
	fn next<T: std::str::FromStr>(&mut self) -> T {
		loop {
			if let Some(token) = self.buffer.pop() {
				return token.parse().ok().expect("Failed parsing input")
			}
			let mut input = String::new();
			std::io::stdin().read_line(&mut input).expect("Failed reading input");
			self.buffer = input.split_whitespace().rev().map(String::from).collect();
		}
	}
}

fn main() {
    let mut scanner = Scanner::default();

    let n = scanner.next::<usize>();
    let q = scanner.next::<usize>();

    let mut dsu = Dsu::new(n);

    for _ in 0..q {
        let typ = scanner.next::<usize>();
        let u = scanner.next::<usize>();
        let v = scanner.next::<usize>();

        if typ == 0 {
            dsu.merge(u, v);
        } else {
            assert!(typ == 1);
            println!("{}",  if dsu.same(u, v) { 1 } else { 0 });
        }
    }
}