Buggy python submission (verdict: Time Limit Exceeded):
N,M=list(map(int,input().split()))

path=[]
for i in range(M):
    a,b=list(map(int,input().split()))
    path.append([a-1,b-1])
    
class UnionFind():
    #負の値はルートで集合の個数
    #正の値は次の要素を返す
    def __init__(self,size):
        self.table = [-1 for _  in range(size)]
        self.member_num = [1 for _ in range(size)]

    #集合の代表を求める
    def find(self,x):
        while self.table[x] >= 0:
            #根に来た時,self.table[根のindex]は負の値なのでx = 根のindexで値が返される。
            x = self.table[x]
        return x

    #併合
    def union(self,x,y):
        s1 = self.find(x)#根のindex,table[s1]がグラフの高さ
        s2 = self.find(y)
        m1=self.member_num[s1]
        m2=self.member_num[s2]
        if s1 != s2:#根が異なる場合
            if self.table[s1] != self.table[s2]:#グラフの高さが異なる場合
                if self.table[s1] < self.table[s2]:
                    self.table[s2] = s1
                    self.member_num[s1]=m1+m2
                    return [m1,m2]
                else:
                    self.table[s1] = s2
                    self.member_num[s2]=m1+m2
                    return [m1,m2]
            else:
                #グラフの長さが同じ場合,どちらを根にしても変わらない
                #その際,グラフが1長くなることを考慮する
                self.table[s1] = -1
                self.table[s2] = s1
                self.member_num[s1] = m1+m2
                return [m1,m2]
        return [0,0]
    
uf=UnionFind(N)

path=path[::-1]
count = 0
ans = [N*(N-1)//2]
for i in range(M):
    a,b=path[i]
    m1,m2 = uf.union(a,b)
    count+=m1*m2
    ans.append(N*(N-1)//2 - count)

ans=ans[:-1]
ans=ans[::-1]

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

Fixed submission:

N,M=list(map(int,input().split()))

path=[]
for i in range(M):
    a,b=list(map(int,input().split()))
    path.append([a-1,b-1])
    
class UnionFind():
    #負の値はルートで集合の個数
    #正の値は次の要素を返す
    def __init__(self,size):
        self.table = [-1 for _  in range(size)]
        self.member_num = [1 for _ in range(size)]

    #集合の代表を求める
    def find(self,x):
        while self.table[x] >= 0:
            #根に来た時,self.table[根のindex]は負の値なのでx = 根のindexで値が返される。
            x = self.table[x]
        return x

    #併合
    def union(self,x,y):
        s1 = self.find(x)#根のindex,table[s1]がグラフの高さ
        s2 = self.find(y)
        m1=self.member_num[s1]
        m2=self.member_num[s2]
        if s1 != s2:#根が異なる場合
            if m1 != m2:#グラフの高さが異なる場合
                if m1 < m2:
                    self.table[s1] = s2
                    self.member_num[s2]=m1+m2
                    return [m1,m2]
                else:
                    self.table[s2] = s1
                    self.member_num[s1]=m1+m2
                    return [m1,m2]
            else:
                #グラフの長さが同じ場合,どちらを根にしても変わらない
                #その際,グラフが1長くなることを考慮する
                self.table[s1] = -1
                self.table[s2] = s1
                self.member_num[s1] = m1+m2
                return [m1,m2]
        return [0,0]
    
uf=UnionFind(N)

path=path[::-1]
count = 0
ans = [N*(N-1)//2]
for i in range(M):
    a,b=path[i]
    m1,m2 = uf.union(a,b)
    count+=m1*m2
    ans.append(N*(N-1)//2 - count)

ans=ans[:-1]
ans=ans[::-1]

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