Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
parent = [i for i in range(n)]
count = [1 for i in range(n)]

def root(i):
    if i == parent[i]:
        return i
    parent[i] = root(parent[i])
    return parent[i]

def merge(i, j):
    parent[j] = i
    count[i] += count[j]

ret = [0] * m + [n * (n - 1) // 2]
for i in range(m - 1, -1, -1):
    a, b = ab[i]
    ra, rb = root(a - 1), root(b - 1)
    if ra == rb:
        ret[i] = ret[i + 1]
    else:
        ret[i] = ret[i + 1] - count[ra] * count[rb]
        merge(ra, rb)

for i in range(1, m + 1):
    print(ret[i])


Fixed submission:
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
group = [i for i in range(n)]
islands = {i: [i] for i in range(n)}
ret = [0] * m + [n * (n - 1) // 2]
for i in range(m - 1, -1, -1):
    a, b = ab[i]
    ga, gb = group[a - 1], group[b - 1]
    if ga == gb:
        ret[i] = ret[i + 1]
    else:
        la, lb = len(islands[ga]), len(islands[gb])
        ret[i] = ret[i + 1] - la * lb
        if la > lb:
            for j in islands[gb]:
                group[j] = ga
                islands[ga].append(j)
        else:
            for j in islands[ga]:
                group[j] = gb
                islands[gb].append(j)
for i in range(1, m + 1):
    print(ret[i])

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
group = [i for i in range(n)]
islands = {i: [i] for i in range(n)}
ret = [0] * m + [n * (n - 1) // 2]
for i in range(m - 1, -1, -1):
    a, b = ab[i]
    ga, gb = group[a - 1], group[b - 1]
    if ga == gb:
        ret[i] = ret[i + 1]
    else:
        ret[i] = ret[i + 1] - len(islands[ga]) * len(islands[gb])
        islands[ga] += islands[gb]
        for j in islands[gb]:
            group[j] = ga
for i in range(1, m + 1):
    print(ret[i])

Fixed submission:
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
group = [i for i in range(n)]
islands = {i: [i] for i in range(n)}
ret = [0] * m + [n * (n - 1) // 2]
for i in range(m - 1, -1, -1):
    a, b = ab[i]
    ga, gb = group[a - 1], group[b - 1]
    if ga == gb:
        ret[i] = ret[i + 1]
    else:
        la, lb = len(islands[ga]), len(islands[gb])
        ret[i] = ret[i + 1] - la * lb
        if la > lb:
            for j in islands[gb]:
                group[j] = ga
                islands[ga].append(j)
        else:
            for j in islands[ga]:
                group[j] = gb
                islands[gb].append(j)
for i in range(1, m + 1):
    print(ret[i])

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
group = [i for i in range(n)]
islands = {i: [i] for i in range(n)}
ret = [0] * m + [n * (n - 1) // 2]
for i in range(m - 1, -1, -1):
    a, b = ab[i]
    ga, gb = group[a - 1], group[b - 1]
    if ga == gb:
        ret[i] = ret[i + 1]
    else:
        ret[i] = ret[i + 1] - len(islands[ga]) * len(islands[gb])
        islands[ga] += islands[gb]
        for j in islands[gb]:
            group[j] = ga
for i in range(1, m + 1):
    print(ret[i])

Fixed submission:
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
group = [i for i in range(n)]
islands = {i: [i] for i in range(n)}
ret = [0] * m + [n * (n - 1) // 2]
for i in range(m - 1, -1, -1):
    a, b = ab[i]
    ga, gb = group[a - 1], group[b - 1]
    if ga == gb:
        ret[i] = ret[i + 1]
    else:
        la, lb = len(islands[ga]), len(islands[gb])
        ret[i] = ret[i + 1] - la * lb
        if la > lb:
            for j in islands[gb]:
                group[j] = ga
                islands[ga].append(j)
        else:
            for j in islands[ga]:
                group[j] = gb
                islands[gb].append(j)
for i in range(1, m + 1):
    print(ret[i])