結果

問題 No.1928 Make a Binary Tree
ユーザー ikomaikoma
提出日時 2022-05-06 22:52:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,138 bytes
コンパイル時間 424 ms
コンパイル使用メモリ 10,936 KB
実行使用メモリ 83,368 KB
最終ジャッジ日時 2023-09-20 04:08:39
合計ジャッジ時間 14,781 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
12,880 KB
testcase_01 AC 17 ms
8,380 KB
testcase_02 AC 17 ms
8,328 KB
testcase_03 AC 17 ms
8,208 KB
testcase_04 AC 17 ms
8,316 KB
testcase_05 AC 17 ms
8,268 KB
testcase_06 WA -
testcase_07 AC 17 ms
8,276 KB
testcase_08 AC 17 ms
8,324 KB
testcase_09 AC 17 ms
8,284 KB
testcase_10 AC 17 ms
8,428 KB
testcase_11 AC 17 ms
8,308 KB
testcase_12 AC 17 ms
8,328 KB
testcase_13 WA -
testcase_14 AC 17 ms
8,324 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 797 ms
82,720 KB
testcase_36 TLE -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)


N=int(input())
edge = [[] for _ in range(N)]
XY=[list(map(int,input().split())) for _ in range(N-1)]
for x,y in XY:
    x,y=x-1,y-1
    edge[x].append(y)
    edge[y].append(x)
visit = [0]*N

dnode = []
from heapq import *

def dfs(v=0):
    visit[v]=1
    n_tree = []
    del_lst = []
    for to in edge[v]:
        if visit[to]:continue
        n,lst = dfs(to)
        n_tree.append(n)
        del_lst.extend(lst)
    if len(n_tree)==0:
        return 1,[]
    if len(n_tree)==1:
        if del_lst:
            # heapify(del_lst)
            n = -heappop(del_lst)
        else: n=0
        return n+n_tree[0]+1,del_lst
    if len(n_tree)<=2:
        return sum(n_tree)+1,del_lst
    # n_tree.sort(reverse=True) # 重い
    mx1,mx2=0,0
    for n in n_tree:
        if mx1<n:
            mx2=mx1
            mx1=n
        elif mx2<n:
            mx2=n
    ret = mx1+mx2+1
    for i in n_tree:
        if i!=mx1:
            if i!=mx2:
                heappush(del_lst, -i)
            else:mx2=-1
        else:mx1=-1
    return ret,del_lst

ans,dl = dfs()
print(ans)
0