結果

問題 No.1928 Make a Binary Tree
ユーザー ygd.ygd.
提出日時 2022-05-07 00:27:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,410 ms / 3,000 ms
コード長 2,290 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 87,276 KB
実行使用メモリ 140,344 KB
最終ジャッジ日時 2023-09-20 08:24:45
合計ジャッジ時間 27,236 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,184 KB
testcase_01 AC 76 ms
71,328 KB
testcase_02 AC 76 ms
71,248 KB
testcase_03 AC 75 ms
71,244 KB
testcase_04 AC 77 ms
71,224 KB
testcase_05 AC 75 ms
71,128 KB
testcase_06 AC 75 ms
71,076 KB
testcase_07 AC 76 ms
71,376 KB
testcase_08 AC 77 ms
71,288 KB
testcase_09 AC 76 ms
71,284 KB
testcase_10 AC 76 ms
71,456 KB
testcase_11 AC 78 ms
71,244 KB
testcase_12 AC 75 ms
71,260 KB
testcase_13 AC 76 ms
71,444 KB
testcase_14 AC 75 ms
71,140 KB
testcase_15 AC 235 ms
80,876 KB
testcase_16 AC 246 ms
80,456 KB
testcase_17 AC 220 ms
79,068 KB
testcase_18 AC 232 ms
80,308 KB
testcase_19 AC 132 ms
77,868 KB
testcase_20 AC 219 ms
79,708 KB
testcase_21 AC 198 ms
78,804 KB
testcase_22 AC 227 ms
79,792 KB
testcase_23 AC 208 ms
79,776 KB
testcase_24 AC 231 ms
79,992 KB
testcase_25 AC 239 ms
81,184 KB
testcase_26 AC 740 ms
116,508 KB
testcase_27 AC 529 ms
101,204 KB
testcase_28 AC 557 ms
104,072 KB
testcase_29 AC 810 ms
120,788 KB
testcase_30 AC 490 ms
99,872 KB
testcase_31 AC 582 ms
108,232 KB
testcase_32 AC 789 ms
120,524 KB
testcase_33 AC 538 ms
101,852 KB
testcase_34 AC 591 ms
106,980 KB
testcase_35 AC 207 ms
106,780 KB
testcase_36 AC 296 ms
131,212 KB
testcase_37 AC 497 ms
123,588 KB
testcase_38 AC 75 ms
71,344 KB
testcase_39 AC 579 ms
116,388 KB
testcase_40 AC 563 ms
115,204 KB
testcase_41 AC 571 ms
116,108 KB
testcase_42 AC 573 ms
116,128 KB
testcase_43 AC 563 ms
115,368 KB
testcase_44 AC 563 ms
116,284 KB
testcase_45 AC 582 ms
115,596 KB
testcase_46 AC 582 ms
115,600 KB
testcase_47 AC 582 ms
115,508 KB
testcase_48 AC 568 ms
115,652 KB
testcase_49 AC 217 ms
116,932 KB
testcase_50 AC 499 ms
112,628 KB
testcase_51 AC 497 ms
112,444 KB
testcase_52 AC 496 ms
112,276 KB
testcase_53 AC 498 ms
112,364 KB
testcase_54 AC 490 ms
112,376 KB
testcase_55 AC 270 ms
126,500 KB
testcase_56 AC 1,410 ms
139,736 KB
testcase_57 AC 1,202 ms
133,644 KB
testcase_58 AC 1,068 ms
126,236 KB
testcase_59 AC 425 ms
140,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]
#dx8 = [1,1,0,-1,-1,-1,0,1]
#dy8 = [0,1,1,1,0,-1,-1,-1]


def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N-1):
        x,y = map(int,input().split())
        x -= 1; y -= 1
        G[x].append(y)
        G[y].append(x)
    
    ans = 0
    par = [-1]*N
    children = [[] for _ in range(N)]
    stack = []
    stack.append(~0)
    stack.append(0)
    while stack:
        v = stack.pop()
        if v >= 0:
            for u in G[v]:
                if par[v] == u: continue
                par[u] = v
                stack.append(~u)
                stack.append(u)
        else:
            v = ~v
            VPQ = children[v]
            #子を全てチェックする。
            for u in G[v]:
                if par[v] == u: continue
                if len(children[u]) > len(VPQ):
                    VPQ, children[u] = children[u], VPQ
                    while children[u]:
                        num = heappop(children[u])
                        heappush(VPQ,-num)
            #print("v",v,VPQ)
            #大きいほうから最大2個と自分を親につける。
            temp = 1
            for _ in range(2):
                if VPQ:
                    score = -1 * heappop(VPQ)
                    temp += score
            #print("v",v,"temp",temp)
            #根であれば終了
            if v == 0:
                print(temp);exit()
            #残ったものと、採用するものをすべて親につける必要があるのでその前VPQにまとめる
            heappush(VPQ,-temp)
            if children[par[v]] < VPQ:
                children[par[v]], VPQ = VPQ, children[par[v]]
            while VPQ:
                num = heappop(VPQ)
                heappush(children[par[v]], num)
            

                
if __name__ == '__main__':
    main()
0