結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
11,008 KB
testcase_01 AC 27 ms
11,008 KB
testcase_02 AC 26 ms
10,880 KB
testcase_03 AC 26 ms
10,880 KB
testcase_04 AC 25 ms
11,008 KB
testcase_05 AC 26 ms
10,880 KB
testcase_06 AC 27 ms
10,752 KB
testcase_07 AC 27 ms
10,880 KB
testcase_08 AC 28 ms
10,880 KB
testcase_09 AC 29 ms
11,008 KB
testcase_10 AC 27 ms
10,880 KB
testcase_11 AC 26 ms
10,880 KB
testcase_12 AC 26 ms
10,752 KB
testcase_13 AC 28 ms
11,008 KB
testcase_14 AC 29 ms
10,880 KB
testcase_15 AC 71 ms
13,312 KB
testcase_16 AC 60 ms
12,544 KB
testcase_17 AC 51 ms
12,032 KB
testcase_18 AC 70 ms
13,056 KB
testcase_19 AC 34 ms
11,136 KB
testcase_20 AC 53 ms
12,160 KB
testcase_21 AC 39 ms
11,648 KB
testcase_22 AC 57 ms
12,544 KB
testcase_23 AC 48 ms
12,032 KB
testcase_24 AC 64 ms
13,056 KB
testcase_25 AC 75 ms
13,312 KB
testcase_26 AC 1,135 ms
54,656 KB
testcase_27 AC 643 ms
38,016 KB
testcase_28 AC 709 ms
40,448 KB
testcase_29 AC 1,301 ms
60,800 KB
testcase_30 AC 594 ms
35,968 KB
testcase_31 AC 770 ms
44,416 KB
testcase_32 AC 1,235 ms
59,136 KB
testcase_33 AC 657 ms
38,528 KB
testcase_34 AC 826 ms
44,800 KB
testcase_35 AC 956 ms
60,032 KB
testcase_36 AC 1,094 ms
87,168 KB
testcase_37 AC 974 ms
60,672 KB
testcase_38 AC 27 ms
11,008 KB
testcase_39 AC 1,356 ms
86,016 KB
testcase_40 AC 1,172 ms
86,144 KB
testcase_41 AC 1,209 ms
86,144 KB
testcase_42 AC 1,187 ms
86,272 KB
testcase_43 AC 1,206 ms
86,144 KB
testcase_44 AC 1,177 ms
86,144 KB
testcase_45 AC 1,175 ms
86,144 KB
testcase_46 AC 1,182 ms
86,144 KB
testcase_47 AC 1,131 ms
86,016 KB
testcase_48 AC 1,187 ms
86,016 KB
testcase_49 AC 1,052 ms
111,488 KB
testcase_50 AC 1,274 ms
59,520 KB
testcase_51 AC 1,241 ms
59,392 KB
testcase_52 AC 1,235 ms
59,520 KB
testcase_53 AC 1,226 ms
59,520 KB
testcase_54 AC 1,237 ms
59,520 KB
testcase_55 AC 990 ms
88,704 KB
testcase_56 AC 1,278 ms
60,800 KB
testcase_57 AC 1,292 ms
60,672 KB
testcase_58 AC 1,322 ms
61,056 KB
testcase_59 AC 1,099 ms
56,320 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:
      for j in vec[i]:
        res+=-j
      vec[i].clear()
    else:
      for j in range(2):
        res+=-heappop(vec[i])
    return res
  ans=dfs(0,-1)
  print(ans)
  return

main()
0