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')
def test_groups(self) -> None: dsu = DSU(5) dsu.merge(0, 1) dsu.merge(0, 2) assert dsu.groups() == [[0, 1, 2], [3], [4]]
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
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()))
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)
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)
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)
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)
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)
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)
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)
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)
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)
def _merge_benchmark(self, dsu: DSU, pairs: List[Tuple[int, int]]) -> None: for i, j in pairs: dsu.merge(i, j)
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")