結果
問題 | No.1054 Union add query |
ユーザー | 草苺奶昔 |
提出日時 | 2023-05-21 14:08:49 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,936 bytes |
コンパイル時間 | 158 ms |
コンパイル使用メモリ | 82,852 KB |
実行使用メモリ | 97,872 KB |
最終ジャッジ日時 | 2024-06-01 12:33:44 |
合計ジャッジ時間 | 4,764 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | AC | 65 ms
67,476 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 311 ms
85,808 KB |
testcase_07 | AC | 259 ms
86,020 KB |
testcase_08 | AC | 300 ms
85,756 KB |
testcase_09 | AC | 352 ms
96,080 KB |
testcase_10 | AC | 199 ms
93,452 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: 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] p = self._find(self._parent[u]) first = p[0] second = p[1] + self._delta[u] - self._delta[first] self._parent[u] = first self._delta[u] = second return p 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))