Problem p03575 (python)

Fixed submission:
N,M = map(int,input().split())
es = []
for i in range(M):
    a,b = map(int,input().split())
    a,b = a-1, b-1
    es.append((a,b))
 
parent = None
def root(a):
    if parent[a] == a:
        return a
    else:
        parent[a] = root(parent[a])
        return parent[a]
def is_same(a,b):
    return root(a) == root(b)
def unite(a,b):
    ra = root(a)
    rb = root(b)
    if ra == rb: return
    parent[ra] = rb;
 
ans = 0
for i in range(M):
    parent = [k for k in range(N)]
    for j,e in enumerate(es):
        if j == i: continue
        a,b = e
        unite(a,b)
    a,b = es[i]
    if not is_same(a,b):
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
class UnionFind:

    def __init__(self, n):
        self.n = n
        self.parents = [i for i in range(n)]
        self.ranks = [0] * n
        self.count = n

    def union(self, x, y):
        self.link(x, y)

    def link(self, x, y):
        x_root = self.parents[x]
        y_root = self.parents[y]
        if x_root == y_root:
            return

        if self.ranks[x] > self.ranks[y]:
            self.parents[y] = xroot
        else:
            self.parents[x] = y_root
            if self.ranks[x] == self.ranks[y]:
                self.ranks[y] += 1
        self.count -= 1

    def find(self, x):
        if self.parents[x] != x:
            self.parents[x] = self.find(self.parents[x])
        return self.parents[x]

N, M = list(map(int, input().split()))

bridges = []
for i in range(M):
    a, b = list(map(int, input().split()))
    # bridges.append(a, b)
    bridges.append([a-1, b-1])


count = 0
for i in range(M):
    uf = UnionFind(N)
    for j in range(M):
        if j == i:
            continue
        else:
            x, y = bridges[j]
            uf.union(x, y)
    if uf.count > 1:
        count += 1
print(count)

Buggy submission (verdict: Runtime Error):
class UnionFind:

    def __init__(self, n):
        self.n = n
        self.parents = [i for i in range(n)]
        self.ranks = [0] * n
        self.count = n

    def union(self, x, y):
        self.link(x,y)

    def link(self, x, y):
        if self.parents[x] == self.parents[y]:
            return
        if self.ranks[x] < self.ranks[y]:
            self.parents[x] = y
        else:
            if self.ranks[y] == self.ranks[x]:
                self.ranks[y] += 1
            self.parents[y] = x
        # print('linked')
        self.count -= 1

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

N, M = list(map(int, input().split()))

bridges = []
for i in range(M):
    bridges.append(list(map(int, input().split())))


# bridges = bridges[::-1]
# result = []
# print('-----')
count = 0
for i in range(M):
    # print(i, 'th ------')
    uf = UnionFind(N)
    for j in range(M):
        x, y = bridges[j]
        if j == i:
            # print('  skip',x-1,y-1, 'count',uf.count, uf.parents)
            continue
        else:
            uf.union(x, y)
            # print('  union',x-1,y-1, 'count',uf.count, uf.parents)
    # print(i, uf.count)
    if uf.count > 1:
        count += 1


# for x, y in bridges:
#     uf.union(x-1, y-1)
#     result.append(uf.get_group_num())

# print(result[::-1])

print(count)

Buggy submission (verdict: Runtime Error):
class UnionFind:

    def __init__(self, n):
        self.n = n
        self.parents = [i for i in range(n)]
        self.ranks = [0] * n
        self.count = n

    def union(self, x, y):
        self.link(x,y)

    def link(self, x, y):
        if self.parents[x] == self.parents[y]:
            return
        if self.ranks[x] < self.ranks[y]:
            self.parents[x] = y
        else:
            if self.ranks[y] == self.ranks[x]:
                self.ranks[y] += 1
            self.parents[y] = x

        self.count -= 1

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

N, M = list(map(int, input().split()))

bridges = []
for i in range(M):
    bridges.append(list(map(int, input().split())))


# bridges = bridges[::-1]
# result = []
print('-----')
count = 0
for i in range(M):
    # print(i, 'th ------')
    uf = UnionFind(N)
    for j in range(M):
        x, y = bridges[j]
        if j == i:

            continue
        else:
            uf.union(x, y)


    if uf.count > 1:
        count += 1


# for x, y in bridges:
#     uf.union(x-1, y-1)
#     result.append(uf.get_group_num())

# print(result[::-1])

print(count)