Buggy python submission (verdict: Runtime Error):
class UnionFind():
    n=1
    rnk=[0]
    par=[0]
    sze=[1]
    def __init__(self,N):
        self.n=N
        self.rnk=[0 for i in range(self.n)]
        self.par=[i for i in range(self.n)]
        self.sze=[1 for i in range(self.n)]
    def find(self,x):
        if x==self.par[x]:
            return x
        else:
            self.par[x]=self.find(self.par[x])
            return self.par[x]
    def unite(self,x,y):
        x=self.find(x)
        y=self.find(y)
        self.par[x]=y
        self.rnk[x]=1
        self.sze[y]+=self.sze[x]
        self.sze[x]=0
    def same(self,x,y):
        return self.find(x)==self.find(y)
    def size(self,x):
        return self.sze[self.find(x)]
N,M=map(int,input().split())
G=UnionFind(N)
A=[0 for i in range(M)]
B=[0 for i in range(M)]
ans=[0 for i in range(M+1)]
for i in range(M):
    A[i],B[i]=map(int,input().split())
    A[i]-=1;B[i]-=1
ans[M]=(N*(N-1))//2
for i in range(M-1,-1,-1):
    if G.same(A[i],B[i]):
        ans[i]=ans[i+1]
    else:
        ans[i]=ans[i+1]-G.size(A[i])*G.size(B[i])
    G.unite(A[i],B[i])
for i in range(1,M+1):
    print(ans[i])

Fixed submission:
class UnionFind():
    n=1
    par=[0]
    rnk=[0]
    def __init__(self,size):
        self.n=size
        self.par=[i for i in range(self.n)]
        self.rnk=[0 for i in range(self.n)]
    def find(self,vertex1):
        if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
    def unite(self,vertex1,vertex2):
        vertex1=self.find(vertex1)
        vertex2=self.find(vertex2)
        if vertex1==vertex2:
            return
        if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
    def same(self,vetrex1,vertex2):
        return self.find(vetrex1)==self.find(vertex2)

N,M=map(int,input().split())
A=[0 for i in range(M)]
B=[0 for i in range(M)]
for i in range(M):
    A[i],B[i]=map(int,input().split())
    A[i]-=1;B[i]-=1
G=UnionFind(N)
D=dict()
for i in range(N):
    D[i]=1
ans=[(N*(N-1))//2]
for i in range(M)[::-1]:
    #print([G.find(i) for i in range(N)],A[i],B[i],D,ans[-1])
    a=G.find(A[i])
    b=G.find(B[i])
    G.unite(A[i],B[i])
    x=G.find(A[i])
    if a!=b:
        ans.append(ans[-1]-D[a]*D[b])
        if x==a:
            D[a],D[b]=D[a]+D[b],0
        else:
            D[a],D[b]=0,D[a]+D[b]
    else:
        ans.append(ans[-1])
for line in ans[:-1][::-1]:
    print(line)

Buggy python submission (verdict: Time Limit Exceeded):
class UnionFind():
    n=1
    par=[0]
    rnk=[0]
    def __init__(self,size):
        self.n=size
        self.par=[i for i in range(self.n)]
        self.rnk=[0 for i in range(self.n)]
    def find(self,vertex1):
        if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
    def unite(self,vertex1,vertex2):
        vertex1=self.find(vertex1)
        vertex2=self.find(vertex2)
        if vertex1==vertex2:
            return
        if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
    def same(self,vetrex1,vertex2):
        return self.find(vetrex1)==self.find(vertex2)

N,M=map(int,input().split())
A=[0 for i in range(M)]
B=[0 for i in range(M)]
for i in range(M):
    A[i],B[i]=map(int,input().split())
    A[i]-=1;B[i]-=1
G=UnionFind(N)
D=dict()
for i in range(N):
    D[i]=1
ans=[(N*(N-1))//2]
for i in range(M)[::-1]:
    #この行があるとTLEになるはず
    tmp=[G.find(i) for i in range(N)]
    #この上の行のせいでO(NM)になって通らない
    a=G.find(A[i])
    b=G.find(B[i])
    G.unite(A[i],B[i])
    x=G.find(A[i])
    if a!=b:
        ans.append(ans[-1]-D[a]*D[b])
        if x==a:
            D[a],D[b]=D[a]+D[b],0
        else:
            D[a],D[b]=0,D[a]+D[b]
    else:
        ans.append(ans[-1])
for line in ans[:-1][::-1]:
    print(line)


Fixed submission:
class UnionFind():
    n=1
    par=[0]
    rnk=[0]
    def __init__(self,size):
        self.n=size
        self.par=[i for i in range(self.n)]
        self.rnk=[0 for i in range(self.n)]
    def find(self,vertex1):
        if self.par[vertex1]==vertex1:
            return vertex1
        else:
            self.par[vertex1]=self.find(self.par[vertex1])
            return self.par[vertex1]
    def unite(self,vertex1,vertex2):
        vertex1=self.find(vertex1)
        vertex2=self.find(vertex2)
        if vertex1==vertex2:
            return
        if (self.rnk[vertex1]<self.rnk[vertex2]):
            self.par[vertex1]=vertex2
        else:
            self.par[vertex2]=vertex1
            if(self.rnk[vertex1]==self.rnk[vertex2]):
                self.rnk[vertex1]+=1
    def same(self,vetrex1,vertex2):
        return self.find(vetrex1)==self.find(vertex2)

N,M=map(int,input().split())
A=[0 for i in range(M)]
B=[0 for i in range(M)]
for i in range(M):
    A[i],B[i]=map(int,input().split())
    A[i]-=1;B[i]-=1
G=UnionFind(N)
D=dict()
for i in range(N):
    D[i]=1
ans=[(N*(N-1))//2]
for i in range(M)[::-1]:
    #print([G.find(i) for i in range(N)],A[i],B[i],D,ans[-1])
    a=G.find(A[i])
    b=G.find(B[i])
    G.unite(A[i],B[i])
    x=G.find(A[i])
    if a!=b:
        ans.append(ans[-1]-D[a]*D[b])
        if x==a:
            D[a],D[b]=D[a]+D[b],0
        else:
            D[a],D[b]=0,D[a]+D[b]
    else:
        ans.append(ans[-1])
for line in ans[:-1][::-1]:
    print(line)