Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
tree=[[] for i in range(n+1)]
parent=[0]*(n+1)
count=[0]*m
for i in range(1,m+1):
    a,b=ab[-i]
    A=parent[a]
    B=parent[b]
    if A==0 and B==0:
        parent[a]=a
        parent[b]=a
        tree[a].append(b)
        count[-i]=1
    elif A==0:
        count[-i]=len(tree[B])+1
        parent[a]=B
        tree[B].append(a)
    elif B==0:
        count[-i]=len(tree[A])+1
        parent[b]=A
        tree[A].append(b)
    elif parent[a]==parent[b]:
        count[-i]=0
    else:
        A=parent[a]
        B=parent[b]
        u=len(tree[A])
        v=len(tree[B])
        count[-i]=(u+1)*(v+1)   
        for u in tree[B]:
            parent[u]=A
        parent[B]=A
        tree[A].append(B)
        tree[A]+=tree[B]
        tree[B]=[]
sum=0
for i in range(0,m):
    sum+=count[i]
    print(sum)

Fixed submission:
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
tree=[[] for i in range(n+1)]
parent=[0]*(n+1)
count=[0]*m
for i in range(1,m+1):
    a,b=ab[-i]
    A=parent[a]
    B=parent[b]
    if A==0 and B==0:
        parent[a]=a
        parent[b]=a
        tree[a].append(b)
        count[-i]=1
    elif A==0:
        count[-i]=len(tree[B])+1
        parent[a]=B
        tree[B].append(a)
    elif B==0:
        count[-i]=len(tree[A])+1
        parent[b]=A
        tree[A].append(b)
    elif parent[a]==parent[b]:
        count[-i]=0
    else:
        A=parent[a]
        B=parent[b]
        u=len(tree[A])
        v=len(tree[B])
        count[-i]=(u+1)*(v+1)   
        if u<v:
            A,B=B,A
            a,b=b,a
        for u in tree[B]:
            parent[u]=A
        parent[B]=A
        tree[A].append(B)
        tree[A]+=tree[B]
        tree[B]=[]
sum=0
for i in range(0,m):
    sum+=count[i]
    print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
tree=[[] for i in range(n+1)]
parent=[0]*(n+1)
count=[0]*m
for i in range(1,m+1):
    a,b=ab[-i]
    if parent[a]==0 and parent[b]==0:
        parent[a]=a
        parent[b]=a
        tree[a].append(b)
        count[-i]=1
    elif parent[a]==0:
        count[-i]=len(tree[parent[b]])+1
        parent[a]=parent[b]
        tree[parent[b]].append(a)
    elif parent[b]==0:
        count[-i]=len(tree[parent[a]])+1
        parent[b]=parent[a]
        tree[parent[a]].append(b)
    elif parent[a]==parent[b]:
        count[-i]=0
    else:
        A=parent[a]
        B=parent[b]
        count[-i]=(len(tree[A])+1)*(len(tree[B])+1)   
        for u in tree[B]:
            parent[u]=A
        parent[B]=A
        tree[A].append(B)
        tree[A]+=tree[B]
        tree[B]=[]
sum=0
for i in range(0,m):
    sum+=count[i]
    print(sum)

Fixed submission:
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
tree=[[] for i in range(n+1)]
parent=[0]*(n+1)
count=[0]*m
for i in range(1,m+1):
    a,b=ab[-i]
    A=parent[a]
    B=parent[b]
    if A==0 and B==0:
        parent[a]=a
        parent[b]=a
        tree[a].append(b)
        count[-i]=1
    elif A==0:
        count[-i]=len(tree[B])+1
        parent[a]=B
        tree[B].append(a)
    elif B==0:
        count[-i]=len(tree[A])+1
        parent[b]=A
        tree[A].append(b)
    elif parent[a]==parent[b]:
        count[-i]=0
    else:
        A=parent[a]
        B=parent[b]
        u=len(tree[A])
        v=len(tree[B])
        count[-i]=(u+1)*(v+1)   
        if u<v:
            A,B=B,A
            a,b=b,a
        for u in tree[B]:
            parent[u]=A
        parent[B]=A
        tree[A].append(B)
        tree[A]+=tree[B]
        tree[B]=[]
sum=0
for i in range(0,m):
    sum+=count[i]
    print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
tree=[[] for i in range(n+1)]
parent=[0]*(n+1)
count=[0]*m
for i in range(1,m+1):
    a,b=ab[-i]
    if parent[a]==0 and parent[b]==0:
        parent[a]=a
        parent[b]=a
        tree[a].append(b)
        count[-i]=1
    elif parent[a]==0:
        count[-i]=len(tree[parent[b]])+1
        parent[a]=parent[b]
        tree[parent[b]].append(a)
    elif parent[b]==0:
        count[-i]=len(tree[parent[a]])+1
        parent[b]=parent[a]
        tree[parent[a]].append(b)
    elif parent[a]==parent[b]:
        count[-i]=0
    else:
        A=parent[a]
        B=parent[b]
        count[-i]=(len(tree[A])+1)*(len(tree[B])+1)   
        for u in tree[B]:
            parent[u]=A
        parent[B]=A
        tree[A].append(B)
        tree[A]+=tree[B]
        tree[B]=[]
sum=0
for i in range(0,m):
    sum+=count[i]
    print(sum)

Fixed submission:
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(m)]
tree=[[] for i in range(n+1)]
parent=[0]*(n+1)
count=[0]*m
for i in range(1,m+1):
    a,b=ab[-i]
    A=parent[a]
    B=parent[b]
    if A==0 and B==0:
        parent[a]=a
        parent[b]=a
        tree[a].append(b)
        count[-i]=1
    elif A==0:
        count[-i]=len(tree[B])+1
        parent[a]=B
        tree[B].append(a)
    elif B==0:
        count[-i]=len(tree[A])+1
        parent[b]=A
        tree[A].append(b)
    elif parent[a]==parent[b]:
        count[-i]=0
    else:
        A=parent[a]
        B=parent[b]
        u=len(tree[A])
        v=len(tree[B])
        count[-i]=(u+1)*(v+1)   
        if u<v:
            A,B=B,A
            a,b=b,a
        for u in tree[B]:
            parent[u]=A
        parent[B]=A
        tree[A].append(B)
        tree[A]+=tree[B]
        tree[B]=[]
sum=0
for i in range(0,m):
    sum+=count[i]
    print(sum)