結果
問題 | No.1054 Union add query |
ユーザー | 草苺奶昔 |
提出日時 | 2023-05-21 14:43:40 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 545 ms / 2,000 ms |
コード長 | 2,909 bytes |
コンパイル時間 | 282 ms |
コンパイル使用メモリ | 82,100 KB |
実行使用メモリ | 97,548 KB |
最終ジャッジ日時 | 2024-06-01 12:35:55 |
合計ジャッジ時間 | 4,812 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 65 ms
67,584 KB |
testcase_01 | AC | 65 ms
67,456 KB |
testcase_02 | AC | 65 ms
67,712 KB |
testcase_03 | AC | 545 ms
85,028 KB |
testcase_04 | AC | 475 ms
97,548 KB |
testcase_05 | AC | 475 ms
82,812 KB |
testcase_06 | AC | 314 ms
86,164 KB |
testcase_07 | AC | 260 ms
85,768 KB |
testcase_08 | AC | 304 ms
85,632 KB |
testcase_09 | AC | 362 ms
96,164 KB |
testcase_10 | AC | 202 ms
94,024 KB |
ソースコード
from collections import defaultdict from typing import Callable, DefaultDict, List, Optional, Tuple class WeightedUnionFind: """维护分量和的并查集.""" __slots__ = "_parent", "_value", "_delta", "_total", "_part" def __init__(self, n: int): self._parent = [-1] * n self._value = [0] * n self._delta = [0] * n self._total = [0] * n self._part = n def add(self, u: int, delta: int) -> None: """u的值加上delta.""" self._value[u] += delta self._total[self.find(u)] += delta def addGroup(self, u: int, delta: int) -> None: """u所在集合的值加上delta.""" root = self.find(u) self._delta[root] += delta self._total[root] -= self._parent[root] * delta def get(self, u: int) -> int: """u的值.""" return self._value[u] + self._find(u)[1] def getGroup(self, u: int) -> int: """u所在集合的值.""" return self._total[self.find(u)] def union(self, u: int, v: int, f: Optional[Callable[[int, int], None]] = None) -> bool: u = self.find(u) v = self.find(v) if u == v: return False if self._parent[u] > self._parent[v]: u, v = v, u self._parent[u] += self._parent[v] self._parent[v] = u self._delta[v] -= self._delta[u] self._total[u] += self._total[v] self._part -= 1 if f is not None: f(u, v) return True def find(self, u: int) -> int: return self._find(u)[0] def isConnected(self, u: int, v: int) -> bool: return self.find(u) == self.find(v) def getSize(self, u: int) -> int: return -self._parent[self.find(u)] def getGroups(self) -> DefaultDict[int, List[int]]: groups = defaultdict(list) for i in range(len(self._parent)): root = self.find(i) if root in groups: groups[root].append(i) else: groups[root] = [i] return groups @property def part(self) -> int: return self._part def _find(self, u: int) -> Tuple[int, int]: if self._parent[u] < 0: return u, self._delta[u] a, b = self._find(self._parent[u]) b += self._delta[u] self._parent[u] = a self._delta[u] = b - self._delta[a] return a, b if __name__ == "__main__": # https://yukicoder.me/problems/no/1054 import sys input = lambda: sys.stdin.readline().rstrip("\r\n") n, q = map(int, input().split()) uf = WeightedUnionFind(n) for _ in range(q): op, a, b = map(int, input().split()) if op == 1: a, b = a - 1, b - 1 uf.union(a, b) elif op == 2: a -= 1 uf.addGroup(a, b) else: a -= 1 print(uf.get(a))