結果
問題 | No.768 Tapris and Noel play the game on Treeone |
ユーザー | 草苺奶昔 |
提出日時 | 2023-03-28 02:38:56 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 297 ms / 2,000 ms |
コード長 | 2,816 bytes |
コンパイル時間 | 164 ms |
コンパイル使用メモリ | 82,376 KB |
実行使用メモリ | 113,856 KB |
最終ジャッジ日時 | 2024-09-19 19:33:26 |
合計ジャッジ時間 | 6,555 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 62 ms
69,244 KB |
testcase_01 | AC | 60 ms
68,524 KB |
testcase_02 | AC | 61 ms
68,608 KB |
testcase_03 | AC | 88 ms
78,260 KB |
testcase_04 | AC | 95 ms
78,376 KB |
testcase_05 | AC | 92 ms
77,872 KB |
testcase_06 | AC | 78 ms
74,836 KB |
testcase_07 | AC | 222 ms
93,908 KB |
testcase_08 | AC | 176 ms
86,220 KB |
testcase_09 | AC | 193 ms
88,256 KB |
testcase_10 | AC | 173 ms
85,964 KB |
testcase_11 | AC | 281 ms
102,260 KB |
testcase_12 | AC | 284 ms
102,336 KB |
testcase_13 | AC | 281 ms
101,796 KB |
testcase_14 | AC | 275 ms
101,972 KB |
testcase_15 | AC | 297 ms
101,864 KB |
testcase_16 | AC | 295 ms
100,844 KB |
testcase_17 | AC | 290 ms
101,964 KB |
testcase_18 | AC | 281 ms
113,856 KB |
testcase_19 | AC | 286 ms
111,260 KB |
testcase_20 | AC | 271 ms
108,368 KB |
testcase_21 | AC | 265 ms
104,236 KB |
20evil_special_uni1.txt | AC | 300 ms
102,924 KB |
20evil_special_uni2.txt | AC | 278 ms
101,776 KB |
ソースコード
from typing import Callable, Generic, List, TypeVar T = TypeVar("T") class Rerooting(Generic[T]): __slots__ = ("adjList", "_n", "_decrement") def __init__(self, n: int, decrement: int = 0): self.adjList = [[] for _ in range(n)] self._n = n self._decrement = decrement def addEdge(self, u: int, v: int) -> None: u -= self._decrement v -= self._decrement self.adjList[u].append(v) self.adjList[v].append(u) def rerooting( self, e: Callable[[int], T], op: Callable[[T, T], T], composition: Callable[[T, int, int, int], T], root=0, ) -> List["T"]: root -= self._decrement assert 0 <= root < self._n parents = [-1] * self._n order = [root] stack = [root] while stack: cur = stack.pop() for next in self.adjList[cur]: if next == parents[cur]: continue parents[next] = cur order.append(next) stack.append(next) dp1 = [e(i) for i in range(self._n)] dp2 = [e(i) for i in range(self._n)] for cur in order[::-1]: res = e(cur) for next in self.adjList[cur]: if parents[cur] == next: continue dp2[next] = res res = op(res, composition(dp1[next], cur, next, 0)) res = e(cur) for next in self.adjList[cur][::-1]: if parents[cur] == next: continue dp2[next] = op(res, dp2[next]) res = op(res, composition(dp1[next], cur, next, 0)) dp1[cur] = res for newRoot in order[1:]: parent = parents[newRoot] dp2[newRoot] = composition(op(dp2[newRoot], dp2[parent]), parent, newRoot, 1) dp1[newRoot] = op(dp1[newRoot], dp2[newRoot]) return dp1 if __name__ == "__main__": n = int(input()) edges = [] for _ in range(n - 1): u, v = map(int, input().split()) edges.append((u - 1, v - 1)) E = int # 当前节点是否构成子树的最大匹配, 0: 不参与, 1: 参与 def e(root: int) -> E: return 0 def op(childRes1: E, childRes2: E) -> E: return childRes1 | childRes2 def composition(fromRes: E, parent: int, cur: int, direction: int) -> E: """direction: 0: cur -> parent, 1: parent -> cur""" return fromRes ^ 1 # 孩子参与匹配则父亲不参与, 反之成立 R = Rerooting(n) for u, v in edges: R.addEdge(u, v) dp = R.rerooting(e=e, op=op, composition=composition, root=0) res = [i for i in range(n) if dp[i] == 0] print(len(res)) for i in res: print(i + 1)