結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-15 20:59:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,187 ms / 3,000 ms
コード長 1,064 bytes
コンパイル時間 367 ms
コンパイル使用メモリ 82,024 KB
実行使用メモリ 413,436 KB
最終ジャッジ日時 2024-04-23 22:11:37
合計ジャッジ時間 34,346 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
54,336 KB
testcase_01 AC 44 ms
55,548 KB
testcase_02 AC 45 ms
55,196 KB
testcase_03 AC 44 ms
54,488 KB
testcase_04 AC 45 ms
54,908 KB
testcase_05 AC 48 ms
54,400 KB
testcase_06 AC 45 ms
55,536 KB
testcase_07 AC 48 ms
54,572 KB
testcase_08 AC 48 ms
55,956 KB
testcase_09 AC 47 ms
55,508 KB
testcase_10 AC 48 ms
55,200 KB
testcase_11 AC 50 ms
54,804 KB
testcase_12 AC 45 ms
54,620 KB
testcase_13 AC 47 ms
55,540 KB
testcase_14 AC 45 ms
55,196 KB
testcase_15 AC 213 ms
80,480 KB
testcase_16 AC 206 ms
79,696 KB
testcase_17 AC 188 ms
79,280 KB
testcase_18 AC 208 ms
80,168 KB
testcase_19 AC 102 ms
77,136 KB
testcase_20 AC 180 ms
79,080 KB
testcase_21 AC 147 ms
78,420 KB
testcase_22 AC 200 ms
79,504 KB
testcase_23 AC 173 ms
79,628 KB
testcase_24 AC 211 ms
80,160 KB
testcase_25 AC 220 ms
80,316 KB
testcase_26 AC 849 ms
114,944 KB
testcase_27 AC 601 ms
100,948 KB
testcase_28 AC 641 ms
102,780 KB
testcase_29 AC 942 ms
120,020 KB
testcase_30 AC 567 ms
98,656 KB
testcase_31 AC 691 ms
106,024 KB
testcase_32 AC 945 ms
118,896 KB
testcase_33 AC 611 ms
101,436 KB
testcase_34 AC 737 ms
107,196 KB
testcase_35 AC 355 ms
130,676 KB
testcase_36 AC 841 ms
275,824 KB
testcase_37 AC 541 ms
117,572 KB
testcase_38 AC 46 ms
54,056 KB
testcase_39 AC 1,187 ms
272,808 KB
testcase_40 AC 1,176 ms
274,348 KB
testcase_41 AC 1,152 ms
268,136 KB
testcase_42 AC 1,144 ms
273,940 KB
testcase_43 AC 1,125 ms
268,532 KB
testcase_44 AC 1,150 ms
271,756 KB
testcase_45 AC 1,147 ms
274,576 KB
testcase_46 AC 1,160 ms
272,640 KB
testcase_47 AC 1,142 ms
275,140 KB
testcase_48 AC 1,142 ms
275,464 KB
testcase_49 AC 935 ms
413,436 KB
testcase_50 AC 736 ms
114,800 KB
testcase_51 AC 743 ms
115,016 KB
testcase_52 AC 746 ms
115,248 KB
testcase_53 AC 738 ms
114,984 KB
testcase_54 AC 732 ms
114,896 KB
testcase_55 AC 845 ms
269,260 KB
testcase_56 AC 1,053 ms
121,284 KB
testcase_57 AC 1,013 ms
121,384 KB
testcase_58 AC 1,028 ms
120,928 KB
testcase_59 AC 500 ms
123,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
from heapq import *
from sys import setrecursionlimit
setrecursionlimit(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]*N
  vec=[[]for i in range(N)]
  def dfs(i,parent):
    checked[i]=True
    for to in Graph[i]:
      if not checked[to]:
        p=dfs(to,i)
        heappush(vec[i],-p)
    max_size=len(vec[i])
    index=i
    for to in Graph[i]:
      if to!=parent and len(vec[to])>max_size:
        index=to
        max_size=len(vec[to])
    if index!=i:
      while vec[i]:
        heappush(vec[index],heappop(vec[i]))
    for to in Graph[i]:
      if index!=to and to!=parent:
        while vec[to]:
          heappush(vec[index],heappop(vec[to]))
    vec[i]=vec[index]
    res=1
    if len(vec[i])<=2:
      while vec[i]:
        res+=-heappop(vec[i])
    else:
      for j in range(2):
        res+=-heappop(vec[i])
    return res
  ans=dfs(0,-1)
  print(ans)
  return

main()
0