結果
問題 | No.1718 Random Squirrel |
ユーザー | 草苺奶昔 |
提出日時 | 2023-03-26 20:47:11 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,549 ms / 2,000 ms |
コード長 | 3,583 bytes |
コンパイル時間 | 81 ms |
コンパイル使用メモリ | 13,056 KB |
実行使用メモリ | 94,308 KB |
最終ジャッジ日時 | 2024-09-19 10:00:00 |
合計ジャッジ時間 | 23,544 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
11,904 KB |
testcase_01 | AC | 40 ms
11,904 KB |
testcase_02 | AC | 37 ms
12,032 KB |
testcase_03 | AC | 37 ms
11,776 KB |
testcase_04 | AC | 36 ms
11,904 KB |
testcase_05 | AC | 36 ms
11,776 KB |
testcase_06 | AC | 36 ms
11,648 KB |
testcase_07 | AC | 37 ms
12,032 KB |
testcase_08 | AC | 37 ms
11,776 KB |
testcase_09 | AC | 37 ms
11,904 KB |
testcase_10 | AC | 35 ms
11,904 KB |
testcase_11 | AC | 1,025 ms
66,872 KB |
testcase_12 | AC | 172 ms
22,528 KB |
testcase_13 | AC | 594 ms
45,768 KB |
testcase_14 | AC | 1,086 ms
69,172 KB |
testcase_15 | AC | 795 ms
61,648 KB |
testcase_16 | AC | 382 ms
35,552 KB |
testcase_17 | AC | 922 ms
63,860 KB |
testcase_18 | AC | 1,172 ms
79,556 KB |
testcase_19 | AC | 796 ms
59,732 KB |
testcase_20 | AC | 350 ms
32,128 KB |
testcase_21 | AC | 1,404 ms
90,432 KB |
testcase_22 | AC | 1,398 ms
94,308 KB |
testcase_23 | AC | 1,549 ms
90,288 KB |
testcase_24 | AC | 1,441 ms
90,304 KB |
testcase_25 | AC | 1,404 ms
94,052 KB |
testcase_26 | AC | 906 ms
90,248 KB |
testcase_27 | AC | 967 ms
89,828 KB |
testcase_28 | AC | 915 ms
90,248 KB |
testcase_29 | AC | 971 ms
92,712 KB |
testcase_30 | AC | 1,001 ms
94,152 KB |
testcase_31 | AC | 969 ms
92,508 KB |
testcase_32 | AC | 993 ms
92,572 KB |
ソースコード
from collections import defaultdict from typing import List, Tuple INF = int(1e18) def randomSquirrel(n: int, edges: List[Tuple[int, int]], sweets: List[int]) -> List[int]: E = Tuple[int, int] # (maxDist, mustVisitCount) def e(root: int) -> E: return (0, 0) if isSpecial[root] else (-INF, 0) def op(childRes1: E, childRes2: E) -> E: dist1, must1 = childRes1 dist2, must2 = childRes2 return (max(dist1, dist2), must1 + must2) def composition(fromRes: E, parent: int, cur: int, direction: int) -> E: """direction: 0: cur -> parent, 1: parent -> cur""" dist, must = fromRes w = weights[cur][parent] if direction == 0 else weights[parent][cur] # dist>=0 才开始算入必须经过的路径 return (dist + w, must + 1) if dist >= 0 else (dist, must) isSpecial = [False] * n for v in sweets: isSpecial[v] = True R = Rerooting(n) weights = [defaultdict(int) for _ in range(n)] for u, v in edges: R.addEdge(u, v) weights[u][v] = 1 weights[v][u] = 1 dp = R.rerooting(e=e, op=op, composition=composition, root=0) res = [mustVisit * 2 - maxDist for maxDist, mustVisit in dp] # 可以不回到出发点,2*点数-最大距离 return res 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, k = map(int, input().split()) edges = [] for _ in range(n - 1): u, v = map(int, input().split()) u, v = u - 1, v - 1 edges.append((u, v)) sweets = [int(x) - 1 for x in input().split()] print(*randomSquirrel(n, edges, sweets), sep="\n")