Problem p03108 (python)

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Wed Mar  6 14:59:59 2019

@author: Yamazaki Kenichi
"""

N,M = map(int,input().split())
bb = [list(map(int,(input().split()))) for i in range(M)]

def comb2(N):
    return N*(N-1)//2
ans = [int(comb2(N))]
bb.reverse()

T = [[-1,1] for i in range(N+1)]
def p(u):
    return T[u][0]
def findset(u):
    if p(u) == -1:
        return u
    else:
        T[u][0] = findset(p(u))
        return T[u][0]
def unite(u,v):
    if findset(u) == findset(v):
        return    
    if T[findset(u)][1] >= T[findset(v)][1]:
        T[findset(u)][1] += T[findset(v)][1]
        T[findset(v)][0] = findset(u)
    else:
        T[findset(v)][1] += T[findset(u)][1]
        T[findset(u)][0] = findset(v)
        
for c in bb:
    if findset(c[0]) != findset(c[1]):
        res1 = T[findset(c[0])][1]
        res2 = T[findset(c[1])][1]
        unite(c[0],c[1])
        ans.append(ans[-1]-res1*res2)
    else:
        ans.append(ans[-1])
for i in range(M):
    print(ans[-i-2])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  3 19:52:57 2019

@author: Yamazaki Kenichi
"""
import sys
sys.setrecursionlimit(3000)

N,M = map(int,input().split())
bb = [list(map(int,(input().split()))) for i in range(M)]

def comb2(N):
    return N*(N-1)//2
ans = [int(comb2(N))]
A = [-1 for i in range(N+1)]
B = [[] for i in range(N+1)]
T=[[-1,-1,-1] for i in range(N+1)]
def p(u):
    return T[u][0]
def ch(u):
    return T[u][1]
def root(u):
    if p(u) == -1:
        return u
    else:
        return root(p(u))
def leef(u):
    if ch(u) == -1:
        return u
    else:
        return leef(ch(u))
def pn(u):
    if p(u) == -1:
        return 0
    else:
        return pn(p(u))+1
def cn(u):
    if ch(u) == -1:
        return 0
    else:
        return cn(ch(u)) +1
def al(u):
    return T[u][2]
k = 0
bb.reverse()
for c in bb:
    if ans[-1] == 0:
        ans.append(0)
        continue
    if al(c[0]) == -1 and al(c[1]) ==-1:
        T[c[0]][2],T[c[1]][2] = 1,1
        T[c[0]][1] = c[1]
        T[c[1]][0] = c[0]
        ans.append(ans[-1]-1)
    elif al(c[0]) != -1 and al(c[1]) ==-1:
        T[c[1]][2] = 1
        res = pn(c[0])+1+cn(c[0])
        tmp = leef(c[0])
        T[tmp][1] = c[1]
        T[c[1]][0] = tmp
        ans.append(ans[-1]-res)
    elif al(c[0]) == -1 and al(c[1]) !=-1:
        T[c[0]][2] = 1
        res = pn(c[1])+1+cn(c[1])
        tmp = leef(c[1])
        T[tmp][1] = c[0]
        T[c[0]][0] = tmp
        ans.append(ans[-1]-res)
    elif root(c[0]) != root(c[1]):
        res = (pn(c[0])+1+cn(c[0])) * (pn(c[1])+1+cn(c[1]))
        tmp = leef(c[0])
        tmp2 = root(c[1])
        T[tmp][1] = tmp2
        T[tmp2][0] = tmp
        ans.append(ans[-1]-res)
    else:
        ans.append(ans[-1])
        continue
#    print(pn(c[0]),cn(c[0]),pn(c[1]),cn(c[1]))
#    print(T)
for i in range(M):
    print(ans[-i-2])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  3 19:52:57 2019

