結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
11,008 KB
testcase_01 AC 28 ms
11,136 KB
testcase_02 AC 29 ms
11,008 KB
testcase_03 AC 26 ms
11,008 KB
testcase_04 AC 27 ms
11,008 KB
testcase_05 AC 26 ms
11,136 KB
testcase_06 AC 29 ms
11,008 KB
testcase_07 AC 26 ms
11,008 KB
testcase_08 AC 27 ms
11,136 KB
testcase_09 AC 27 ms
11,008 KB
testcase_10 AC 27 ms
11,008 KB
testcase_11 AC 27 ms
11,136 KB
testcase_12 AC 27 ms
11,136 KB
testcase_13 AC 26 ms
11,008 KB
testcase_14 AC 25 ms
11,008 KB
testcase_15 AC 71 ms
13,440 KB
testcase_16 AC 59 ms
12,672 KB
testcase_17 AC 49 ms
12,288 KB
testcase_18 AC 68 ms
13,184 KB
testcase_19 AC 32 ms
11,264 KB
testcase_20 AC 52 ms
12,288 KB
testcase_21 AC 40 ms
11,648 KB
testcase_22 AC 57 ms
12,544 KB
testcase_23 AC 48 ms
12,032 KB
testcase_24 AC 67 ms
13,184 KB
testcase_25 AC 74 ms
13,440 KB
testcase_26 AC 1,110 ms
55,936 KB
testcase_27 AC 644 ms
39,168 KB
testcase_28 AC 735 ms
41,728 KB
testcase_29 AC 1,285 ms
62,592 KB
testcase_30 AC 582 ms
37,120 KB
testcase_31 AC 792 ms
45,696 KB
testcase_32 AC 1,237 ms
60,800 KB
testcase_33 AC 654 ms
39,680 KB
testcase_34 AC 797 ms
45,824 KB
testcase_35 AC 921 ms
66,816 KB
testcase_36 AC 1,156 ms
94,208 KB
testcase_37 AC 978 ms
62,332 KB
testcase_38 AC 26 ms
11,136 KB
testcase_39 AC 1,156 ms
73,600 KB
testcase_40 AC 1,178 ms
73,600 KB
testcase_41 AC 1,144 ms
73,600 KB
testcase_42 AC 1,135 ms
73,600 KB
testcase_43 AC 1,145 ms
73,728 KB
testcase_44 AC 1,155 ms
73,600 KB
testcase_45 AC 1,153 ms
73,728 KB
testcase_46 AC 1,175 ms
73,728 KB
testcase_47 AC 1,156 ms
73,600 KB
testcase_48 AC 1,176 ms
73,600 KB
testcase_49 AC 1,039 ms
84,480 KB
testcase_50 AC 1,274 ms
61,184 KB
testcase_51 AC 1,292 ms
61,184 KB
testcase_52 AC 1,281 ms
61,312 KB
testcase_53 AC 1,270 ms
61,184 KB
testcase_54 AC 1,286 ms
61,184 KB
testcase_55 AC 1,109 ms
93,568 KB
testcase_56 AC 1,302 ms
62,592 KB
testcase_57 AC 1,327 ms
62,464 KB
testcase_58 AC 1,311 ms
62,336 KB
testcase_59 AC 1,412 ms
97,280 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)]
  dq=deque()
  dq.append([0+N,-1])
  dq.append([0,-1])
  cnt=[1]*N
  while dq:
    i,parent=dq.pop()
    if i<N:
      checked[i]=True
      for to in Graph[i]:
        if not checked[to]:
          dq.append([to+N,i])
          dq.append([to,i])
    else:
      i-=N
      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]
      if len(vec[i])<=2:
        for j in vec[i]:
          cnt[i]+=-j
        vec[i].clear()
      else:
        for j in range(2):
          cnt[i]+=-heappop(vec[i])
      assert(cnt[i]>=1)
      if parent!=-1:
        heappush(vec[parent],-cnt[i])
  print(cnt[0])
  return

main()
0