結果
問題 |
No.1752 Up-Down Tree
|
ユーザー |
![]() |
提出日時 | 2025-04-24 12:28:24 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,277 bytes |
コンパイル時間 | 199 ms |
コンパイル使用メモリ | 82,272 KB |
実行使用メモリ | 102,700 KB |
最終ジャッジ日時 | 2025-04-24 12:30:39 |
合計ジャッジ時間 | 5,828 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 1 WA * 19 |
ソースコード
from collections import deque n = int(input()) adj = [[] for _ in range(n + 1)] for _ in range(n - 1): a, b = map(int, input().split()) adj[a].append(b) adj[b].append(a) # Build parent and children structure parent = [0] * (n + 1) children = [[] for _ in range(n + 1)] visited = [False] * (n + 1) q = deque([1]) visited[1] = True parent[1] = 0 while q: u = q.popleft() for v in adj[u]: if not visited[v] and v != parent[u]: parent[v] = u children[u].append(v) visited[v] = True q.append(v) # Compute child count for each node child_count = [0] * (n + 1) for u in range(1, n + 1): child_count[u] = len(children[u]) # Compute up_sum using BFS starting from root up_sum = [0] * (n + 1) q = deque([1]) up_sum[1] = child_count[1] - 1 # root's up_sum is children count -1 # Process nodes in BFS order to ensure parents are processed before children while q: u = q.popleft() for v in children[u]: up_sum[v] = (child_count[v] - 1) + up_sum[u] q.append(v) # Calculate the maximum value max_val = 1 # root's value is 1 for u in range(2, n + 1): p = parent[u] current_val = 1 + 2 * up_sum[p] if current_val > max_val: max_val = current_val print(max_val)