結果
問題 | No.2214 Products on Tree |
ユーザー | Shirotsume |
提出日時 | 2022-12-22 01:50:08 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 714 ms / 3,000 ms |
コード長 | 1,473 bytes |
コンパイル時間 | 238 ms |
コンパイル使用メモリ | 82,428 KB |
実行使用メモリ | 198,272 KB |
最終ジャッジ日時 | 2024-11-18 03:12:48 |
合計ジャッジ時間 | 17,101 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
54,208 KB |
testcase_01 | AC | 36 ms
53,932 KB |
testcase_02 | AC | 35 ms
54,316 KB |
testcase_03 | AC | 661 ms
181,500 KB |
testcase_04 | AC | 687 ms
181,396 KB |
testcase_05 | AC | 622 ms
175,312 KB |
testcase_06 | AC | 439 ms
143,432 KB |
testcase_07 | AC | 175 ms
95,360 KB |
testcase_08 | AC | 128 ms
87,892 KB |
testcase_09 | AC | 204 ms
102,716 KB |
testcase_10 | AC | 428 ms
141,848 KB |
testcase_11 | AC | 257 ms
111,496 KB |
testcase_12 | AC | 281 ms
118,728 KB |
testcase_13 | AC | 712 ms
182,492 KB |
testcase_14 | AC | 714 ms
180,216 KB |
testcase_15 | AC | 713 ms
182,316 KB |
testcase_16 | AC | 705 ms
180,736 KB |
testcase_17 | AC | 700 ms
180,392 KB |
testcase_18 | AC | 399 ms
151,904 KB |
testcase_19 | AC | 412 ms
155,740 KB |
testcase_20 | AC | 392 ms
152,172 KB |
testcase_21 | AC | 324 ms
131,476 KB |
testcase_22 | AC | 548 ms
182,760 KB |
testcase_23 | AC | 35 ms
54,380 KB |
testcase_24 | AC | 35 ms
54,008 KB |
testcase_25 | AC | 35 ms
54,168 KB |
testcase_26 | AC | 35 ms
54,096 KB |
testcase_27 | AC | 34 ms
54,320 KB |
testcase_28 | AC | 267 ms
151,284 KB |
testcase_29 | AC | 161 ms
101,284 KB |
testcase_30 | AC | 350 ms
186,848 KB |
testcase_31 | AC | 614 ms
186,640 KB |
testcase_32 | AC | 528 ms
177,744 KB |
testcase_33 | AC | 366 ms
198,272 KB |
testcase_34 | AC | 365 ms
198,272 KB |
testcase_35 | AC | 595 ms
181,112 KB |
testcase_36 | AC | 586 ms
180,908 KB |
testcase_37 | AC | 350 ms
179,184 KB |
ソースコード
import sys from collections import deque, Counter sys.setrecursionlimit(5 * 10 ** 5) from pypyjit import set_param set_param('max_unroll_recursion=-1') input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 def CalcDepth(s, graph): INF = 2 ** 63 - 1 from collections import deque n = len(graph) depth = [INF] * n depth[s] = 0 q = deque() q.append(s) while q: now = q.popleft() for to in graph[now]: if depth[to] == INF: depth[to] = depth[now] + 1 q.append(to) return depth def solve(n, AB): dp = [[0] * 2 for _ in range(n)] s = set() graph = [[] for _ in range(n)] for a, b in AB: graph[a - 1].append(b - 1) graph[b - 1].append(a - 1) d = CalcDepth(0, graph) l = list(range(n)) l.sort(key = lambda x: d[x], reverse=True) for now in l: dp[now][0] = 1 dp[now][1] = 1 s.add(now) for to in graph[now]: if to in s: nc = [0, 0] nc[0] = dp[now][0] * (dp[to][0] + dp[to][1]) nc[0] %= mod nc[1] = dp[now][0] * dp[to][1] + dp[now][1] * (dp[to][0] + dp[to][1]) nc[1] %= mod dp[now] = nc return dp[0][1] n = ii() AB = [li() for _ in range(n - 1)] print(solve(n, AB))