Ejemplo n.º 1
0
    def test_groups(self) -> None:
        dsu = DSU(5)

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

        assert dsu.groups() == [[0, 1, 2], [3], [4]]
Ejemplo n.º 2
0
def resolve():
    H, W = LI()
    Q = I()

    d = ((1, 0), (0, 1), (-1, 0), (0, -1))

    red = [[False] * W for _ in range(H)]
    dsu = DSU(H * W)

    for _ in range(Q):
        q = LI()
        if q[0] == 1:
            r, c = q[1:]
            r -= 1
            c -= 1
            red[r][c] = True
            for dy, dx in d:
                ny = r + dy
                nx = c + dx
                if 0 <= ny < H and 0 <= nx < W and red[ny][nx]:
                    cur = r * W + c
                    nxt = ny * W + nx
                    dsu.merge(cur, nxt)
        else:
            ra, ca, rb, cb = q[1:]
            ra -= 1
            ca -= 1
            rb -= 1
            cb -= 1
            a = ra * W + ca
            b = rb * W + cb
            if red[ra][ca] and red[rb][cb] and dsu.same(a, b):
                print('Yes')
            else:
                print('No')
Ejemplo n.º 3
0
def kruskal(es, V):
    es.sort(key=lambda x: x[2])
    dsu = DSU(V)
    res = 0
    for e in es:
        if not dsu.same(e[0], e[1]):
            dsu.merge(e[0], e[1])
            res += e[2]
    return res
Ejemplo n.º 4
0
def main():
    n, m = list(map(int, input().strip().split()))
    tree = DSU(n)
    for i in range(m):
        a, b = list(map(int, input().strip().split()))
        a = a - 1
        b = b - 1
        if a >= b:
            tree.merge(a, b)

    print(len(tree.groups()))
Ejemplo n.º 5
0
def main():
    n, q = list(map(int, input().strip().split()))
    tree = DSU(n)
    for _ in range(q):
        t, v1, v2 = list(map(int, input().strip().split()))
        if t == 0:
            tree.merge(v1, v2)
        if t == 1:
            if tree.same(v1, v2):
                print(1)
            else:
                print(0)
Ejemplo n.º 6
0
def main():
    n, m = list(map(int, input().strip().split()))
    tree = DSU(n)
    for _ in range(m):
        a, b = list(map(int, input().strip().split()))
        tree.merge(a - 1, b - 1)

    result = 0
    for g in tree.groups():
        result = max(result, len(g))

    print(result)
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
    def test_size(self) -> None:
        dsu = DSU(5)

        dsu.merge(0, 1)
        assert dsu.size(0) == 2
        dsu.merge(0, 2)
        assert dsu.size(0) == 3

        # Test assertion of invalid indices
        for i in (-1, 5):
            with pytest.raises(AssertionError):
                dsu.size(i)
Ejemplo n.º 9
0
def main() -> None:
    n, q = map(int, sys.stdin.readline().split())
    dsu = DSU(n)

    for _ in range(q):
        t, u, v = map(int, sys.stdin.readline().split())
        if t == 0:
            dsu.merge(u, v)
        if t == 1:
            if dsu.same(u, v):
                print(1)
            else:
                print(0)
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
    def test_benchmark(self, benchmark: BenchmarkFixture) -> None:
        random.seed(0)
        n = 100000

        dsu = DSU(n)
        pairs = []
        for _ in range(1000000):
            pairs.append((random.randrange(0, n), random.randrange(0, n)))

        benchmark(self._merge_benchmark, dsu, pairs)
Ejemplo n.º 12
0
    def test_merge(self) -> None:
        dsu = DSU(5)

        assert not dsu.same(0, 1)

        dsu.merge(0, 1)
        assert dsu.same(0, 1)

        # Test assertion of invalid pairs
        for i in range(-1, 6):
            for j in range(-1, 6):
                if not (0 <= i < 5 and 0 <= j < 5):
                    with pytest.raises(AssertionError):
                        dsu.merge(i, j)
Ejemplo n.º 13
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]]
Ejemplo n.º 14
0
H, W = (int(x) for x in input().split())
H, W = H + 2, W + 2
Q = int(input())
DIRECTIONS = [(-1, 0), (1, 0), (0, -1), (0, 1)]


def make_array(*args, default=int):
    if len(args) == 0:
        return default()

    return [make_array(*args[1:]) for _ in range(args[0])]


colored = make_array(H, W, default=False)
dsu = DSU(H * W)

for _ in range(Q):
    t, *rest = (int(x) for x in input().split())
    if t == 1:
        r, c = rest
        if colored[r][c]:
            continue

        colored[r][c] = True
        for dr, dc in DIRECTIONS:
            if colored[r + dr][c + dc]:
                dsu.merge((r + dr) * W + c + dc, r * W + c)

    else:
        ra, ca, rb, cb = rest
Ejemplo n.º 15
0
from atcoder.dsu import DSU
N, M = (int(x) for x in input().split())
edges = []
cost = 0
for _ in range(M):
    A, B, C = (int(x) for x in input().split())
    edges.append((C, A - 1, B - 1))
    if C > 0:
        cost += C

edges.sort()

dsu = DSU(N)

for c, a, b in edges:
    if not dsu.same(a, b):
        dsu.merge(a, b)
        if c > 0:
            cost -= c

print(cost)
Ejemplo n.º 16
0
def dsu():
    return DSU(5)
Ejemplo n.º 17
0
from atcoder.dsu import DSU

N = int(input())
edges = []
for _ in range(N - 1):
    u, v, w = (int(x) for x in input().split())
    edges.append((w, u - 1, v - 1))

edges.sort()

dsu = DSU(N)
ans = 0
for w, u, v in edges:
    ans += w * dsu.size(u) * dsu.size(v)
    dsu.merge(u, v)

print(ans)
Ejemplo n.º 18
0
Archivo: a.py Proyecto: 3w36zj6/AtCoder
import sys

from atcoder.dsu import DSU

n, q = map(int, sys.stdin.readline().split())
dsu = DSU(n)

for _ in range(q):
    t, u, v = map(int, sys.stdin.readline().split())
    if t == 0:
        dsu.merge(u, v)
    if t == 1:
        if dsu.same(u, v):
            print(1)
        else:
            print(0)
Ejemplo n.º 19
0
 def _merge_benchmark(self, dsu: DSU, pairs: List[Tuple[int, int]]) -> None:
     for i, j in pairs:
         dsu.merge(i, j)