Problem p03108 (python)

Fixed submission:
def search_root(l, n):
    i = n
    while l[i] != i:
        i = l[i]
    return i


def set_root(l, root_index, start_index):
    i = start_index
    while l[i] != root_index:
        tmp = i
        i = l[i]
        l[tmp] = root_index


def func(n):
    return (n * (n - 1)) // 2


node_size, input_size = [int(i) for i in input().split()]

br = []
for _ in range(input_size):
    br.append([int(i) for i in input().split()])

cnt_list = [0]
size_list = [1 for _ in range(node_size + 1)]
node_list = [i for i in range(node_size + 1)]

for i in range(1, input_size + 1):
    node1, node2 = br[-i]

    root1, root2 = search_root(node_list, node1), search_root(node_list, node2)

    if root1 == root2:
        cnt_list.append(cnt_list[-1])
    else:
        cnt_list.append(cnt_list[-1] + (size_list[root1] * size_list[root2]))
        if size_list[root1] < size_list[root2]:
            node_list[root1] = root2
            size_list[root2] += size_list[root1]
        else:
            node_list[root2] = root1
            size_list[root1] += size_list[root2]

val = func(node_size)
for i in cnt_list[::-1][1:]:
    print(val - i)

Buggy submission (verdict: Runtime Error):
def func(n):
    return (n * (n - 1)) // 2


node_size, input_size = [int(i) for i in input().split()]

br = [func(node_size)]
for _ in range(input_size):
    br.append([int(i) for i in input().split()])

cnt_list = [func(node_size)]

index = 0
node_to_group = {}
group_to_index = {}
index_size = {}

for j in range(1, input_size + 1):
    flag1, flag2 = br[-j][0] in node_to_group, br[-j][1] in node_to_group

    if flag1 and flag2:
        if group_to_index[node_to_group[br[-j][0]]] == group_to_index[node_to_group[br[-j][1]]]:
            tmp = cnt_list[0]
        else:
            tmp = cnt_list[0] + func(index_size[group_to_index[node_to_group[br[-j][0]]]]) + \
                  func(index_size[group_to_index[node_to_group[br[-j][1]]]]) - \
                  func(index_size[group_to_index[node_to_group[br[-j][0]]]] +
                       index_size[group_to_index[node_to_group[br[-j][1]]]])
            index_size[group_to_index[node_to_group[br[-j][0]]]] += index_size[group_to_index[node_to_group[br[-j][1]]]]
            del index_size[group_to_index[node_to_group[br[-j][1]]]]
            group_to_index[node_to_group[br[-j][1]]] = group_to_index[node_to_group[br[-j][0]]]

    elif flag1:
        tmp = cnt_list[0] - index_size[group_to_index[node_to_group[br[-j][0]]]]
        node_to_group[br[-j][1]] = node_to_group[br[-j][0]]
        index_size[group_to_index[node_to_group[br[-j][0]]]] += 1
    elif flag2:
        tmp = cnt_list[0] - index_size[group_to_index[node_to_group[br[-j][1]]]]
        node_to_group[br[-j][0]] = node_to_group[br[-j][1]]
        index_size[group_to_index[node_to_group[br[-j][1]]]] += 1
    else:
        tmp = cnt_list[0] - 1
        node_to_group[br[-j][0]] = br[-j][0]
        node_to_group[br[-j][1]] = br[-j][0]
        group_to_index[br[-j][0]] = index
        index_size[index] = 2
        index += 1
    
    if tmp == 0:
        for _ in range(input_size - j):
            print(0)
        break
    cnt_list.insert(0, tmp)

for i in cnt_list:
    print(i)

Buggy submission (verdict: Runtime Error):
def func(n):
    return (n * (n - 1)) // 2


node_size, input_size = [int(i) for i in input().split()]

br = [func(node_size)]
for _ in range(input_size):
    br.append([int(i) for i in input().split()])

cnt_list = [func(node_size)]

index = 0
node_to_group = {}
group_to_index = {}
index_size = {}

for j in range(1, input_size + 1):
    flag1, flag2 = br[-j][0] in node_to_group, br[-j][1] in node_to_group

    if flag1 and flag2:
        if group_to_index[node_to_group[br[-j][0]]] == group_to_index[node_to_group[br[-j][1]]]:
            tmp = cnt_list[0]
        else:
            tmp = cnt_list[0] + func(index_size[group_to_index[node_to_group[br[-j][0]]]]) + \
                  func(index_size[group_to_index[node_to_group[br[-j][1]]]]) - \
                  func(index_size[group_to_index[node_to_group[br[-j][0]]]] +
                       index_size[group_to_index[node_to_group[br[-j][1]]]])
            index_size[group_to_index[node_to_group[br[-j][0]]]] += index_size[group_to_index[node_to_group[br[-j][1]]]]
            del index_size[group_to_index[node_to_group[br[-j][1]]]]
            group_to_index[node_to_group[br[-j][1]]] = group_to_index[node_to_group[br[-j][0]]]

    elif flag1:
        tmp = cnt_list[0] - index_size[group_to_index[node_to_group[br[-j][0]]]]
        node_to_group[br[-j][1]] = node_to_group[br[-j][0]]
        index_size[group_to_index[node_to_group[br[-j][0]]]] += 1
    elif flag2:
        tmp = cnt_list[0] - index_size[group_to_index[node_to_group[br[-j][1]]]]
        node_to_group[br[-j][0]] = node_to_group[br[-j][1]]
        index_size[group_to_index[node_to_group[br[-j][1]]]] += 1
    else:
        tmp = cnt_list[0] - 1
        node_to_group[br[-j][0]] = br[-j][0]
        node_to_group[br[-j][1]] = br[-j][0]
        group_to_index[br[-j][0]] = index
        index_size[index] = 2
        index += 1
    print(index_size)
    if tmp == 0:
        for _ in range(input_size - j):
            print(0)
        break
    cnt_list.insert(0, tmp)

for i in cnt_list:
    print(i)