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 {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


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

ans = 0
for i in range(M):
    A, B = map(int, input().split())
    uf.union(A-1, B-1)

for i in range(uf.group_count()):
    if ans < uf.size(i):
        ans = uf.size(i)
print(ans)

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

l = []
A = 0
B = 0
ans = 1
if M > 0:
    A, B = map(int, input().split())
    l.append([A, B])
for i in range(M-1):
    A, B = map(int, input().split())
    hit = None
    for tup in l:
        if A in tup or B in tup:
            if hit != None:
                hit = set(hit+tup+[A, B])
            else:
                hit = set(tup+[A, B])
            l.remove(tup)
    if hit != None:
        l.append(hit)
    else:
        l.append((A, B))

if len(l) > 0:
    ans = 1
for item in (l):
    if ans < len(item):
        ans = len(item)
print(ans)

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

l = []

A, B = map(int, input().split())
l.append((A, B))
for i in range(M-1):
    A, B = map(int, input().split())
    hit = None
    for tup in l:
        if A in tup or B in tup:
            if hit != None:
                hit = tuple(set(hit+tup+(A, B)))
            else:
                hit = tuple(set(tup+(A, B)))
            l.remove(tup)
    if hit != None:
        l.append(hit)
    else:
        l.append((A, B))
ans = 0

for item in (l):
    if ans < len(item):
        ans = len(item)
print(ans)

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

l = []

A, B = map(int, input().split())
l.append((A, B))
for i in range(M-1):
    A, B = map(int, input().split())
    hit = None
    for tup in l:
        if A in tup or B in tup:
            if hit != None:
                hit += tuple(set(tup+(A, B)))
                hit
            else:
                hit = tuple (set(tup+(A, B)))
            l.remove(tup)
    if hit != None:
        l.append(hit)
    else:
        l.append((A, B))
ans = 0

for item in (l):
    if ans < len(item):
        ans = len(item)
print(ans)