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 Ruby (2.7.1) (judge verdict: Accepted)

def get_i()
  return gets.chomp.split(" ").map(&:to_i)
end
#input of float(split by space)
def get_f()
  return gets.chomp.split(" ").map(&:to_f)
end
#input of string(split by space)
def get()
  return gets.chomp.split(" ")
end
#input of string(split per one character)
def get_nsp()
  return gets.chomp.split("")
end
#yes or no decision
def yn_judge(bool,y="Yes",n="No")
  return bool ? y : n 
end
#create of array
def array(size1,init=nil,size2=-1)
  if size2==-1
      return Array.new(size1){init}
  else
      return Array.new(size2){Array.new(size1){init}}
  end
end

def max(a,b)
    return a>b ? a : b
end

def min(a,b)
    return a>b ? b : a
end

INF=Float::INFINITY


#====UnionFindTree====
class UnionFindTree
    attr_accessor:parent,:size,:rank,:group
    def initialize(n)
        @parent=Array.new(n){|idx| idx}
        @rank=Array.new(n,0)
        @size=Array.new(n,1)
    end

    def find(x)
        return x if @parent[x]==x
        @parent[x]=find(@parent[x])
        return @parent[x]
    end

    def union(x,y)
        par_x=find(x)
        par_y=find(y)
        return if par_x==par_y
        if @rank[par_x]<@rank[par_y]
            @parent[par_x]=par_y
            @size[par_y]+=@size[par_x]
        else
            @parent[par_y]=par_x
            @size[par_x]+=@size[par_y]
            @rank[par_x]+=1 if @rank[par_x]==@rank[par_y]
        end
    end

    def same?(x,y)
        return find(x)==find(y)
    end

    def get_size(x)
        return @size[find(x)]
    end
    def make_groups()
      n=@parent.size
      @group=Array.new(n,[])
      @parent.each_with_index do|child,i|
        @group[i]<< child 
      end
    end
end
N,Q=get_i
uft=UnionFindTree.new(N)
Q.times do|i|
  t,u,v=get_i
  if t.zero?
      uft.union(u,v)
  else
      puts yn_judge(uft.same?(u,v),1,0)
  end
end