AtCoder-Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ryusuke920/AtCoder-Library

:heavy_check_mark: Tree/UnionFindTree.py

Verified with

Code

class UnionFind:
    def __init__(self, n: int) -> None:
        self.n = n
        self.p = [-1] * n


    def leader(self, a: int) -> int:
        while self.p[a] >= 0:
            a = self.p[a]
        return a


    def merge(self, a: int, b: int) -> int:
        x = self.leader(a)
        y = self.leader(b)

        if x == y:
            return x

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

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

        return x

    def same(self, a: int, b: int) -> bool:
        return self.leader(a) == self.leader(b)

    def groups(self) -> list:
        member = [[] for _ in range(self.n)]
        for i in range(self.n):
            member[self.leader(i)].append(i)
        return member

    def size(self, a: int) -> int:
        return -self.p[self.leader(a)]


def main() -> None:
    N, M = map(int, input().split())

    UF = UnionFind(N)

    for _ in range(m):
        A, B = map(lambda x: int(x) - 1,input().split())
        UF.merge(A, B)

    ans = 0
    for i in range(N):
        ans = max(ans, UF.size(i))

    print(ans)


if __name__ == "__main__":
    main()
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/python.py", line 96, in bundle
    raise NotImplementedError
NotImplementedError
Back to top page