Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
nei = {}
for _ in range(q):
  t, u, v = map(int, input().split())
  if t == 0:
    if u not in nei:
      nei[u] = set()
    if v not in nei:
      nei[v] = set()
    nei[u].add(v)
    nei[v].add(u)
  else:
    flag = False
    stack = set()
    visited = set()
    stack.add(u)
    while stack:
      node = stack.pop()
      if node in visited:
        continue
      if node == v:
        flag = True
        break
      visited.add(node)
      if not nei.get(node, None):
        continue
      for next_node in nei[node]:
        stack.add(next_node)
    if flag:
      print(1)
    else:
      print(0)


Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)
n, q = map(int, input().split())
un = UnionFind(n)
for _ in range(q):
  t, u, v = map(int, input().split())
  if t == 0:
    un.union(u, v)
  else:
    print(1 if un.same(u, v) else 0)

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
nei = {}
for i in range(n):
  nei[i] = set()
for _ in range(q):
  t, u, v = map(int, input().split())
  if t == 0:
    nei[u].add(v)
    nei[v].add(u)
  else:
    stack = [u]
    visited = set()
    flag = False
    while stack:
      node = stack.pop()
      if node in visited:
        continue
      if node == v:
        flag = True
        break
      visited.add(node)
      for next_node in nei[node]:
        stack.append(next_node)
    if flag:
      print(1)
    else:
      print(0)


Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)
n, q = map(int, input().split())
un = UnionFind(n)
for _ in range(q):
  t, u, v = map(int, input().split())
  if t == 0:
    un.union(u, v)
  else:
    print(1 if un.same(u, v) else 0)

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
nei = {}
for i in range(n):
  nei[i] = set()
for _ in range(q):
  t, u, v = map(int, input().split())
  if t == 0:
    nei[u].add(v)
    nei[v].add(u)
  else:
    stack = [u]
    visited = set()
    flag = False
    while stack:
      node = stack.pop()
      if node in visited:
        continue
      if node == v:
        flag = True
        break
      visited.add(node)
      for next_node in nei[node]:
        stack.append(next_node)
    if flag:
      print(1)
    else:
      print(0)


Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)
n, q = map(int, input().split())
un = UnionFind(n)
for _ in range(q):
  t, u, v = map(int, input().split())
  if t == 0:
    un.union(u, v)
  else:
    print(1 if un.same(u, v) else 0)