Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
Ai = [list(map(int, input().split())) for _ in range(M)]

groups = [-1 for i in range(N + 1)] #0番目は使わない
sizes = [1 for _ in range(N + 1)]
Ai.reverse()

value = N * (N - 1) // 2
answers = []
answers.append(value)

def parent(n):
    if groups[n] < 0:
        return n
    return parent(groups[n])

for a in Ai:
    if value <= 0:
        answers.append(0)
        continue
    p1 = parent(a[0])
    p2 = parent(a[1])
    if p1 == p2:
        answers.append(value)
        continue

    value -= sizes[p1] * sizes[p2]
    if p1 > p2:
        sizes[p2] += sizes[p1]
        groups[p1] = p2
    else:
        sizes[p2] += sizes[p1]
        groups[p1] = p2
    answers.append(value)

answers.reverse()
answers = answers[1:]
for a in answers:
    print(a)

Fixed submission:
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a - 1, b - 1])

bridge = bridge[1:]
bridge = reversed(bridge)
result = []


parent = [-1] * n
size = [1] * n
def root(v):
    p = parent[v]
    if p >= 0:
        return root(p)
    return v

value = n * (n - 1) // 2
result.append(value)

for b in bridge:
    if value <= 0:
        result.append(0)
        continue
    p0 = root(b[0])
    p1 = root(b[1])
    if p0 == p1:
        result.append(value)
        continue
    s0 = size[p0]
    s1 = size[p1]
    if p0 < p1:
        parent[p1] = p0
        size[p0] += size[p1]
    else:
        parent[p0] = p1
        size[p1] += size[p0]
    value -= s0 * s1
    result.append(max(value, 0))

result = reversed(result)
for r in result:
    print(r)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
Ai = [list(map(int, input().split())) for _ in range(M)]

groups = [-1 for i in range(N + 1)] #0番目は使わない
sizes = [1 for _ in range(N + 1)]
Ai.reverse()

value = N * (N - 1) // 2
answers = []
answers.append(value)

def parent(n):
    if groups[n] < 0:
        return n
    return parent(groups[n])

for a in Ai:
    p1 = parent(a[0])
    p2 = parent(a[1])

    if p1 != p2:
        if p1 > p2:
            t = p1
            p1 = p2
            p2 = t
        value -= sizes[p1] * sizes[p2]
        if value < 0:
            value = 0
        sizes[p1] += sizes[p2]
        groups[p2] = p1
    answers.insert(0, value)
answers = answers[1:]
for a in answers:
    print(a)

Fixed submission:
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a - 1, b - 1])

bridge = bridge[1:]
bridge = reversed(bridge)
result = []


parent = [-1] * n
size = [1] * n
def root(v):
    p = parent[v]
    if p >= 0:
        return root(p)
    return v

value = n * (n - 1) // 2
result.append(value)

for b in bridge:
    if value <= 0:
        result.append(0)
        continue
    p0 = root(b[0])
    p1 = root(b[1])
    if p0 == p1:
        result.append(value)
        continue
    s0 = size[p0]
    s1 = size[p1]
    if p0 < p1:
        parent[p1] = p0
        size[p0] += size[p1]
    else:
        parent[p0] = p1
        size[p1] += size[p0]
    value -= s0 * s1
    result.append(max(value, 0))

result = reversed(result)
for r in result:
    print(r)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a - 1, b - 1])

bridge = bridge[1:]
bridge = reversed(bridge)
result = []
groups = []

value = n * (n - 1) // 2
result.append(value)

def find_group(v):
    for i, a in enumerate(groups):
        if v in a:
            return i
    return -1

for b in bridge:
    if value <= 0:
        result.append(0)
        continue
    b1 = find_group(b[0])
    b2 = find_group(b[1])
    if b1 < 0 and b2 < 0:
        groups.append(b)
        value -= 1
    elif b1 == b2:
        pass
    elif b1 >= 0 and b2 >= 0:
        value -= (len(groups[b1]) * len(groups[b2]))
        if b1 < b2:
            groups[b1].extend(groups[b2])
            del groups[b2]
        else:
            groups[b2].extend(groups[b1])
            del groups[b1]
    elif b1 >= 0:
        value -= len(groups[b1])
        groups[b1].append(b[1])
    else:
        value -= len(groups[b2])
        groups[b2].append(b[0])
    result.append(max(value, 0))

result = reversed(result)
for r in result:
    print(r)


Fixed submission:
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a - 1, b - 1])

bridge = bridge[1:]
bridge = reversed(bridge)
result = []


parent = [-1] * n
size = [1] * n
def root(v):
    p = parent[v]
    if p >= 0:
        return root(p)
    return v

value = n * (n - 1) // 2
result.append(value)

for b in bridge:
    if value <= 0:
        result.append(0)
        continue
    p0 = root(b[0])
    p1 = root(b[1])
    if p0 == p1:
        result.append(value)
        continue
    s0 = size[p0]
    s1 = size[p1]
    if p0 < p1:
        parent[p1] = p0
        size[p0] += size[p1]
    else:
        parent[p0] = p1
        size[p1] += size[p0]
    value -= s0 * s1
    result.append(max(value, 0))

result = reversed(result)
for r in result:
    print(r)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a - 1, b - 1])

def find_group(groups, a):
    for i in range(0, len(groups)):
        g = groups[i]
        if a in g:
            return i
    return -1

