結果

問題 No.1928 Make a Binary Tree
ユーザー 👑 rin204rin204
提出日時 2022-05-06 21:45:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 989 ms / 3,000 ms
コード長 811 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 81,900 KB
実行使用メモリ 385,720 KB
最終ジャッジ日時 2024-07-05 22:57:05
合計ジャッジ時間 26,337 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 9)
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
from heapq import *

n = int(input())
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    edges[u].append(v)
    edges[v].append(u)

def dfs(pos, bpos):
    hq = []
    for npos in edges[pos]:
        if npos == bpos:
            continue
        else:
            tmp = dfs(npos, pos)
            if len(hq) > len(tmp):
                for t in tmp:
                    heappush(hq, t)
            else:
                for h in hq:
                    heappush(tmp, h)
                hq = tmp
    add = -1
    for _ in range(2):
        if hq:
            add += heappop(hq)
    heappush(hq, add)
    return hq

hq = dfs(0, -1)
print(-hq[0])
0