Problem p03108 (python)

Fixed submission:
import sys
readline = sys.stdin.readline
N, M = map(int, input().split())
ab = [tuple(map(lambda x: int(x) - 1, readline().split())) for _ in range(M)]
class UnionFind:
    def __init__(self, size):
        self.rank = [-1 for _ in range(size)]
        self.number = [1 for _ in range(size)]
    def find(self, x):
        while self.rank[x] >= 0:
            x = self.rank[x]
        return x
    def union(self, s1, s2):
        if s1 == s2:
            return
        if self.rank[s1] == self.rank[s2]:
            self.rank[s1] -= 1
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        elif self.rank[s1] < self.rank[s2]:
            self.rank[s2] = s1
            self.number[s1] += self.number[s2]
        else:
            self.rank[s1] = s2
            self.number[s2] += self.number[s1]
uf = UnionFind(N)
p = N * (N - 1) // 2
ans = [p]
for x, y in reversed(ab[1:]):
    px = uf.find(x)
    py = uf.find(y)
    if px != py:
        p -= uf.number[px] * uf.number[py]
        uf.union(px, py)
    ans.append(p)
print(*ans[::-1], sep="\n")

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline
n, m = map(int, input().split())
a = []
b = []
for _ in range(m):
    aa, bb = list(map(int, readline().split()))
    a.append(aa - 1)
    b.append(bb - 1)
a = a[::-1]
b = b[::-1]
ans = int(n * (n - 1) / 2)
ans_rec = [0] * m
ans_rec[-1] = ans
p = [-1 for i in range(n)]
size = [1 for i in range(n)]
def find(x):
    px = p[x]
    if px < 0:
        return x
    else:
        pxx = find(px)
        p[x] = pxx
        return pxx
for k in range(m - 1):
    pa = find(a[k])
    pb = find(b[k])
    if pa == pb:
        pass
    else:
        ans -= size[pa] * size[pb]
        size[pa] += size[pb]
        p[pb] = pa
    ans_rec[- k - 2] = ans
print(*ans_rec, sep="\n")

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline
n, m = map(int, input().split())
a = []
b = []
for _ in range(m):
    aa, bb = list(map(int, readline().split()))
    a.append(aa - 1)
    b.append(bb - 1)
a = a[::-1]
b = b[::-1]
ans = int(n * (n - 1) / 2)
ans_rec = [0] * m
ans_rec[-1] = ans
p = [-1] * n
size = [1] * n
def find(x):
    px = p[x]
    if px < 0:
        return x
    else:
        pxx = find(px)
        p[x] = pxx
        return pxx
for k in range(m - 1):
    pa = find(a[k])
    pb = find(b[k])
    if pa == pb:
        pass
    else:
        ans -= size[pa] * size[pb]
        size[pa] += size[pb]
        p[pb] = pa
    ans_rec[- k - 2] = ans
for aa in ans_rec:
    print(aa)

Buggy submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline
n, m = map(int, input().split())
a = []
b = []
for _ in range(m):
    aa, bb = list(map(int, readline().split()))
    a.append(aa - 1)
    b.append(bb - 1)
a = a[::-1]
b = b[::-1]
ans = int(n * (n - 1) / 2)
ans_rec = [0] * m
ans_rec[-1] = ans
p = [-1] * n
size = [1] * n
def find(x):
    px = p[x]
    if px < 0:
        return x
    else:
        pxx = find(px)
        p[x] = pxx
        return pxx
for k in range(m - 1):
    pa = find(a[k])
    pb = find(b[k])
    if pa == pb:
        pass
    else:
        ans -= size[pa] * size[pb]
        size[pa] += size[pb]
        p[pb] = pa
    ans_rec[- k - 2] = ans
print(*ans_rec, sep="\n")