Problem p02573 (python)

Fixed submission:
import sys


class UnionFind():
    def __init__(self, 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


N, M = map(int, input().split())
info = [tuple(map(int, s.split())) for s in sys.stdin.readlines()]

uf = UnionFind(N)
for a, b in info:
    a -= 1; b -= 1
    uf.union(a, b)

ans = min(uf.parents)

print(-ans)

Buggy submission (verdict: Runtime Error):
import sys
n, m = map(int, input().split())
info = [tuple(map(int, s.split())) for s in sys.stdin.readlines()]

pars = [-1]*n

def get_root(x):
    if pars[x] < 0:
        return x
    else:
        pars[x] = get_root(pars[x])
        return pars[x]

def combine(x, y):
    if x == y:
        return
    r_x = get_root(x)
    r_y = get_root(y)
    if r_x > r_y:
        r_x, r_y = r_y, r_x
    if r_x != r_y:
        pars[r_x] += pars[r_y] # 足して経路長を求めるのに使う
        pars[r_y] = r_x

def size(x):
    return -pars[get_root(x)]

for x, y in info:
    combine(x, y)

print(-min(pars))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
info = [tuple(map(int, s.split())) for s in sys.stdin.readlines()]

pars = [-1]*n

def get_root(x):
    if pars[x] < 0:
        return x
    else:
        return get_root(pars[x])


def combine(x, y):
    if x == y:
        return
    r_x = get_root(x)
    r_y = get_root(y)
    if r_x > r_y:
        r_x, r_y = r_y, r_x
    if r_x != r_y:
        pars[r_x] += pars[r_y] # 足して経路長を求めるのに使う
        pars[r_y] = r_x

def size(x):
    return -pars[get_root(x)]

for x, y in info:
    combine(x, y)

print(-min(pars))

Buggy submission (verdict: Runtime Error):
nm = input().split(" ")
n = int(nm[0])
m = int(nm[1])

pars = [-1]*n

def get_root(x):
    if pars[x] < 0:
        return x
    else:
        return get_root(pars[x])


def combine(x, y):
    if x == y:
        return
    r_x = get_root(x)
    r_y = get_root(y)
    if r_x > r_y:
        r_x, r_y = r_y, r_x
    if r_x != r_y:
        pars[r_x] += pars[r_y] # 足して経路長を求めるのに使う
        pars[r_y] = r_x

def size(x):
    return -pars[get_root(x)]

for i in range(m):
    pair = input().split(" ")
    x = int(pair[0]) - 1 # 入力が1スタートなので
    y = int(pair[1]) - 1 # 入力が1スタートなので
    combine(x, y)

max_len = 0
for i in range(n):
    len = size(i)
    if len > max_len:
        max_len = len

print(max_len)

Buggy submission (verdict: Runtime Error):
nm = input().split(" ")
n = int(nm[0])
m = int(nm[1])

pars = [-1]*n

def get_root(x):
    if pars[x] < 0:
        return x
    else:
        return get_root(pars[x])


def combine(x, y):
    r_x = get_root(x)
    r_y = get_root(y)
    if r_x > r_y:
        r_x, r_y = r_y, r_x
    if r_x != r_y:
        pars[r_x] += pars[r_y] # 足して経路長を求めるのに使う
        pars[r_y] = r_x

def size(x):
    return -pars[get_root(x)]

for i in range(m):
    pair = input().split(" ")
    x = int(pair[0]) - 1 # 入力が1スタートなので
    y = int(pair[1]) - 1 # 入力が1スタートなので
    combine(x, y)

max_len = 0
for i in range(n):
    len = size(i)
    if len > max_len:
        max_len = len

print(max_len)