Problem p03108 (python)

Fixed submission:
N,M = map(int,input().split())
edges = []
for _ in range(M):
    a,b = map(int,input().split())
    edges.append((a-1,b-1))
edges.reverse()
parents = list(range(N))
size = [1 for _ in range(N)]
rank = [0 for _ in range(N)]

def root(x):
    px = parents[x]
    if px == x:
        return(x)
    else:
        r = root(px)
        parents[x] = r
        return(r)

def same(x,y):
    return(root(x) == root(y))

def union(x,y):
    rx = root(x)
    ry = root(y)
    if rx == ry:
        return(0)
    if rank[ry] > rank[rx]:
        parents[rx] = ry
        size[ry] += size[rx]
    else:
        parents[ry] = rx
        size[rx] += size[ry]
        if rank[rx] == rank[ry]:
            rank[rx] += 1
    return(1)

inconvinience = N*(N-1)//2
answers=[inconvinience]

for edge in edges:
    x,y = edge
    rx = root(x)
    ry = root(y)
    sx = size[rx]
    sy = size[ry]
    res = union(rx,ry)
    if res == 1:
        inconvinience -= sx*sy
    answers.append(inconvinience)

answers.reverse()

for answer in answers[1:]:
    print(answer)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
unionfindTree = [i for i in range(N)]
size = [1 for _ in range(N)]
inconvenience = N*(N-1)//2
answers = [inconvenience] #M bridges destroyed
#answers[i] should be inconvenience after M-i bridges destroyed.

def root(x):
    p = unionfindTree[x]
    if x==p:
        return(p)
    else:
        r = root(p)
        unionfindTree[x] = r
        return(r)

def getsize(x):
    r = root(x)
    return(size[r])

def union(x,y):
    rx = root(x)
    ry = root(y)
    convenience = 0
    if rx != ry:
        unionfindTree[ry] = rx
        # sx = size[rx]
        # sy = size[ry]
        # convenience = sx * sy
        # size[rx] = sx+sy
    return(convenience)

revBridges = []
for _ in range(M):
    a,b = map(int,input().split())
    revBridges.append((a-1,b-1))
revBridges.reverse()

for (a,b) in revBridges:
    c = union(a,b)
    inconvenience -= c
    answers.append(inconvenience)
for i in range(M):
    print(answers[M-1-i])

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
unionfindTree = [i for i in range(N)]
size = [1 for _ in range(N)]
inconvenience = N*(N-1)//2
answers = [inconvenience] #M bridges destroyed
#answers[i] should be inconvenience after M-i bridges destroyed.

def root(x):
    p = unionfindTree[x]
    if x==p:
        return(p)
    else:
        r = root(p)
        unionfindTree[x] = r
        return(r)

def getsize(x):
    r = root(x)
    return(size[r])

def union(x,y):
    rx = root(x)
    ry = root(y)
    convenience = 0
    if rx != ry:
        unionfindTree[ry] = rx
        sx = size[rx]
        sy = size[ry]
        convenience = sx * sy
        size[rx] = sx+sy
    return(convenience)

revBridges = []
for _ in range(M):
    a,b = map(int,input().split())
    revBridges.append((a-1,b-1))
revBridges.reverse()

for (a,b) in revBridges:
    c = union(a,b)
    inconvenience -= c
    answers.append(inconvenience)
for i in range(M):
    print(answers[M-1-i])

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
unionfindTree = [i for i in range(N)]
size = [1 for _ in range(N)]
inconvenience = N*(N-1)//2
answers = [inconvenience] #M bridges destroyed
#answers[i] should be inconvenience after M-i bridges destroyed.

def root(x):
    p = unionfindTree[x]
    if x==p:
        return(p)
    else:
        unionfindTree[x] = root(p)
        # size[x] = size[p]
        return(unionfindTree[x])

def getsize(x):
    r = root(x)
    return(size[r])

def union(x,y):
    rx = root(x)
    ry = root(y)
    convenience = 0
    if rx != ry:
        unionfindTree[ry] = rx
        sx = size[rx]
        sy = size[ry]
        convenience = sx * sy
        size[rx] = sx+sy
    return(convenience)

revBridges = []
for _ in range(M):
    a,b = map(int,input().split())
    revBridges.append((a-1,b-1))
revBridges.reverse()

for i in range(M):
    a,b = revBridges[i]
    c = union(a,b)
    inconvenience -= c
    answers.append(inconvenience)
    # print("link",(a,b))
    # print(unionfindTree)
    # print([getsize(i) for i in range(N)])

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
unionfindTree = [i for i in range(N)]
size = [1 for _ in range(N)]
inconvenience = N*(N-1)//2
answers = [inconvenience] #M bridges destroyed
#answers[i] should be inconvenience after M-i bridges destroyed.

def root(x):
    p = unionfindTree[x]
    if x==p:
        return(p)
    else:
        root = root(p)
        unionfindTree[x] = root
        return(root)

def getsize(x):
    r = root(x)
    return(size[r])

def union(x,y):
    rx = root(x)
    ry = root(y)
    convenience = 0
    if rx != ry:
        unionfindTree[ry] = rx
        sx = size[rx]
        sy = size[ry]
        convenience = sx * sy
        size[rx] = sx+sy
    return(convenience)

revBridges = []
for _ in range(M):
    a,b = map(int,input().split())
    revBridges.append((a-1,b-1))
revBridges.reverse()

for (a,b) in revBridges:
    c = union(a,b)
    inconvenience -= c
    answers.append(inconvenience)
# for i in range(M):
    # print(answers[M-1-i])

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
unionfindTree = [i for i in range(N)]
size = [1 for _ in range(N)]
inconvenience = N*(N-1)//2
answers = [inconvenience] #M bridges destroyed
#answers[i] should be inconvenience after M-i bridges destroyed.

def root(x):
    p = unionfindTree[x]
    if x==p:
        return(p)
    else:
        unionfindTree[x] = root(p)
        # size[x] = size[p]
        return(unionfindTree[x])

def getsize(x):
    r = root(x)
    return(size[r])

def union(x,y):
    rx = root(x)
    ry = root(y)
    convenience = 0
    if rx != ry:
        unionfindTree[ry] = rx
        sx = size[rx]
        sy = size[ry]
        convenience = sx * sy
        size[rx] = sx+sy
    return(convenience)

revBridges = []
for _ in range(M):
    a,b = map(int,input().split())
    revBridges.append((a-1,b-1))
revBridges.reverse()

for i in range(M):
    a,b = revBridges[i]
    c = union(a,b)
    inconvenience -= c
    answers.append(inconvenience)
    # print("link",(a,b))
    # print(unionfindTree)
    # print([getsize(i) for i in range(N)])

for i in range(M):
    print(answers[M-1-i])

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
unionfindTree = [i for i in range(N)]
size = [1 for _ in range(N)]
inconvenience = N*(N-1)//2
answers = [inconvenience]

def root(x):
    p = unionfindTree[x]
    if x==p:
        return(p)
    else:
        unionfindTree[x] = root(p)
        return(unionfindTree[x])

def union(x,y):
    rx = root(x)
    ry = root(y)
    convenience = 0
    if rx != ry:
        unionfindTree[ry] = rx
        sx = size[rx]
        sy = size[ry]
        convenience = sx * sy
        size[rx] = sx+sy
    return(convenience)

revBridges = []
for _ in range(M):
    a,b = map(int,input().split())
    revBridges.append((a-1,b-1))

for i in range(M):
    a,b = revBridges[M-1-i]
    c = union(a,b)
    inconvenience -= c
    answers.append(inconvenience)

for i in range(M):
    print(answers[M-1-i])