Problem p03108 (python)

Fixed submission:
n, m = [int(item) for item in input().split()]
ab = [[int(item) for item in input().split()] for _ in range(m)]
ab.reverse()

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0] * n
        self.size = [1] * n

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        return self.size[self.find(x)]

inconv = n*(n-1) // 2 
UF = UnionFind(n)

inconv_list = []
inconv_list.append(inconv)
for a, b in ab:
    if not UF.same_check(a-1, b-1):
        inconv -= UF.get_size(a-1) * UF.get_size(b-1)
        UF.union(a-1, b-1)
    inconv_list.append(inconv)
for item in inconv_list[::-1][1:]:
    print(item)

Buggy submission (verdict: Runtime Error):
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

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

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

    def same_count(self, x):
        return self.par.count(self.find(x))

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

        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def print_par(self):
        print(self.par)

txt = open("a.txt", "r")
n, m = [int(item) for item in input().split()]
e = [[int(item) for item in input().split()] for i in range(m)]
e = e[::-1]

UF = UnionFind(n)
unconvinience = n * (n-1) / 2
uc_list = [int(unconvinience)]

for item in e:
    if UF.same_check(item[0], item[1]):
        pass
    else:
        unconvinience -= (UF.same_count(item[0]) * UF.same_count(item[1]))

    uc_list.append(int(unconvinience))
    UF.union(item[0], item[1])

for i in range(m):
    print(uc_list[::-1][i+1])

Buggy submission (verdict: Runtime Error):
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

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

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

    def same_count(self, x):
        return self.par.count(self.find(x))

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

        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def print_par(self):
        print(self.par)

txt = open("a.txt", "r")
n, m = [int(item) for item in txt.readline().split()]
e = [[int(item) for item in txt.readline().split()] for i in range(m)]
e = e[::-1]

UF = UnionFind(n)
unconvinience = n * (n-1) / 2
uc_list = [int(unconvinience)]

for item in e:
    if UF.same_check(item[0], item[1]):
        pass
    else:
        unconvinience -= (UF.same_count(item[0]) * UF.same_count(item[1]))

    uc_list.append(int(unconvinience))
    UF.union(item[0], item[1])

print(uc_list)
for i in range(m):
    print(uc_list[::-1][i+1])