結果
問題 | No.1928 Make a Binary Tree |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:51:14 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,323 bytes |
コンパイル時間 | 326 ms |
コンパイル使用メモリ | 82,100 KB |
実行使用メモリ | 849,516 KB |
最終ジャッジ日時 | 2025-03-26 15:52:30 |
合計ジャッジ時間 | 26,629 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 3 WA * 32 MLE * 12 -- * 10 |
ソースコード
import sysfrom collections import dequedef main():sys.setrecursionlimit(1 << 25)N = int(sys.stdin.readline())if N == 1:print(1)return# Build adjacency listadj = [[] for _ in range(N + 1)]for _ in range(N - 1):x, y = map(int, sys.stdin.readline().split())adj[x].append(y)adj[y].append(x)# Build parent and children structure using BFSparent = [0] * (N + 1)children = [[] for _ in range(N + 1)]q = deque([1])parent[1] = -1 # mark root's parent as -1while q:u = q.popleft()for v in adj[u]:if parent[v] == 0 and v != parent[u]:parent[v] = uchildren[u].append(v)q.append(v)# Iterative post-order traversalstack = [(1, False)]dp = [0] * (N + 1)t1 = [0] * (N + 1)t2 = [0] * (N + 1)while stack:node, visited = stack.pop()if not visited:stack.append((node, True))# Push children in reverse order to process them in original orderfor child in reversed(children[node]):stack.append((child, False))else:max_a = max_b = 0# Collect t1 and t2 from all childrenfor child in children[node]:current_t1 = t1[child]current_t2 = t2[child]# Update max_a and max_b with current_t1if current_t1 > max_a:max_b = max_amax_a = current_t1elif current_t1 > max_b:max_b = current_t1# Update max_a and max_b with current_t2if current_t2 > max_a:max_b = max_amax_a = current_t2elif current_t2 > max_b:max_b = current_t2a = max_ab = max_bdp[node] = 1 + a + b# Compute t1 and t2 for the current nodeif dp[node] > a:new_t1 = dp[node]new_t2 = aelse:new_t1 = anew_t2 = max(b, dp[node])t1[node] = new_t1t2[node] = new_t2print(dp[1])if __name__ == "__main__":main()