@author: Yamazaki Kenichi
"""

N,M = map(int,input().split())
bb = [list(map(int,(input().split()))) for i in range(M)]

def comb2(N):
    return N*(N-1)//2
ans = [int(comb2(N))]
A = [-1 for i in range(N+1)]
B = [[] for i in range(N+1)]
T=[[-1,-1,-1] for i in range(N+1)]
def p(u):
    return T[u][0]
def ch(u):
    return T[u][1]
def root(u):
    if p(u) == -1:
        return u
    else:
        return root(p(u))
def leef(u):
    if ch(u) == -1:
        return u
    else:
        return leef(ch(u))
def pn(u):
    if p(u) == -1:
        return 0
    else:
        return pn(p(u))+1
def cn(u):
    if ch(u) == -1:
        return 0
    else:
        return cn(ch(u)) +1
def al(u):
    return T[u][2]
k = 0
bb.reverse()
for c in bb:
    if ans[-1] == 0:
        ans.append(0)
        continue
    if al(c[0]) == -1 and al(c[1]) ==-1:
        T[c[0]][2],T[c[1]][2] = 1,1
        T[c[0]][1] = c[1]
        T[c[1]][0] = c[0]
        ans.append(ans[-1]-1)
    elif al(c[0]) != -1 and al(c[1]) ==-1:
        T[c[1]][2] = 1
        res = pn(c[0])+1+cn(c[0])
        tmp = leef(c[0])
        T[tmp][1] = c[1]
        T[c[1]][0] = tmp
        ans.append(ans[-1]-res)
    elif al(c[0]) == -1 and al(c[1]) !=-1:
        T[c[0]][2] = 1
        res = pn(c[1])+1+cn(c[1])
        tmp = leef(c[1])
        T[tmp][1] = c[0]
        T[c[0]][0] = tmp
        ans.append(ans[-1]-res)
    elif root(c[0]) != root(c[1]):
        res = (pn(c[0])+1+cn(c[0])) * (pn(c[1])+1+cn(c[1]))
        tmp = leef(c[0])
        tmp2 = root(c[1])
        T[tmp][1] = tmp2
        T[tmp2][0] = tmp
        ans.append(ans[-1]-res)
    else:
        ans.append(ans[-1])
        continue
#    print(pn(c[0]),cn(c[0]),pn(c[1]),cn(c[1]))
#    print(T)
for i in range(M):
    print(ans[-i-2])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  3 19:52:57 2019

@author: Yamazaki Kenichi
"""

N,M = map(int,input().split())
bb = [list(map(int,(input().split()))) for i in range(M)]

def comb2(N):
    return N*(N-1)//2
ans = [int(comb2(N))]
A = [-1 for i in range(N+1)]
B = [[] for i in range(N+1)]
T=[[-1,-1,-1] for i in range(N+1)]
def p(u):
    return T[u][0]
def ch(u):
    return T[u][1]
def root(u):
    if p(u) == -1:
        return u
    else:
        return pn(p(u))
def leef(u):
    if ch(u) == -1:
        return u
    else:
        return cn(ch(u))
def pn(u):
    if p(u) == -1:
        return 0
    else:
        return pn(p(u))+1
def cn(u):
    if ch(u) == -1:
        return 0
    else:
        return cn(ch(u)) +1
def al(u):
    return T[u][2]
k = 0
bb.reverse()
for c in bb:
    if ans[-1] == 0:
        ans.append(0)
        continue
    if al(c[0]) == -1 and al(c[1]) ==-1:
        T[c[0]][2],T[c[1]][2] = 1,1
        T[c[0]][1] = c[1]
        T[c[1]][0] = c[0]
        ans.append(ans[-1]-1)
    elif al(c[0]) != -1 and al(c[1]) ==-1:
        T[c[1]][2] = 1
        res = pn(c[0])+1+cn(c[0])
        tmp = leef(c[0])
        T[tmp][1] = c[1]
        T[c[1]][0] = tmp
        ans.append(ans[-1]-res)
    elif al(c[0]) == -1 and al(c[1]) !=-1:
        T[c[0]][2] = 1
        res = pn(c[1])+1+cn(c[1])
        tmp = leef(c[1])
        T[tmp][1] = c[0]
        T[c[0]][0] = tmp
        ans.append(ans[-1]-res)
    else:
        res = (pn(c[0])+1+cn(c[0])) + (pn(c[1])+1+cn(c[1]))
        tmp = leef(c[0])
        tmp2 = root(c[1])
        T[tmp][1] = tmp2
        T[tmp2][0] = tmp
        ans.append(ans[-1]-res)
for i in range(M):
    print(ans[-i-2])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  3 19:52:57 2019

@author: Yamazaki Kenichi
"""
from scipy.special import comb

N,M = map(int,input().split())
bb = [list(map(int,(input().split()))) for i in range(M)]

ans = [int(comb(N,2))]
A = [-1 for i in range(N+1)]
B = [[] for i in range(N+1)]
k = 0
bb.reverse()
for c in bb:
    if ans[-1] == 0:
        ans.append(0)
        continue
    if A[c[0]] == -1 and A[c[1]] ==-1:
        A[c[0]] = k
        A[c[1]] = k
        ans.append(ans[-1]-1)
        B[k].extend(c)
        k += 1
    elif A[c[0]] != -1 and A[c[1]] ==-1:
        tmp = A[c[0]]
        A[c[1]] = tmp
        ans.append(ans[-1]-len(B[tmp]))
        B[tmp].append(c[1])
    elif A[c[0]] == -1 and A[c[1]] !=-1:
        tmp = A[c[1]]
        A[c[0]] = tmp
        ans.append(ans[-1]-len(B[tmp]))
        B[tmp].append(c[0])
    else:
        tmp1 = A[c[0]]
        tmp2 = A[c[1]]
        ans.append(ans[-1]-len(B[tmp1])*len(B[tmp2]))
        tmp = B[tmp1][:]
        B[tmp1] += B[tmp2]
        B[tmp2] += B[tmp1]
for i in range(M):
    print(ans[-i-2])