Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = []
for i in range(M):
    A.append(list(map(int,input().split())))
A = A[::-1]

P = [[x+1] for x in range(N)]
n = [x+1 for x in range(N)]
a = [N*(N-1)/2]

for i in range(M-1):
    if a[i] == 0:
        a.append(0)
    else:        
        p = P[n[A[i][0]-1]-1]
        q = P[n[A[i][1]-1]-1]
        if n[A[i][0]-1] != n[A[i][1]-1]:
            a.append(a[i]-len(p) * len(q))
            if len(p) >= len(q):                
                P[n[A[i][0]-1]-1].extend(q)
                for j in q:
                    n[j-1] = n[A[i][0]-1]
            else:
                P[n[A[i][1]-1]-1].extend(p)
                for j in p:
                    n[j-1] = n[A[i][1]-1]
        else:
            a.append(a[i])
        
for k in range(M):
    print(int(a[::-1][k]))

Fixed submission:
N, M = map(int, input().split())
A = []
for i in range(M):
    A.append(list(map(int,input().split())))
A = A[::-1]


P = [[x+1] for x in range(N)]
n = [x+1 for x in range(N)]
an = [N*(N-1)/2]


for i in range(M-1):
    if an[i] == 0:
        an.append(0)
    else:        
        Aind = A[i][0]-1
        Bind = A[i][1]-1
        PAind = P[n[Aind]-1]
        PBind = P[n[Bind]-1]
        if n[Aind] != n[Bind]:
            if len(PAind) >= len(PBind):
                an.append(an[i]-len(PAind) * len(PBind))
                P[n[Aind]-1].extend(PBind)
                for j in PBind:
                    n[j-1] = n[Aind]
            else:
                an.append(an[i]-len(PAind) * len(PBind))
                P[n[Bind]-1].extend(PAind)
                for j in PAind:
                    n[j-1] = n[Bind]
        else:
            an.append(an[i])
            
        
an = an[::-1]
for k in range(M):
    print(int(an[k]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = []
for i in range(M):
    A.append(list(map(int,input().split())))
A = A[::-1]


P = [[x+1] for x in range(N)]
n = [x+1 for x in range(N)]
an = [N*(N-1)/2]


for i in range(M-1):
    if an[i] == 0:
        an.append(0)
    else:        
        Aind = A[i][0]-1
        Bind = A[i][1]-1
        PAind = P[n[Aind]-1]
        PBind = P[n[Bind]-1]
        if n[Aind] != n[Bind]:
            an.append(an[i]-len(PAind) * len(PBind))
            P[n[Aind]-1].extend(PBind)
            for j in PBind:
                n[j-1] = n[Aind]
        else:
            an.append(an[i])
            
        
an = an[::-1]
for k in range(M):
    print(int(an[k]))

Fixed submission:
N, M = map(int, input().split())
A = []
for i in range(M):
    A.append(list(map(int,input().split())))
A = A[::-1]


P = [[x+1] for x in range(N)]
n = [x+1 for x in range(N)]
an = [N*(N-1)/2]


for i in range(M-1):
    if an[i] == 0:
        an.append(0)
    else:        
        Aind = A[i][0]-1
        Bind = A[i][1]-1
        PAind = P[n[Aind]-1]
        PBind = P[n[Bind]-1]
        if n[Aind] != n[Bind]:
            if len(PAind) >= len(PBind):
                an.append(an[i]-len(PAind) * len(PBind))
                P[n[Aind]-1].extend(PBind)
                for j in PBind:
                    n[j-1] = n[Aind]
            else:
                an.append(an[i]-len(PAind) * len(PBind))
                P[n[Bind]-1].extend(PAind)
                for j in PAind:
                    n[j-1] = n[Bind]
        else:
            an.append(an[i])
            
        
an = an[::-1]
for k in range(M):
    print(int(an[k]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = []
for i in range(M):
    A.append(list(map(int,input().split())))
A = A[::-1]


P = [[x+1] for x in range(N)]
n = [x+1 for x in range(N)]
an = [N*(N-1)/2]


for i in range(M-1):
    Aind = A[i][0]-1
    Bind = A[i][1]-1
    PAind = P[n[Aind]-1]
    PBind = P[n[Bind]-1]
    if n[Aind] != n[Bind]:
        an.append(an[i]-len(PAind) * len(PBind))
        P[n[Aind]-1].extend(PBind)
        for j in PBind:
            n[j-1] = n[Aind]
    else:
        an.append(an[i])
        
        
an = an[::-1]
for k in range(M):
    print(int(an[k]))

Fixed submission:
N, M = map(int, input().split())
A = []
for i in range(M):
    A.append(list(map(int,input().split())))
A = A[::-1]


P = [[x+1] for x in range(N)]
n = [x+1 for x in range(N)]
an = [N*(N-1)/2]


for i in range(M-1):
    if an[i] == 0:
        an.append(0)
    else:        
        Aind = A[i][0]-1
        Bind = A[i][1]-1
        PAind = P[n[Aind]-1]
        PBind = P[n[Bind]-1]
        if n[Aind] != n[Bind]:
            if len(PAind) >= len(PBind):
                an.append(an[i]-len(PAind) * len(PBind))
                P[n[Aind]-1].extend(PBind)
                for j in PBind:
                    n[j-1] = n[Aind]
            else:
                an.append(an[i]-len(PAind) * len(PBind))
                P[n[Bind]-1].extend(PAind)
                for j in PAind:
                    n[j-1] = n[Bind]
        else:
            an.append(an[i])
            
        
an = an[::-1]
for k in range(M):
    print(int(an[k]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = []
for i in range(M):
    A.append(list(map(int,input().split())))
A = A[::-1]


P = [[x+1] for x in range(N)]
n = [x+1 for x in range(N)]
an = [N*(N-1)/2]


for i in range(M-1):
    Aind = A[i][0]-1
    Bind = A[i][1]-1
    if n[Aind] != n[Bind]:
        an.append(an[i]-len(P[n[Aind]-1]) * len(P[n[Bind]-1]))
        P[n[Aind]-1].extend(P[n[Bind]-1])
        for j in P[n[Bind]-1]:
            n[j-1] = n[Aind]
    else:
        an.append(an[i])
        
        
an = an[::-1]
for k in range(M):
    print(int(an[k]))

Fixed submission:
N, M = map(int, input().split())
A = []
for i in range(M):
    A.append(list(map(int,input().split())))
A = A[::-1]


P = [[x+1] for x in range(N)]
n = [x+1 for x in range(N)]
an = [N*(N-1)/2]


for i in range(M-1):
    if an[i] == 0:
        an.append(0)
    else:        
        Aind = A[i][0]-1
        Bind = A[i][1]-1
        PAind = P[n[Aind]-1]
        PBind = P[n[Bind]-1]
        if n[Aind] != n[Bind]:
            if len(PAind) >= len(PBind):
                an.append(an[i]-len(PAind) * len(PBind))
                P[n[Aind]-1].extend(PBind)
                for j in PBind:
                    n[j-1] = n[Aind]
            else:
                an.append(an[i]-len(PAind) * len(PBind))
                P[n[Bind]-1].extend(PAind)
                for j in PAind:
                    n[j-1] = n[Bind]
        else:
            an.append(an[i])
            
        
an = an[::-1]
for k in range(M):
    print(int(an[k]))