Problem p02573 (python)

Fixed submission:
class UnionFind:

    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {root: self.members(root) for root in self.roots()}

    def __str__(self):
        return '\n'.join(f'{root}: {self.members(root)}' for root in self.roots())


N, M = map(int, input().split())

uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    uf.union(a - 1, b - 1)

print(max(uf.size(i) for i in range(N)))

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

G = []
lookup_tab = {}

AB = [list(sorted((map(int, input().split())))) for _ in range(M)]
AB.sort()
for a, b in AB:
    if a not in lookup_tab and b not in lookup_tab:
        G.append({a, b})
        lookup_tab[a] = len(G) - 1
        lookup_tab[b] = len(G) - 1
    elif a in lookup_tab and b not in lookup_tab:
        gid = lookup_tab[a]
        lookup_tab[b] = gid
        G[gid].add(b)
    elif b in lookup_tab and a not in lookup_tab:
        gid = lookup_tab[b]
        lookup_tab[a] = gid
        G[gid].add(a)
    else:
        assert(lookup_tab[a] == lookup_tab[b])
        continue

if G:
    print(max([len(g) for g in G]))
else:
    print(1)

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

G = []
m = {}
for a, b in zip(A, B):
    if a not in m and b not in m:
        G.append({a, b})
        m[a] = len(G) - 1
        m[b] = len(G) - 1
    elif a in m:
        gid = m[a]
        G[gid].add(b)
        m[b] = gid
    elif b in m:
        gid = m[b]
        G[gid].add(a)
        m[a] = gid

if M == 0:
    print(1)
else:
    print(max(len(g) for g in G))

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

G = []
m = {}
for a, b in zip(A, B):
    if a not in m and b not in m:
        G.append({a, b})
        m[a] = len(G) - 1
        m[b] = len(G) - 1
    elif a in m:
        G[m[a]].add(b)
    elif b in m:
        G[m[b]].add(a)

if M == 0:
    print(1)
else:
    print(max(len(g) for g in G))

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

G = []
m = {}
for a, b in zip(A, B):
    if a not in m and b not in m:
        G.append({a, b})
        m[a] = len(G) - 1
        m[b] = len(G) - 1
    elif a in m:
        gid = m[a]
        G[gid].add(b)
    elif b in m:
        gid = m[b]
        G[gid].add(a)

max_size = max(len(g) for g in G)
print(max_size)

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

G = []
m = {}
for a, b in zip(A, B):
    if a not in m and b not in m:
        G.append({a, b})
        m[a] = len(G) - 1
        m[b] = len(G) - 1
    elif a in m:
        gid = m[a]
        G[gid].add(b)
    elif b in m:
        gid = m[b]
        G[gid].add(a)

max_size = max(len(g) for g in G)
print(max_size)