Пример #1
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)
Пример #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')
Пример #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
Пример #4
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]]
Пример #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)
Пример #6
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)
Пример #7
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)
Пример #8
0
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)
Пример #9
0
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
        if colored[ra][ca] and colored[rb][cb] and dsu.same(
                ra * W + ca, rb * W + cb):
            print("Yes")
        else:
            print("No")