"""Union-find (disjoint-set forest): union by size, full path compression.

Layout: a single Python list `p` of n entries. For a non-root x, p[x] is its
parent; for a root x, p[x] is -size (always negative), so `parent(x)` reports
x for a root. Packing the size into the root's slot means a union touches
only the two root slots it already had to read (one cache line each).

The operations are closures over `p` (and the set counter), stored in the
instance's slots: calling them skips method binding and `self.` attribute
loads, and `p` is a fast cell-variable load.
"""


class UnionFind:
    __slots__ = ("_n", "find", "union", "connected", "set_size", "sets",
                 "parent")

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

        def find(x):
            r = p[x]
            if r < 0:
                return x
            root = p[r]
            if root < 0:
                return r
            # x is at least 2 levels deep: locate the root ...
            q = p[root]
            while q >= 0:
                root = q
                q = p[root]
            # ... then link every element on the path directly to it.
            p[x] = root
            x = r
            while True:
                nxt = p[x]
                if nxt == root:
                    return root
                p[x] = root
                x = nxt

        def union(a, b):
            nonlocal count
            sa = p[a]
            if sa < 0:
                ra = a
            else:
                ra = sa
                sa = p[ra]
                if sa >= 0:
                    ra = find(a)
                    sa = p[ra]
            sb = p[b]
            if sb < 0:
                rb = b
            else:
                rb = sb
                sb = p[rb]
                if sb >= 0:
                    rb = find(b)
                    sb = p[rb]
            if ra == rb:
                return False
            # sizes are stored negated: sa <= sb means a's set is not smaller
            if sa <= sb:
                p[ra] = sa + sb
                p[rb] = ra
            else:
                p[rb] = sa + sb
                p[ra] = rb
            count -= 1
            return True

        def connected(a, b):
            ra = p[a]
            if ra < 0:
                ra = a
            elif p[ra] >= 0:
                ra = find(a)
            rb = p[b]
            if rb < 0:
                rb = b
            elif p[rb] >= 0:
                rb = find(b)
            return ra == rb

        def set_size(x):
            r = p[x]
            if r < 0:
                return -r
            s = p[r]
            if s < 0:
                return -s
            return -p[find(x)]

        def sets():
            return count

        def parent(x):
            r = p[x]
            return x if r < 0 else r

        self.find = find
        self.union = union
        self.connected = connected
        self.set_size = set_size
        self.sets = sets
        self.parent = parent

    def __len__(self) -> int:
        return self._n
