結果
問題 | No.763 Noelちゃんと木遊び |
ユーザー | rlangevin |
提出日時 | 2023-01-29 15:14:47 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 326 ms / 2,000 ms |
コード長 | 1,535 bytes |
コンパイル時間 | 151 ms |
コンパイル使用メモリ | 82,392 KB |
実行使用メモリ | 110,660 KB |
最終ジャッジ日時 | 2024-06-29 12:35:03 |
合計ジャッジ時間 | 6,178 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 194 ms
110,660 KB |
testcase_01 | AC | 163 ms
85,284 KB |
testcase_02 | AC | 253 ms
97,488 KB |
testcase_03 | AC | 192 ms
89,072 KB |
testcase_04 | AC | 178 ms
90,112 KB |
testcase_05 | AC | 192 ms
89,908 KB |
testcase_06 | AC | 306 ms
109,088 KB |
testcase_07 | AC | 310 ms
109,428 KB |
testcase_08 | AC | 196 ms
90,700 KB |
testcase_09 | AC | 157 ms
86,008 KB |
testcase_10 | AC | 121 ms
81,300 KB |
testcase_11 | AC | 326 ms
109,336 KB |
testcase_12 | AC | 290 ms
106,936 KB |
testcase_13 | AC | 271 ms
97,988 KB |
testcase_14 | AC | 273 ms
100,740 KB |
testcase_15 | AC | 199 ms
90,220 KB |
testcase_16 | AC | 110 ms
78,344 KB |
testcase_17 | AC | 200 ms
89,368 KB |
testcase_18 | AC | 324 ms
109,412 KB |
testcase_19 | AC | 312 ms
106,996 KB |
testcase_20 | AC | 304 ms
107,132 KB |
ソースコード
import sys readline = sys.stdin.readline from collections import deque def bfs(G, s, N): Q = deque([]) dist = [-1] * N par = [-1] * N sz = [1] * N dist[s] = 0 for u in G[s]: par[u] = s dist[u] = 1 sz[u] += sz[s] Q.append(u) while Q: u = Q.popleft() for v in G[u]: if dist[v] != -1: continue dist[v] = dist[u] + 1 par[v] = u sz[v] += sz[u] Q.append(v) return par def topsort(G): Q = deque() N = len(G) count = [0] * N for u in range(N): for v in G[u]: count[v] += 1 for u in range(N): if count[u] == 0: Q.append(u) anslst = [] while Q: u = Q.pop() anslst.append(u) for v in G[u]: count[v] -= 1 if count[v] == 0: Q.append(v) return anslst N = int(readline()) G = [[] for i in range(N)] a, b = [0] * (N - 1), [0] * (N - 1) for i in range(N - 1): a[i], b[i] = map(int, input().split()) a[i], b[i] = a[i] - 1, b[i] - 1 G[a[i]].append(b[i]) G[b[i]].append(a[i]) par = bfs(G, 0, N) G2 = [[] for i in range(N)] for i in range(N - 1): if par[a[i]] == b[i]: G2[a[i]].append(b[i]) else: G2[b[i]].append(a[i]) G = topsort(G2) dp0 = [0] * N dp1 = [1] * N for u in G: for v in G2[u]: dp0[v] += max(dp0[u], dp1[u]) dp1[v] += max(dp0[u], dp1[u] - 1) print(max(dp0[0], dp1[0]))