bridge = bridge[1:]
bridge = reversed(bridge)
result = []
groups = []
for i in range(0, n):
    groups.append(i)

result.append(n * (n - 1) // 2)

for b in bridge:
    a1 = groups[b[0]]
    a2 = groups[b[1]]
    if a1 == a2:
        pass
    elif a1 < a2:
        groups[b[0]] = a1
        groups[b[1]] = a1
        for i, g in enumerate(groups):
            if g == a2:
                groups[i] = a1
    else:
        groups[b[0]] = a2
        groups[b[1]] = a2
        for i, g in enumerate(groups):
            if g == a1:
                groups[i] = a2
    counts = list()
    for i in range(0, len(groups)):
        counts.append(0)
    for g in groups:
        counts[g] += 1
    
    count = 0
    for i in range(0, len(counts) - 1):
        for j in range(i + 1, len(counts)):
            count += counts[i] * counts[j]
    result.append(count)

result = reversed(result)
for r in result:
    print(r)


Fixed submission:
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a - 1, b - 1])

bridge = bridge[1:]
bridge = reversed(bridge)
result = []


parent = [-1] * n
size = [1] * n
def root(v):
    p = parent[v]
    if p >= 0:
        return root(p)
    return v

value = n * (n - 1) // 2
result.append(value)

for b in bridge:
    if value <= 0:
        result.append(0)
        continue
    p0 = root(b[0])
    p1 = root(b[1])
    if p0 == p1:
        result.append(value)
        continue
    s0 = size[p0]
    s1 = size[p1]
    if p0 < p1:
        parent[p1] = p0
        size[p0] += size[p1]
    else:
        parent[p0] = p1
        size[p1] += size[p0]
    value -= s0 * s1
    result.append(max(value, 0))

result = reversed(result)
for r in result:
    print(r)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a - 1, b - 1])

def find_group(groups, a):
    for i in range(0, len(groups)):
        g = groups[i]
        if a in g:
            return i
    return -1


while len(bridge) > 0:
    bridge = bridge[1:]
    groups = []
    for i in range(0, n):
        groups.append(i)
    for b in bridge:
        a1 = groups[b[0]]
        a2 = groups[b[1]]
        if a1 == a2:
            continue
        if a1 < a2:
            groups[b[0]] = a1
            groups[b[1]] = a1
            for i, g in enumerate(groups):
                if g == a2:
                    groups[i] = a1
        else:
            groups[b[0]] = a2
            groups[b[1]] = a2
            for i, g in enumerate(groups):
                if g == a1:
                    groups[i] = a2
    counts = list()
    for i in range(0, len(groups)):
        counts.append(0)
    for g in groups:
        counts[g] += 1
    
    count = 0
    for i in range(0, len(counts) - 1):
        for j in range(i + 1, len(counts)):
            count += counts[i] * counts[j]
    print(count)

Fixed submission:
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a - 1, b - 1])

bridge = bridge[1:]
bridge = reversed(bridge)
result = []


parent = [-1] * n
size = [1] * n
def root(v):
    p = parent[v]
    if p >= 0:
        return root(p)
    return v

value = n * (n - 1) // 2
result.append(value)

for b in bridge:
    if value <= 0:
        result.append(0)
        continue
    p0 = root(b[0])
    p1 = root(b[1])
    if p0 == p1:
        result.append(value)
        continue
    s0 = size[p0]
    s1 = size[p1]
    if p0 < p1:
        parent[p1] = p0
        size[p0] += size[p1]
    else:
        parent[p0] = p1
        size[p1] += size[p0]
    value -= s0 * s1
    result.append(max(value, 0))

result = reversed(result)
for r in result:
    print(r)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a, b])

def find_group(groups, a):
    for i in range(0, len(groups)):
        g = groups[i]
        if a in g:
            return i
    return -1


while len(bridge) > 0:
    bridge = bridge[1:]
    groups = []
    for i in range(1, n + 1):
        groups.append([i])
    for b in bridge:
        a1 = find_group(groups, b[0])
        a2 = find_group(groups, b[1])
        if a1 == a2:
            continue
        aa = groups.pop(a2)
        if a1 > a2:
            a1 -= 1
        groups[a1].extend(aa)
    count = 0
    for i in range(0, len(groups) - 1):
        for j in range(i + 1, len(groups)):
            count += len(groups[i]) * len(groups[j])
    print(count)

Fixed submission:
n, m = map(int, input().split())
bridge = list()

for i in range(0, m):
    a, b = map(int, input().split())
    bridge.append([a - 1, b - 1])

bridge = bridge[1:]
bridge = reversed(bridge)
result = []


parent = [-1] * n
size = [1] * n
def root(v):
    p = parent[v]
    if p >= 0:
        return root(p)
    return v

value = n * (n - 1) // 2
result.append(value)

for b in bridge:
    if value <= 0:
        result.append(0)
        continue
    p0 = root(b[0])
    p1 = root(b[1])
    if p0 == p1:
        result.append(value)
        continue
    s0 = size[p0]
    s1 = size[p1]
    if p0 < p1:
        parent[p1] = p0
        size[p0] += size[p1]
    else:
        parent[p0] = p1
        size[p1] += size[p0]
    value -= s0 * s1
    result.append(max(value, 0))

result = reversed(result)
for r in result:
    print(r)