Example #1
0
    def test_initial_status(self) -> None:
        dsu = DSU(5)

        for i, j in combinations(range(5), 2):
            assert not dsu.same(i, j)

        for index in range(5):
            assert dsu.size(index) == 1
            assert dsu.leader(index) == index

        assert dsu.groups() == [[0], [1], [2], [3], [4]]
Example #2
0
def resolve():
    N = I()
    f = LI_()

    # 2 ** 連結成分数 - 1
    dsu = DSU(N)
    for i, e in enumerate(f):
        dsu.merge(i, e)
    lc_num = len(set(dsu.leader(i) for i in range(N)))

    ans = pow(2, lc_num, MOD) - 1
    print(ans)
Example #3
0
    def test_leader(self) -> None:
        dsu = DSU(5)

        dsu.merge(0, 1)
        dsu.merge(0, 2)

        assert dsu.leader(1) in [0, 1, 2]
        assert dsu.leader(2) in [0, 1, 2]
        assert dsu.leader(1) == dsu.leader(2)
        assert dsu.leader(3) not in [0, 1, 2]
        assert dsu.leader(4) not in [0, 1, 2]

        # Test assertion of invalid indices
        for i in (-1, 5):
            with pytest.raises(AssertionError):
                dsu.leader(i)