Problem p03108 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(100001)


def solve(n: int, m: int, A: "List[int]", B: "List[int]"):
    par = [i for i in range(n)]
    nums = [1] * n
    def get_root(a):
        if par[a] == a:
            return a
        root = get_root(par[a])
        par[a] = root
        return par[a]
    def merge(a, b):
        root_a = get_root(a)
        root_b = get_root(b)
        if root_a == root_b:
            return 0
        else:
            ret = nums[root_a] * nums[root_b]
            group = nums[root_a] + nums[root_b]
            par[root_a] = root_b
            nums[root_b] = group
            return ret

    con = 0
    ret = []
    pairs = n * (n - 1) // 2
    ret.append(pairs - con)
    for i in range(m - 1):
        idx = m - i - 1
        a = A[idx] - 1
        b = B[idx] - 1
        con += merge(a, b)
        ret.append(pairs - con)
    ret.reverse()
    for c in ret:
        print(c)
    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int()] * (M)  # type: "List[int]" 
    B = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, M, A, B)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys


def solve(n: int, m: int, A: "List[int]", B: "List[int]"):
    par = [i for i in range(n)]
    nums = [1] * n
    def get_root(a):
        if par[a] == a:
            return a
        root = get_root(par[a])
        par[a] = root
        return par[a]
    def merge(a, b):
        root_a = get_root(a)
        root_b = get_root(b)
        if root_a == root_b:
            return 0
        else:
            ret = nums[root_a] * nums[root_b]
            group = nums[root_a] + nums[root_b]
            par[root_a] = root_b
            nums[root_b] = group
            return ret

    con = 0
    ret = []
    pairs = n * (n - 1) // 2
    ret.append(pairs - con)
    for i in range(m - 1):
        idx = m - i - 1
        a = A[idx] - 1
        b = B[idx] - 1
        con += merge(a, b)
        ret.append(pairs - con)
    ret.reverse()
    for c in ret:
        print(c)
    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int()] * (M)  # type: "List[int]" 
    B = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, M, A, B)

if __name__ == '__main__':
    main()