Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
all_pairs = N * (N - 1) // 2
island = [set([i]) for i in range(1,N+1)]
A=[0 for i in range(M)]
B=[0 for i in range(M)]
ans=[0 for i in range(M)]
for i in range(M):
    A[i],B[i]=map(int,input().split())

for i in range(M-1, -1, -1):
    a,b=A[i],B[i]
    a2 = next(filter(lambda e:a in e, island), None)
    b2 = next(filter(lambda e:b in e, island), None)
    if a2 != b2:
        a2 |= b2
        b2.clear()
    #print(island)
    ans[i] = all_pairs
    for j in range(N):
        l = len(island[j])
        if l != 0:
            ans[i] -= l * (l - 1) // 2

for i in range(1,M):
    print(ans[i])
print(all_pairs)


Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.n = n
        # 最初はすべての根が自分自身である
        self.parent = [-1 for i in range(n)]

    def root(self, x):
        if self.parent[x] < 0:
            return x
        self.parent[x] = self.root(self.parent[x])
        return self.parent[x]

    def size(self, x):
        return -self.parent[self.root(x)]

    def connect(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.size(x) < self.size(y):
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x

N, M = map(int,input().split())
AB = []
ans = [0 for i in range(M+1)]
for i in range(M):
    AB.append(list(map(int,input().split())))

uni = UnionFind(N)
ans[M] = N * (N - 1) // 2
for i in range(M-1, 0, -1):
    a,b = AB[i]
    a-=1
    b-=1
    if uni.root(a) == uni.root(b):
        ans[i] = ans[i+1]
    else:
        ans[i] = ans[i+1] - uni.size(a) * uni.size(b)
        uni.connect(a,b)

for i in range(1,M+1):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
all_pairs = N * (N - 1) // 2
island = [set([i]) for i in range(1,N+1)]
A=[0 for i in range(M)]
B=[0 for i in range(M)]
ans=[0 for i in range(M)]
for i in range(M):
    A[i],B[i]=map(int,input().split())

for i in range(M-1, -1, -1):
    a,b=A[i],B[i]
    a2 = next(filter(lambda e:a in e, island), None)
    b2 = next(filter(lambda e:b in e, island), None)
    if a2 != b2:
        a2 |= b2
        b2.clear()
    #print(island)
    ans[i] = all_pairs
    for j in range(N):
        l = len(island[j])
        if l != 0:
            ans[i] -= l * (l - 1) // 2

for i in range(1,M):
    print(ans[i])
print(all_pairs)


Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.n = n
        # 最初はすべての根が自分自身である
        self.parent = [-1 for i in range(n)]

    def root(self, x):
        if self.parent[x] < 0:
            return x
        self.parent[x] = self.root(self.parent[x])
        return self.parent[x]

    def size(self, x):
        return -self.parent[self.root(x)]

    def connect(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.size(x) < self.size(y):
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x

N, M = map(int,input().split())
AB = []
ans = [0 for i in range(M+1)]
for i in range(M):
    AB.append(list(map(int,input().split())))

uni = UnionFind(N)
ans[M] = N * (N - 1) // 2
for i in range(M-1, 0, -1):
    a,b = AB[i]
    a-=1
    b-=1
    if uni.root(a) == uni.root(b):
        ans[i] = ans[i+1]
    else:
        ans[i] = ans[i+1] - uni.size(a) * uni.size(b)
        uni.connect(a,b)

for i in range(1,M+1):
    print(ans[i])