Problem p02558 (python)

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 size(self, x):
        return -self.parents[self.find(x)]
 
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
 
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]
 
    def group_count(self):
        return len(self.roots())
 
    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}
 
      
n, q = list(map(lambda x: int(x), input().split()))
uf = UnionFind(n)
for _ in range(q):
    t, u, v = list(map(lambda x: int(x), input().split()))
#     print(uf.parent_indexes)
    if t == 0:
        uf.union(u, v)
        continue
    print('1') if uf.same(u, v) else print('0')

Buggy submission (verdict: Runtime Error):
class UnionFind:
    def __init__(self, length):
        self.parent_indexes = [-1]* length
        
    def union(self, i, j):
        if self.are_in_the_same_group(i, j):
            return
        # iが所属する集団のサイズ>=jが所属する集団のサイズにする
        if self.get_group_size(i) < self.get_group_size(j):
            i, j = j, i
                parent_of_i = self.find(i)
        self.parent_indexes[self.find(i)] = - self.get_group_size(i) - self.get_group_size(j)
        self.parent_indexes[self.find(j)] = self.find(i)
        
    # i番目の要素が所属する集団の親nodeのindexを返す
    # メモ化もする
    def find(self, i):
        if self.parent_indexes[i] < 0:
            return i
        parent_index = self.find(self.parent_indexes[i])
        self.parent_indexes[i] = parent_index
        return parent_index

    def get_group_size(self, i):
        return - self.parent_indexes[self.find(i)]
    
    def are_in_the_same_group(self, i, j):
        return self.find(i) == self.find(j)
    
    
n, q = list(map(lambda x: int(x), input().split()))
uf = UnionFind(n)
for _ in range(q):
    t, u, v = list(map(lambda x: int(x), input().split()))
#     print(uf.parent_indexes)
    if t == 0:
        uf.union(u, v)
        continue
    print('1') if uf.are_in_the_same_group(u, v) else print('0')

Buggy submission (verdict: Runtime Error):

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 size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

      
n, q = list(map(lambda x: int(x), input().split()))
uf = UnionFind(n)
for _ in range(q):
    t, u, v = list(map(lambda x: int(x), input().split()))
#     print(uf.parent_indexes)
    if t == 0:
        uf.union(u, v)
        continue
    print('1') if uf.are_in_the_same_group(u, v) else print('0')

Buggy submission (verdict: Runtime Error):
class UnionFind:
    def __init__(self, length):
        self.parent_indexes = [-1]* length
        
    def union(self, i, j):
        if i == j:
            return
        # iが所属する集団のサイズ>=jが所属する集団のサイズにする
        if self.get_group_size(i) < self.get_group_size(j):
            i, j = j, i
        self.parent_indexes[i] = - self.get_group_size(i) - self.get_group_size(j)
        self.parent_indexes[j] = i
        
    # i番目の要素が所属する集団の親nodeのindexを返す
    # メモ化もする
    def find(self, i):
        if self.parent_indexes[i] < 0:
            return i
#         parent_index = self.find(i)
#         parent_index = self.find(self.parent_indexes[i])
        parent_index = self.find(self.parent_indexes[i])
        self.parent_indexes[i] = parent_index
        return parent_index

    def get_group_size(self, i):
        return - self.parent_indexes[self.find(i)]
    
    def are_in_the_same_group(self, i, j):
        return self.find(i) == self.find(j)
    
    
n, q = list(map(lambda x: int(x), input().split()))
uf = UnionFind(n)
for _ in range(q):
    t, u, v = list(map(lambda x: int(x), input().split()))
    print(uf.parent_indexes)
    if t == 0:
        uf.union(u, v)
        continue
    print('1') if uf.are_in_the_same_group(u, v) else print('0')

Buggy submission (verdict: Runtime Error):
class UnionFind:
    def __init__(self, length):
        self.parent_indexes = [-1]* length
        
    def union(self, i, j):
        # iが所属する集団のサイズ>=jが所属する集団のサイズにする
        if self.get_group_size(i) < self.get_group_size(j):
            i, j = j, i
        self.parent_indexes[i] = - self.get_group_size(i) - self.get_group_size(j)
        self.parent_indexes[j] = i
        
    # i番目の要素が所属する集団の親nodeのindexを返す
    # メモ化もする
    def find(self, i):
        if self.parent_indexes[i] < 0:
            return i
#         parent_index = self.find(i)
#         parent_index = self.find(self.parent_indexes[i])
        parent_index = self.find(self.parent_indexes[i])
        self.parent_indexes[i] = parent_index
        return parent_index

    def get_group_size(self, i):
        return - self.parent_indexes[self.find(i)]
    
    def are_in_the_same_group(self, i, j):
        return self.find(i) == self.find(j)
    
    
n, q = list(map(lambda x: int(x), input().split()))
uf = UnionFind(n)
for _ in range(q):
    t, u, v = list(map(lambda x: int(x), input().split()))
    if t == 0:
        uf.union(u, v)
        continue
    print('1') if uf.are_in_the_same_group(u, v) else print('0')

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

links = defaultdict(set)
n, q = list(map(lambda x: int(x), input().split()))
for _ in range(n):
    t, u, v = list(map(lambda x: int(x), input().split()))
    if t == 0:
        links[min(u, v)].add(max(u, v))
        links[v].add(u)
        continue
    print('1') if max(u, v) in links[min(u, v)] else print('0')