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 Go (1.14.1) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
)

func generated_alib_swap(a int, b int) (int, int) {
	return b, a
}
func (u generated_alib_UnionFind) Size(x int) int {
	return -u.par[u.Find(x)]
}
func (u generated_alib_UnionFind) Same(x, y int) bool {
	return u.Find(x) == u.Find(y)
}
func (u generated_alib_UnionFind) Union(x, y int) {
	xr := u.Find(x)
	yr := u.Find(y)
	if xr == yr {
		return
	}
	if u.Size(yr) < u.Size(xr) {
		yr, xr = generated_alib_swap(yr, xr)
	}
	u.par[yr] += u.par[xr]
	u.par[xr] = yr
}
func (u generated_alib_UnionFind) Find(x int) int {
	if u.par[x] < 0 {
		return x
	}
	u.par[x] = u.Find(u.par[x])
	return u.par[x]
}
func generated_alib_NewUnionFind(N int) *generated_alib_UnionFind {
	u := new(generated_alib_UnionFind)
	u.par = make([]int, N)
	for i := range u.par {
		u.par[i] = -1
	}
	return u
}

type generated_alib_UnionFind struct{ par []int }

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var n, m int
	fmt.Fscan(r, &n, &m)
	uf := generated_alib_NewUnionFind(n)
	for i := 0; i < m; i++ {
		var t, a, b int
		fmt.Fscan(r, &t, &a, &b)
		if t == 0 {
			uf.Union(a, b)
		} else {
			if uf.Same(a, b) {
				fmt.Fprintln(w, 1)
			} else {
				fmt.Fprintln(w, 0)
			}
		}
	}
}
/* this code was generated by the code below
package main

import (
	"bufio"
	"fmt"
	"os"

	alib "a/lib"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n, m int
	fmt.Fscan(r, &n, &m)
	uf := alib.NewUnionFind(n)

	for i := 0; i < m; i++ {
		var t, a, b int
		fmt.Fscan(r, &t, &a, &b)

    if t == 0 {
      uf.Union(a, b)
    } else {
      if uf.Same(a, b) {
        fmt.Fprintln(w, 1)
      } else {
        fmt.Fprintln(w, 0)
      }
    }
	}
}

*/