Problem p03108 (Python)

Buggy submission (status: Runtime Error):
N, M = [int(_) for _ in input().split()]
bridge = [[int(_) for _ in input().split()] for _ in range(M)]

bridge.reverse()
bridge = bridge[:-1]

inconvenience = N * (N - 1) // 2
inconvenience_history = [inconvenience]


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

    def root(self, i):
        if self.par[i] == i:
            return i
        self.par[i] = self.root(self.par[i])
        return self.par[i]

    def tree_rank(self, i):
        return self.rank[self.root(i)]

    def number(self, i):
        return self._number[self.root(i)]

    def union(self, i, j):
        _root_i = self.root(i)
        _root_j = self.root(j)
        if _root_i == _root_j:
            return
        else:
            if self.tree_rank(i) < self.tree_rank(j):
                self.par[_root_i] = _root_j
                self._number[_root_j] += self._number[_root_i]
            else:
                self.par[_root_j] = _root_i
                self._number[_root_i] += self._number[_root_j]
                if self.tree_rank(i) == self.tree_rank(j):
                    self.rank[_root_i] += 1


islands = UnionFind(N)

for A_i, B_i in bridge:
    islands.pri()

    root_A = islands.root(A_i)
    root_B = islands.root(B_i)

    if root_A == root_B:
        inconvenience_history.append(inconvenience)
        continue
    else:
        improvement = islands.number(A_i) * islands.number(B_i)
        islands.union(A_i, B_i)

        inconvenience -= improvement
        if inconvenience == 0:
            inconvenience_history += [0] * (M - len(inconvenience_history))
            break
        inconvenience_history.append(inconvenience)

print(*inconvenience_history[::-1], sep="\n")


Fixed submission:
N, M = [int(_) for _ in input().split()]
bridge = [[int(_) for _ in input().split()] for _ in range(M)]

bridge.reverse()
bridge = bridge[:-1]

inconvenience = N * (N - 1) // 2
inconvenience_history = [inconvenience]


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

    def root(self, i):
        if self.par[i] == i:
            return i
        self.par[i] = self.root(self.par[i])
        return self.par[i]

    def tree_rank(self, i):
        return self.rank[self.root(i)]

    def number(self, i):
        return self._number[self.root(i)]

    def union(self, i, j):
        _root_i = self.root(i)
        _root_j = self.root(j)
        if _root_i == _root_j:
            return
        else:
            if self.tree_rank(i) < self.tree_rank(j):
                self.par[_root_i] = _root_j
                self._number[_root_j] += self._number[_root_i]
            else:
                self.par[_root_j] = _root_i
                self._number[_root_i] += self._number[_root_j]
                if self.tree_rank(i) == self.tree_rank(j):
                    self.rank[_root_i] += 1


islands = UnionFind(N)

for A_i, B_i in bridge:
    root_A = islands.root(A_i)
    root_B = islands.root(B_i)

    if root_A == root_B:
        inconvenience_history.append(inconvenience)
        continue
    else:
        improvement = islands.number(A_i) * islands.number(B_i)
        islands.union(A_i, B_i)

        inconvenience -= improvement
        if inconvenience == 0:
            inconvenience_history += [0] * (M - len(inconvenience_history))
            break
        inconvenience_history.append(inconvenience)

print(*inconvenience_history[::-1], sep="\n")