結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-15 20:58:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,547 ms / 3,000 ms
コード長 1,064 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 114,688 KB
最終ジャッジ日時 2024-04-23 22:10:48
合計ジャッジ時間 45,466 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,752 KB
testcase_01 AC 31 ms
11,008 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 AC 32 ms
10,880 KB
testcase_04 AC 31 ms
10,880 KB
testcase_05 AC 31 ms
10,880 KB
testcase_06 AC 31 ms
10,880 KB
testcase_07 AC 33 ms
10,880 KB
testcase_08 AC 31 ms
10,880 KB
testcase_09 AC 31 ms
10,880 KB
testcase_10 AC 31 ms
10,880 KB
testcase_11 AC 32 ms
10,880 KB
testcase_12 AC 31 ms
10,752 KB
testcase_13 AC 32 ms
10,880 KB
testcase_14 AC 31 ms
11,008 KB
testcase_15 AC 81 ms
13,184 KB
testcase_16 AC 69 ms
12,544 KB
testcase_17 AC 58 ms
12,160 KB
testcase_18 AC 79 ms
13,184 KB
testcase_19 AC 37 ms
11,136 KB
testcase_20 AC 60 ms
12,160 KB
testcase_21 AC 45 ms
11,520 KB
testcase_22 AC 66 ms
12,416 KB
testcase_23 AC 57 ms
12,032 KB
testcase_24 AC 77 ms
12,928 KB
testcase_25 AC 86 ms
13,184 KB
testcase_26 AC 1,312 ms
54,784 KB
testcase_27 AC 778 ms
38,144 KB
testcase_28 AC 849 ms
40,320 KB
testcase_29 AC 1,516 ms
60,800 KB
testcase_30 AC 710 ms
35,968 KB
testcase_31 AC 957 ms
44,544 KB
testcase_32 AC 1,472 ms
59,136 KB
testcase_33 AC 796 ms
38,784 KB
testcase_34 AC 965 ms
44,800 KB
testcase_35 AC 1,041 ms
60,144 KB
testcase_36 AC 1,204 ms
88,576 KB
testcase_37 AC 1,147 ms
60,416 KB
testcase_38 AC 31 ms
11,008 KB
testcase_39 AC 1,337 ms
87,936 KB
testcase_40 AC 1,352 ms
87,680 KB
testcase_41 AC 1,348 ms
87,680 KB
testcase_42 AC 1,345 ms
87,680 KB
testcase_43 AC 1,337 ms
87,680 KB
testcase_44 AC 1,331 ms
87,936 KB
testcase_45 AC 1,345 ms
87,680 KB
testcase_46 AC 1,329 ms
87,808 KB
testcase_47 AC 1,345 ms
87,808 KB
testcase_48 AC 1,359 ms
87,808 KB
testcase_49 AC 1,152 ms
114,688 KB
testcase_50 AC 1,453 ms
59,392 KB
testcase_51 AC 1,453 ms
59,520 KB
testcase_52 AC 1,441 ms
59,392 KB
testcase_53 AC 1,437 ms
59,392 KB
testcase_54 AC 1,447 ms
59,520 KB
testcase_55 AC 1,131 ms
90,112 KB
testcase_56 AC 1,534 ms
60,928 KB
testcase_57 AC 1,511 ms
60,672 KB
testcase_58 AC 1,547 ms
60,928 KB
testcase_59 AC 1,289 ms
56,448 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