Problem p02573 (python)

Fixed submission:
"""D."""

import sys

#def input() -> str:  # noqa: A001
#    """Input."""
#    return sys.stdin.readline()[:-1]


class UnionFindTree:
    """Union-find."""

    def __init__(self, n: int) -> None:
        """Init."""
        self.n = n
        self.rank = [-1] * n

    def find_root(self, x: int) -> int:
        """Find root."""

        if self.rank[x] < 0:
            return x

        self.rank[x] = self.find_root(self.rank[x])
        return self.rank[x]

    def union(self, x: int, y: int) -> None:
        """Unite two trees."""
        x_root = self.find_root(x)
        y_root = self.find_root(y)

        if x_root == y_root:
            return

        if x_root > y_root:
            x_root, y_root = y_root, x_root

        self.rank[x_root] += self.rank[y_root]
        self.rank[y_root] = x_root

    def get_size(self, x: int) -> int:
        """Get size."""
        return self.rank[self.find_root(x)] * -1

    def is_same(self, x: int, y: int) -> bool:
        """Is same group."""
        return self.find_root(x) == self.find_root(y)


n, m = map(int, input().split(' '))

tree = UnionFindTree(n)

for _ in range(m):
    a, b = map(int, input().split(' '))
    tree.union(a - 1, b - 1)

ans = 0

for i in range(n):
    ans = max(ans, tree.get_size(i))

print(ans)

Buggy submission (verdict: Runtime Error):
"""D."""

import sys

#def input() -> str:  # noqa: A001
#    """Input."""
#    return sys.stdin.readline()[:-1]


class UnionFindTree:
    """Union-find."""

    def __init__(self, n: int) -> None:
        """Init."""
        self.n = n
        self.parent = [-1] * n

    def find_root(self, x: int) -> int:
        """Find root."""
        parent = self.parent[x]
        if parent < 0:
            return x
        return self.parent[self.find_root(parent)]

    def union(self, x: int, y: int) -> None:
        """Unite two trees."""
        x_root = self.find_root(x)
        y_root = self.find_root(y)

        if x_root == y_root:
            return

        if x_root > y_root:
            x_root, y_root = y_root, x_root

        self.parent[x_root] += self.parent[y_root]
        self.parent[y_root] = x_root

    def get_size(self, x: int) -> int:
        """Get size."""
        return self.parent[self.find_root(x)] * -1

    def is_same(self, x: int, y: int) -> bool:
        """Is same group."""
        return self.find_root(x) == self.find_root(y)


n, m = map(int, input().split(' '))

tree = UnionFindTree(n)

for _ in range(m):
    a, b = map(int, input().split(' '))
    tree.union(a - 1, b - 1)

ans = 0

for i in range(n):
    ans = max(ans, tree.get_size(i))

print(ans)

Buggy submission (verdict: Runtime Error):
"""D."""

import sys

#def input() -> str:  # noqa: A001
#    """Input."""
#    return sys.stdin.readline()[:-1]


class UnionFindTree:
    """Union-find."""

    def __init__(self, n: int) -> None:
        """Init."""
        self.n = n
        self.parent = [-1] * n

    def find_root(self, x: int) -> int:
        """Find root."""
        parent = self.parent[x]
        if parent < 0:
            return x
        return self.find_root(parent)

    def union(self, x: int, y: int) -> None:
        """Unite two trees."""
        x_root = self.find_root(x)
        y_root = self.find_root(y)

        if x_root == y_root:
            return

        if x_root > y_root:
            x_root, y_root = y_root, x_root

        self.parent[x_root] += self.parent[y_root]
        self.parent[y_root] = x_root

    def get_size(self, x: int) -> int:
        """Get size."""
        return self.parent[self.find_root(x)] * -1

    def is_same(self, x: int, y: int) -> bool:
        """Is same group."""
        return self.find_root(x) == self.find_root(y)


n, m = map(int, input().split(' '))

tree = UnionFindTree(n)

for _ in range(m):
    a, b = map(int, input().split(' '))
    tree.union(a - 1, b - 1)

ans = 0

for i in range(n):
    ans = max(ans, tree.get_size(i))

print(ans)

Buggy submission (verdict: Runtime Error):
"""D."""

import sys
from typing import List


def input() -> str:  # noqa: A001
    """Input."""
    return sys.stdin.readline()[:-1]


class UnionFindTree:
    """Union-find."""

    def __init__(self, n: int) -> None:
        """Init."""
        self.n = n
        self.parent = [-1] * n

    def find_root(self, x: int) -> int:
        """Find root."""
        parent = self.parent[x]
        if parent < 0:
            return x
        return self.find_root(parent)

    def union(self, x: int, y: int) -> None:
        """Unite two trees."""
        x_root = self.find_root(x)
        y_root = self.find_root(y)

        if x_root == y_root:
            return

        if x_root > y_root:
            x_root, y_root = y_root, x_root

        self.parent[x_root] += self.parent[y_root]
        self.parent[y_root] = x_root

    def get_size(self, x: int) -> int:
        """Get size."""
        return self.parent[self.find_root(x)] * -1

    def is_same(self, x: int, y: int) -> bool:
        """Is same group."""
        return self.find_root(x) == self.find_root(y)


n, m = map(int, input().split(' '))

tree = UnionFindTree(n)

for _ in range(m):
    a, b = map(int, input().split(' '))
    tree.union(a - 1, b - 1)

ans = 0

for i in range(n):
    ans = max(ans, tree.get_size(i))

print(ans)

Buggy submission (verdict: Runtime Error):
"""D."""

import sys
from typing import List


def input() -> str:  # noqa: A001
    """Input."""
    return sys.stdin.readline()[:-1]


n, m = map(int, input().split(' '))

groups: List[set] = []
for _ in range(m):
    a, b = map(int, input().split(' '))
    for g in groups:
        if a in g:
            g.add(b)
            break
        if b in g:
            g.add(a)
            break
    else:
        groups.append({a, b})
print(max([len(g) for g in groups]))