結果
問題 | No.1928 Make a Binary Tree |
ユーザー |
![]() |
提出日時 | 2021-12-15 21:29:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 867 ms / 3,000 ms |
コード長 | 1,322 bytes |
コンパイル時間 | 254 ms |
コンパイル使用メモリ | 82,920 KB |
実行使用メモリ | 158,516 KB |
最終ジャッジ日時 | 2024-11-06 04:31:38 |
合計ジャッジ時間 | 25,518 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
from collections import dequefrom heapq import *from sys import setrecursionlimitsetrecursionlimit(400010)def main():N=int(input())Graph=[[]for i in range(N)]for i in range(N-1):x,y=map(int,input().split())Graph[x-1].append(y-1)Graph[y-1].append(x-1)checked=[False]*Nvec=[[]for i in range(N)]dq=deque()dq.append([0+N,-1])dq.append([0,-1])cnt=[1]*Nwhile dq:i,parent=dq.pop()if i<N:checked[i]=Truefor to in Graph[i]:if not checked[to]:dq.append([to+N,i])dq.append([to,i])else:i-=Nmax_size=len(vec[i])index=ifor to in Graph[i]:if to!=parent and len(vec[to])>max_size:index=tomax_size=len(vec[to])if index!=i:for j in vec[i]:heappush(vec[index],j)vec[i].clear()for to in Graph[i]:if index!=to and to!=parent:for j in vec[to]:heappush(vec[index],j)vec[to].clear()vec[i]=vec[index]if len(vec[i])<=2:for j in vec[i]:cnt[i]+=-jvec[i].clear()else:for j in range(2):cnt[i]+=-heappop(vec[i])assert(cnt[i]>=1)if parent!=-1:heappush(vec[parent],-cnt[i])print(cnt[0])returnmain()