Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
N,Q = map(int,input().split())
rank = [0 for i in range(N)]

class Tree:
    def __init__(self,par,key):
        self.par = par
        self.key = key

List = [Tree(i,i) for i in range(N)]
for i in range(Q):
    com, x,y = map(int,input().split())
    while type(x) != int or List[x].par != x:
        x = List[x].par.key
    a = rank[x]
            
    while type(y) != int or List[y].par != y:
        y = List[y].par.key
    b = rank[y]

    if com == 0:
        if a <= b:
            if a == b:
                rank[x] += 1
            List[y].par = List[x]
            
        else:
            List[x].par = List[y]

    else:
        if x == y:
            print("1")
        else:
            print("0")




Fixed submission:
#coding:utf-8
import time

N,Q = map(int,input().split())
rank = [0 for i in range(N)]

class Tree:
    def __init__(self,par,key):
        self.par = par
        self.key = key

List = [Tree(i,i) for i in range(N)]

def unit(x,y):
    while List[x].par != x:
        x = List[x].par
    a = rank[x]
            
    while List[y].par != y:
        y = List[y].par
    b = rank[y]
    if a <= b:
        if a == b:
            rank[x] += 1
        List[y].par = x
    else:
        List[x].par = y



def Find(x,y):
    Q = []
    P = []
    while List[x].par != x:
        x = List[x].par
        Q.append(x)
            
    while List[y].par != y:
        y = List[y].par
        P.append(y)
    for xi in Q:
        List[xi].par = x
        rank[xi] = 1
    for yi in P:
        List[yi].par = y
        rank[yi] = 1
    if x == y:
        print(1)
    else:
        print(0)
    
        
for i in range(Q):
    com, x,y = map(int,input().split())

    if com == 0:
        unit(x,y)
            
    else:
        Find(x,y)

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
import time

N,Q = map(int,input().split())
rank = [0 for i in range(N)]

class Tree:
    def __init__(self,par,key):
        self.par = par
        self.key = key

List = [Tree(i,i) for i in range(N)]
for i in range(Q):
    com, x,y = map(int,input().split())
    x1,y1 = x,y
    while List[x1].par != x1:
        x1 = List[x1].par
    a = rank[x1]
            
    while List[y1].par != y1:
        y1 = List[y1].par
    b = rank[y1]

    if com == 0:
        if a <= b:
            if a == b:
                rank[x1] += 1
            List[y1].par = x1
            
        else:
            List[x1].par = y1

    else:
        if x1 == y1:
            print("1")
        else:
            print("0")




Fixed submission:
#coding:utf-8
import time

N,Q = map(int,input().split())
rank = [0 for i in range(N)]

class Tree:
    def __init__(self,par,key):
        self.par = par
        self.key = key

List = [Tree(i,i) for i in range(N)]

def unit(x,y):
    while List[x].par != x:
        x = List[x].par
    a = rank[x]
            
    while List[y].par != y:
        y = List[y].par
    b = rank[y]
    if a <= b:
        if a == b:
            rank[x] += 1
        List[y].par = x
    else:
        List[x].par = y



def Find(x,y):
    Q = []
    P = []
    while List[x].par != x:
        x = List[x].par
        Q.append(x)
            
    while List[y].par != y:
        y = List[y].par
        P.append(y)
    for xi in Q:
        List[xi].par = x
        rank[xi] = 1
    for yi in P:
        List[yi].par = y
        rank[yi] = 1
    if x == y:
        print(1)
    else:
        print(0)
    
        
for i in range(Q):
    com, x,y = map(int,input().split())

    if com == 0:
        unit(x,y)
            
    else:
        Find(x,y)