Buggy python submission (verdict: Time Limit Exceeded):
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

    def is_same(self, x, y):
        return self.root(x) == self.root(y)

    def members(self, x):
        root = self.root(x)
        return [i for i in range(self.n) if self.root(i) == root]

    def all_roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.all_roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.all_roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.all_roots())

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, m = map(int, input().split())

uf = UnionFind(n)
ab = [tuple(map(int,input().split())) for i in range(m)]

es = [[] for i in range(n)]
ans = [0]*m

sumab = (n * (n-1))//2
ans[-1] = sumab
visited = [0]*n

for i, (a,b) in enumerate(reversed(ab)):
    a,b = a-1,b-1

    uf.union(a,b)
    all_roots = list(uf.all_roots())

    for j in range(len(all_roots)-1):
        j_mem = len(uf.members(all_roots[j]))
        for k in range(j+1,len(all_roots)):
            k_mem = len(uf.members(all_roots[k]))
            ans[-i-2] += j_mem*k_mem


print(*ans, sep=' ')


Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

    def is_same(self, x, y):
        return self.root(x) == self.root(y)

    def members(self, x):
        root = self.root(x)
        return [i for i in range(self.n) if self.root(i) == root]

    def all_roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.all_roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.all_roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.all_roots())

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, m = map(int, input().split())

uf = UnionFind(n)
ab = [tuple(map(int,input().split())) for i in range(m)]

ans = [0]*m
ans[-1] = (n * (n-1))//2

for i, (a,b) in enumerate(reversed(ab)):
    a,b = a-1,b-1

    if -i-2 == -m-1:
        break

    if uf.is_same(a,b):
        ans[-i-2] = ans[-i-1]

    else:
        ans[-i-2] = ans[-i-1] - uf.size(a)*uf.size(b)
        uf.union(a,b)

print(*ans, sep='\n')