結果

問題 No.1928 Make a Binary Tree
ユーザー harurunharurun
提出日時 2021-12-15 21:29:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 682 ms / 3,000 ms
コード長 1,322 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 82,100 KB
実行使用メモリ 158,392 KB
最終ジャッジ日時 2024-04-23 22:15:18
合計ジャッジ時間 20,973 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,748 KB
testcase_01 AC 39 ms
54,820 KB
testcase_02 AC 38 ms
55,124 KB
testcase_03 AC 38 ms
56,188 KB
testcase_04 AC 38 ms
55,476 KB
testcase_05 AC 36 ms
55,392 KB
testcase_06 AC 38 ms
55,092 KB
testcase_07 AC 48 ms
56,316 KB
testcase_08 AC 37 ms
55,444 KB
testcase_09 AC 39 ms
55,432 KB
testcase_10 AC 39 ms
56,404 KB
testcase_11 AC 39 ms
54,988 KB
testcase_12 AC 38 ms
54,920 KB
testcase_13 AC 39 ms
54,928 KB
testcase_14 AC 39 ms
55,820 KB
testcase_15 AC 173 ms
80,204 KB
testcase_16 AC 164 ms
79,840 KB
testcase_17 AC 157 ms
79,360 KB
testcase_18 AC 188 ms
81,340 KB
testcase_19 AC 86 ms
77,032 KB
testcase_20 AC 152 ms
79,668 KB
testcase_21 AC 113 ms
78,424 KB
testcase_22 AC 156 ms
79,604 KB
testcase_23 AC 133 ms
79,104 KB
testcase_24 AC 174 ms
79,976 KB
testcase_25 AC 177 ms
80,764 KB
testcase_26 AC 599 ms
110,084 KB
testcase_27 AC 419 ms
97,868 KB
testcase_28 AC 426 ms
99,508 KB
testcase_29 AC 630 ms
114,352 KB
testcase_30 AC 354 ms
95,560 KB
testcase_31 AC 435 ms
102,240 KB
testcase_32 AC 636 ms
113,532 KB
testcase_33 AC 440 ms
97,976 KB
testcase_34 AC 478 ms
102,600 KB
testcase_35 AC 248 ms
109,120 KB
testcase_36 AC 357 ms
135,384 KB
testcase_37 AC 366 ms
115,104 KB
testcase_38 AC 39 ms
55,384 KB
testcase_39 AC 522 ms
120,664 KB
testcase_40 AC 541 ms
120,804 KB
testcase_41 AC 514 ms
121,080 KB
testcase_42 AC 512 ms
120,684 KB
testcase_43 AC 540 ms
120,992 KB
testcase_44 AC 528 ms
120,700 KB
testcase_45 AC 519 ms
120,740 KB
testcase_46 AC 527 ms
120,720 KB
testcase_47 AC 548 ms
120,908 KB
testcase_48 AC 539 ms
121,056 KB
testcase_49 AC 287 ms
126,204 KB
testcase_50 AC 507 ms
109,144 KB
testcase_51 AC 488 ms
109,648 KB
testcase_52 AC 524 ms
109,632 KB
testcase_53 AC 509 ms
109,444 KB
testcase_54 AC 486 ms
109,820 KB
testcase_55 AC 310 ms
135,116 KB
testcase_56 AC 682 ms
114,580 KB
testcase_57 AC 680 ms
114,096 KB
testcase_58 AC 674 ms
114,680 KB
testcase_59 AC 461 ms
158,392 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