Problem p02573 (python)

Fixed submission:
from collections import deque
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
t = dict()
for i in range(1, n+1):
    t[i] = set()
for a, b in ab:
    t[a].add(b)
    t[b].add(a)

q = deque()
s = set()
ans = 0
for i in range(1, n+1):
    if i not in s:
        q.append(i)
        s.add(i)
        tmp = [i]
        while q:
            v = q.popleft()
            for ti in t[v]:
                if ti not in s:
                    tmp.append(ti)
                    q.append(ti)
                    s.add(ti)
    ans = max(ans, len(tmp))
print(ans)

Buggy submission (verdict: Runtime Error):
class UnionFind():
    def __init__(self, n):
        self.par = [-1]*n

    def root(self, x):
        if self.par[x] < 0:
            return x
        return self.root(self.par[x])

    def unite(self, x, y):
        if x > y:
            x, y = y, x
        rx = self.root(x)
        ry = self.root(y)
        if rx != ry:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx

    def size(self, x):
        return -self.par[self.root(x)]

    def same(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        return rx == ry

    def return_par(self):
        return self.par

n, m = map(int, input().split())
uf = UnionFind(n)
for i in range(m):
    a, b = map(int, input().split())
    uf.unite(a-1, b-1)

ans = 0
for i in range(n):
    ans = max(ans, uf.size(i))
print(ans)

Buggy submission (verdict: Runtime Error):
class UnionFind():
    def __init__(self, n):
        self.par = [-1]*n

    def root(self, x):
        if self.par[x] < 0:
            return x
        return self.root(self.par[x])

    def unite(self, x, y):
        if x > y:
            x, y = y, x
        rx = self.root(x)
        ry = self.root(y)
        if rx != ry:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx

    def size(self, x):
        return -self.par[self.root(x)]

    def same(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        return rx == ry

    def return_par(self):
        return self.par

n, m = map(int, input().split())
uf = UnionFind(n)
for i in m:
    a, b = map(int, input().split())
    uf.unite(a-1, b-1)

ans = 0
for i in range(n):
    ans = max(ans, uf.size(i))
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

class UnionFind:
    def __init__(self, n):
        self.par = [-1]*n

    def root(self, x):
        if self.par[x] < 0:
            return x
        return self.root(self.par[x])

    def unite(self, x, y):
        if x > y:
            x, y = y, x
        rx = self.root(x)
        ry = self.root(y)
        if rx != ry:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx

    def size(self, x):
        return -self.par[self.root(x)]

    def same(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        return rx == ry

    def return_par(self):
        return self.par

uf = UnionFind(n)
for a, b in ab:
    uf.unite(a-1, b-1)

ans = 0
for i in range(n):
    ans = max(ans, uf.size(i))
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

class UnionFind:
    par = []
    def __init__(self, n):
        for i in range(n):
            self.par.append(i)

    def root(self, x):
        if self.par[x] == x:
            return x
        return self.root(self.par[x])

    def unite(self, x, y):
        if x > y:
            x, y = y, x
        rx = self.root(x)
        ry = self.root(y)
        if rx != ry:
            self.par[ry] = rx

    def same(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        return rx == ry

    def return_par(self):
        return self.par

uf = UnionFind(n)
for a, b in ab:
    uf.unite(a-1, b-1)
par = uf.return_par()
d = dict()
for p in par:
    if p not in d.keys():
        d[p] = 1
    else:
        d[p] += 1
print(max(d.values()))

Buggy submission (verdict: Runtime Error):
from collections import deque

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
t = []
s = set()
q = deque()

a, b = ab[0]
q.append(a)
lt = set

while len(q) != 0:
    x = q.popleft()
    s.add(x)
    tt = {x}
    tmp = []
    #print(x)
    #print(ab)
    for a, b in ab:
        if a in tt and b not in s:
            tt.add(b)
            s.add(b)
        elif b in tt and a not in s:
            tt.add(a)
            s.add(a)
        elif a not in tt and b not in tt:
            if a not in q:
                q.append(a)
                tmp.append([a, b])
    ab = tmp
    #print(q)
    t.append(tt)
#print(t)
ans = 0
for tt in t:
    ans = max(ans, len(tt))
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
t = []
s = set()
q = deque()

a, b = ab[0]
q.append(a)
lt = set

while len(q) != 0:
    x = q.popleft()
    s.add(x)
    tt = [x]
    for a, b in ab:
        if a in tt and b not in s:
            tt.append(b)
            s.add(b)
        elif b in tt and a not in s:
            tt.append(a)
            s.add(a)
        elif a not in s:
            q.append(a)
            s.add(a)
    t.append(tt)

ans = 0
for tt in t:
    ans = max(ans, len(tt))
print(ans)