結果
| 問題 |
No.1928 Make a Binary Tree
|
| コンテスト | |
| ユーザー |
tnodino
|
| 提出日時 | 2022-05-08 14:59:44 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,142 bytes |
| コンパイル時間 | 165 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 106,880 KB |
| 最終ジャッジ日時 | 2024-07-08 03:01:27 |
| 合計ジャッジ時間 | 42,471 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 WA * 24 |
ソースコード
from sys import setrecursionlimit
setrecursionlimit(10**8)
from heapq import heappop,heappush
def dfs(pos, pre):
global G, Vec
for nxt in G[pos]:
if nxt == pre:
continue
P = dfs(nxt, pos)
heappush(Vec[pos], -P)
M = len(Vec[pos])
idx = pos
for nxt in G[pos]:
if nxt == pre:
continue
if len(Vec[nxt]) > M:
M = len(Vec[nxt])
idx = nxt
if idx != pos:
while Vec[pos]:
heappush(Vec[idx], heappop(Vec[pos]))
for nxt in G[pos]:
if nxt == pre:
continue
if nxt == idx:
continue
while Vec[nxt]:
heappush(Vec[pos], heappop(Vec[nxt]))
Vec[pos] = Vec[idx]
ret = 1
if len(Vec[pos]) <= 2:
while Vec[pos]:
ret += -heappop(Vec[pos])
else:
for _ in range(2):
ret += -heappop(Vec[pos])
return ret
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
x,y = map(int,input().split())
x -= 1
y -= 1
G[x].append(y)
G[y].append(x)
Vec = [[] for _ in range(N)]
print(dfs(0, -1))
tnodino