Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
list_p=[]
for i in range(0,m):
    a,b=map(int,input().split())
    list_p.append([a,b])
list_p.reverse()
group=[]
count=[]
group.append(list_p[0])
count.append(0)
for i in range(1,m):
    number=0
    index=0
    ifa=0
    ifb=0
    for j in group:
        number=number+len(j)*(len(j)-1)//2
        if list_p[i][0] in j:
            ifa=1
            placea=index
        if list_p[i][1] in j:
            ifb=1
            placeb=index
        index=index+1
    count.append(number)
    if ifa==0 and ifb==0:
        group.append(list_p[i])
    elif ifa==1 and ifb==0:
        group[placea].append(list_p[i][1])
    elif ifa==0 and ifb==1:
        group[placeb].append(list_p[i][0])
    elif ifa==1 and ifb==1:
        if placea==placeb:
            pass
        else:
            group[placea].extend(group[placeb])
            group.pop(placeb)
x=n*(n-1)//2
count.reverse()
for i in count:
    print(x-i)


    


Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size=[1]*(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 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]+self.size[y]
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.size[x]=self.size[x]+self.size[y]

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

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

n,m=map(int,input().split())
a_list=[]
b_list=[]
for i in range(m):
    a,b=map(int,input().split())
    a_list.append(a)
    b_list.append(b)
ans=[n*(n-1)//2]
U=UnionFind(n)
for i in range(m-1,0,-1):
    if U.same_check(a_list[i],b_list[i]):
        ans.append(ans[-1])
    else:
        ans.append(ans[-1]-U.Size(a_list[i])*U.Size(b_list[i]))
        U.union(a_list[i],b_list[i])
for i in range(m-1,-1,-1):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
list_p=[]
for i in range(0,m):
    a,b=map(int,input().split())
    list_p.append([a,b])
list_p.reverse()
group=[]
count=[]
group.append(list_p[0])
count.append(0)
for i in range(1,m):
    number=0
    for j in group:
        number=number+len(j)*(len(j)-1)//2
    count.append(number)
    index=0
    ifa=0
    ifb=0
    for j in group:
        if list_p[i][0] in j:
            ifa=1
            placea=index
        if list_p[i][1] in j:
            ifb=1
            placeb=index
        index=index+1
    if ifa==0 and ifb==0:
        group.append(list_p[i])
    elif ifa==1 and ifb==0:
        group[placea].append(list_p[i][1])
    elif ifa==0 and ifb==1:
        group[placeb].append(list_p[i][0])
    elif ifa==1 and ifb==1:
        if placea==placeb:
            pass
        else:
            group[placea].extend(group[placeb])
            group.pop(placeb)
x=n*(n-1)//2
count.reverse()
for i in count:
    print(x-i)


    


Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size=[1]*(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 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]+self.size[y]
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.size[x]=self.size[x]+self.size[y]

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

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

n,m=map(int,input().split())
a_list=[]
b_list=[]
for i in range(m):
    a,b=map(int,input().split())
    a_list.append(a)
    b_list.append(b)
ans=[n*(n-1)//2]
U=UnionFind(n)
for i in range(m-1,0,-1):
    if U.same_check(a_list[i],b_list[i]):
        ans.append(ans[-1])
    else:
        ans.append(ans[-1]-U.Size(a_list[i])*U.Size(b_list[i]))
        U.union(a_list[i],b_list[i])
for i in range(m-1,-1,-1):
    print(ans[i])