結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-15 21:04:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,481 ms / 3,000 ms
コード長 1,086 bytes
コンパイル時間 250 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 111,616 KB
最終ジャッジ日時 2024-04-23 22:13:20
合計ジャッジ時間 44,753 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,880 KB
testcase_01 AC 31 ms
10,880 KB
testcase_02 AC 34 ms
11,008 KB
testcase_03 AC 31 ms
10,880 KB
testcase_04 AC 33 ms
10,752 KB
testcase_05 AC 31 ms
10,752 KB
testcase_06 AC 32 ms
10,880 KB
testcase_07 AC 31 ms
10,880 KB
testcase_08 AC 33 ms
10,880 KB
testcase_09 AC 31 ms
11,008 KB
testcase_10 AC 32 ms
10,880 KB
testcase_11 AC 31 ms
10,880 KB
testcase_12 AC 32 ms
10,880 KB
testcase_13 AC 31 ms
11,008 KB
testcase_14 AC 32 ms
10,880 KB
testcase_15 AC 84 ms
13,184 KB
testcase_16 AC 67 ms
12,544 KB
testcase_17 AC 57 ms
12,032 KB
testcase_18 AC 78 ms
12,928 KB
testcase_19 AC 38 ms
11,008 KB
testcase_20 AC 60 ms
12,160 KB
testcase_21 AC 47 ms
11,648 KB
testcase_22 AC 65 ms
12,544 KB
testcase_23 AC 56 ms
12,032 KB
testcase_24 AC 75 ms
12,928 KB
testcase_25 AC 86 ms
13,184 KB
testcase_26 AC 1,270 ms
54,784 KB
testcase_27 AC 759 ms
38,144 KB
testcase_28 AC 825 ms
40,576 KB
testcase_29 AC 1,479 ms
60,672 KB
testcase_30 AC 698 ms
35,968 KB
testcase_31 AC 938 ms
44,544 KB
testcase_32 AC 1,434 ms
59,136 KB
testcase_33 AC 778 ms
38,656 KB
testcase_34 AC 954 ms
44,928 KB
testcase_35 AC 1,056 ms
60,032 KB
testcase_36 AC 1,215 ms
87,040 KB
testcase_37 AC 1,077 ms
60,800 KB
testcase_38 AC 31 ms
10,880 KB
testcase_39 AC 1,322 ms
86,016 KB
testcase_40 AC 1,341 ms
86,272 KB
testcase_41 AC 1,351 ms
86,016 KB
testcase_42 AC 1,342 ms
86,144 KB
testcase_43 AC 1,335 ms
86,144 KB
testcase_44 AC 1,340 ms
86,144 KB
testcase_45 AC 1,339 ms
86,144 KB
testcase_46 AC 1,325 ms
86,144 KB
testcase_47 AC 1,337 ms
86,144 KB
testcase_48 AC 1,332 ms
86,272 KB
testcase_49 AC 1,159 ms
111,616 KB
testcase_50 AC 1,447 ms
59,392 KB
testcase_51 AC 1,448 ms
59,392 KB
testcase_52 AC 1,441 ms
59,520 KB
testcase_53 AC 1,442 ms
59,520 KB
testcase_54 AC 1,442 ms
59,392 KB
testcase_55 AC 1,125 ms
88,704 KB
testcase_56 AC 1,476 ms
60,800 KB
testcase_57 AC 1,479 ms
60,672 KB
testcase_58 AC 1,481 ms
60,800 KB
testcase_59 AC 1,301 ms
56,576 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:
      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]
    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