Problem p03108 (python)

Fixed submission:
def getparent(parent, place):
    childrens = [place]
    while place != parent[place]:
        place = parent[place]
        childrens.append(place)
    for i in childrens:
        parent[i] = place
    return place


def main():
    ans = []
    n, m = list(map(int, input().split()))
    pairs = []
    parent = [i for i in range(n)]
    parent_sum = [1 for _ in range(n)]
    for i in range(m):
        a, b = list(map(int, input().split()))
        pairs.append((a - 1, b - 1))
    sum_count = n * (n - 1) // 2
    ans.append(sum_count)
    for p, q in reversed(pairs[1:]):
        np, nq = getparent(parent, p), getparent(parent, q)
        if np != nq:
            sum_count -= parent_sum[np] * parent_sum[nq]
            parent_sum[np] += parent_sum[nq]
            parent[nq] = np
        ans.append(sum_count)
    ans.reverse()
    for i in ans:
        print(i)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def getparent(parent, place):
    my_parent = parent[place]
    if my_parent == place:
        return place
    parent[place] = getparent(parent, my_parent)
    return parent[place]


def main():
    ans = []
    n, m = list(map(int, input().split()))
    broad = [[] for i in range(n)]
    b_pair = []
    parent = [i for i in range(n)]
    parent_sum = [1 for i in range(n)]
    for i in range(m):
        a, b = list(map(int, input().split()))
        a -= 1
        b -= 1
        broad[a].append(b)
        broad[b].append(a)
        b_pair.append((a, b))
    sum_count = n * (n - 1) // 2
    ans.append(sum_count)
    b_pair.reverse()
    b_pair.pop()
    for p, q in b_pair:
        np, nq = getparent(parent, p), getparent(parent, q)
        if np != nq:
            sum_count -= parent_sum[np] * parent_sum[nq]
            parent_sum[np] += parent_sum[nq]
            parent[nq] = np
        ans.append(sum_count)
    ans.reverse()
    for i in ans:
        print(i)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def count_max(road):
    num = len(road)
    used = [False for i in range(num)]
    res = 0
    for i in range(num):
        if used[i]:
            continue
        used[i] = True
        line = road[i][:]
        for j in line:
            used[j] = True
        count = 1 + len(line)
        while line:
            now_point = line.pop()
            next = [j for j in road[now_point] if not used[j]]
            for j in next:
                used[j] = True
            count += len(next)
            line += next
        res += count * (count - 1) // 2
    return res


def getparent(parent, place):
    my_parent = parent[place]
    if my_parent == place:
        return place
    parent[place] = getparent(parent, my_parent)
    return parent[place]


def main():
    ans = []
    n, m = list(map(int, input().split()))
    broad = [[] for i in range(n)]
    b_pair = []
    parent = [i for i in range(n)]
    parent_sum = [1 for i in range(n)]
    for i in range(m):
        a, b = list(map(int, input().split()))
        a -= 1
        b -= 1
        broad[a].append(b)
        broad[b].append(a)
        b_pair.append((a, b))
    sum_count = count_max(broad)
    ans.append(sum_count)
    b_pair.reverse()
    b_pair.pop()
    for p, q in b_pair:
        np, nq = getparent(parent, p), getparent(parent, q)
        if np != nq:
            sum_count -= parent_sum[np] * parent_sum[nq]
            parent_sum[np] += parent_sum[nq]
            parent[nq] = np
        ans.append(sum_count)
    ans.reverse()
    for i in ans:
        print(i)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def count_max(road):
    num = len(road)
    used = [False for i in range(num)]
    res = 0
    for i in range(num):
        if used[i]:
            continue
        used[i] = True
        line = road[i][:]
        for j in line:
            used[j] = True
        count = 1
        while len(line):
            now_point = line.pop()
            count += 1
            next = [j for j in road[now_point] if not used[j]]
            for j in next:
                used[j] = True
            line += next
        res += count * (count - 1) // 2
    return res


def getparent(parent, place):
    my_parent = parent[place]
    if my_parent == place:
        return place
    parent[place] = getparent(parent, my_parent)
    return parent[place]

def main():
    ans = []
    n, m = list(map(int, input().split()))
    broad = [[] for i in range(n)]
    b_pair = []
    parent = [i for i in range(n)]
    parent_sum = [1 for i in range(n)]
    for i in range(m):
        a, b = list(map(int, input().split()))
        a -= 1
        b -= 1
        broad[a].append(b)
        broad[b].append(a)
        b_pair.append((a, b))
    sum_count = count_max(broad)
    ans.append(sum_count)
    b_pair.reverse()
    b_pair.pop()
    for p, q in b_pair:
        np, nq = getparent(parent, p), getparent(parent, q)
        if np != nq:
            sum_count -= parent_sum[np] * parent_sum[nq]
            parent_sum[np] += parent_sum[nq]
            parent[nq] = np
        ans.append(sum_count)
    ans.reverse()
    for i in ans:
        print(i)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def count_max(road):
    num = len(road)
    used = [False for i in range(num)]
    res = 0
    for i in range(num):
        if used[i]:
            continue
        used[i] = True
        line = road[i][:]
        for j in line:
            used[j] = True
        count = 1
        while len(line):
            now_point = line.pop()
            count += 1
            next = [j for j in road[now_point] if not used[j]]
            for j in next:
                used[j] = True
            line += next
        res += count * (count - 1) // 2
    return res


def getparent(parent, place):
    my_parent = parent[place]
    if my_parent == place:
        return place
    parent[place] = getparent(parent, my_parent)
    return parent[place]

def main():
    ans = []
    n, m = list(map(int, input().split()))
    broad = [[] for i in range(n)]
    b_pair = []
    parent = [i for i in range(n)]
    parent_sum = [1 for i in range(n)]
    for i in range(m):
        a, b = list(map(int, input().split()))
        a -= 1
        b -= 1
        broad[a].append(b)
        broad[b].append(a)
        b_pair.append((a, b))
    sum_count = count_max(broad)
    ans.append(sum_count)
    b_pair.reverse()
    b_pair.pop()
    for p, q in b_pair:
        if getparent(parent, p) != getparent(parent, q):
            p, q = getparent(parent, p), getparent(parent, q)
            sum_count -= parent_sum[p] * parent_sum[q]
            parent_sum[p] += parent_sum[q]
            parent[q] = p
        ans.append(sum_count)
    ans.reverse()
    for i in ans:
        print(i)


if __name__ == "__main__":
    main()