Problem p02573 (python)

Fixed submission:
N, M = map(int, input().split())

if M == 0:
    print(1)
    exit()

group_list = []
group_index = [0] * N

for _ in range(M):
    a, b = map(int, input().split())
    if group_index[a-1] == 0 and group_index[b-1] == 0:
        group_index[a-1] = len(group_list) + 1
        group_index[b-1] = len(group_list) + 1
        group_list.append(set([a, b]))

    elif group_index[a-1] == 0:
        group_index[a-1] = group_index[b-1]
        group_list[group_index[b-1] - 1].add(a)

    elif group_index[b-1] == 0:
        group_index[b-1] = group_index[a-1]
        group_list[group_index[a-1] - 1].add(b)

    elif group_index[a-1] != group_index[b-1]:
        if len(group_list[group_index[a-1] - 1]) > len(group_list[group_index[b-1] - 1]):
            group_id_b = group_index[b-1] - 1
            for p in group_list[group_id_b]:
                group_index[p-1] = group_index[a-1]
                group_list[group_index[a-1] - 1].add(p)
            group_list[group_id_b] = set()
        else:
            group_id_a = group_index[a-1] - 1
            for p in group_list[group_id_a]:
                group_index[p-1] = group_index[b-1]
                group_list[group_index[b-1] - 1].add(p)
            group_list[group_id_a] = set()

print(max([len(g) for g in group_list]))

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

group_list = []
group_index = [0] * N

for _ in range(M):
    a, b = map(int, input().split())
    if group_index[a-1] == 0 and group_index[b-1] == 0:
        group_index[a-1] = len(group_list) + 1
        group_index[b-1] = len(group_list) + 1
        group_list.append(set([a, b]))

    elif group_index[a-1] == 0:
        group_index[a-1] = group_index[b-1]
        group_list[group_index[b-1] - 1].add(a)

    elif group_index[b-1] == 0:
        group_index[b-1] = group_index[a-1]
        group_list[group_index[a-1] - 1].add(b)

    elif group_index[a-1] != group_index[b-1]:
        group_id_b = group_index[b-1] - 1
        for p in group_list[group_id_b]:
            group_index[p-1] = group_index[a-1]
            group_list[group_index[a-1] - 1].add(p)
        group_list[group_id_b].clear()

print(max([len(g) for g in group_list]))

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = []
B = []
for _ in range(M):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

group_list = []

group_index = [0] * N

last_group = 1

for a, b in zip(A, B):
    if group_index[a-1] == 0 and group_index[b-1] == 0:
        group_index[a-1] = last_group
        group_index[b-1] = last_group
        group_list.append(set([a, b]))
        last_group += 1
    elif group_index[a-1] == 0:
        group_index[a-1] = group_index[b-1]
        group_list[group_index[b-1] - 1].add(a)
    elif group_index[b-1] == 0:
        group_index[b-1] = group_index[a-1]
        group_list[group_index[b-1] - 1].add(b)
    elif group_index[a-1] != group_index[b-1]:
        for p in group_list[group_index[b-1] - 1]:
            group_index[p - 1] = group_index[a-1]
            group_list[group_index[a-1] - 1].add(p)
        group_list[group_index[b-1] - 1] = set()

#print(group_index)
#print(group_list)


print(max([len(g) for g in group_list]))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
from itertools import combinations

N, M = map(int, input().split())
A = []
B = []
for _ in range(M):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

group_list = []

#D = defaultdict(set)
for a, b in zip(A, B):
    if len(group_list) == 0:
        group_list.append(set([a, b]))
    else:
        flag = True
        for g in group_list:
            if a in g:
                g.add(b)
                flag = False
                break
            if b in g:
                g.add(a)
                flag = False
                break
        if flag:
            group_list.append(set([a, b]))

print(max([len(g) for g in group_list]))