結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
11,008 KB
testcase_01 AC 28 ms
10,880 KB
testcase_02 AC 27 ms
10,880 KB
testcase_03 AC 26 ms
10,880 KB
testcase_04 WA -
testcase_05 AC 26 ms
10,880 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 1,025 ms
87,040 KB
testcase_37 WA -
testcase_38 AC 26 ms
10,880 KB
testcase_39 AC 1,335 ms
86,144 KB
testcase_40 AC 1,133 ms
86,016 KB
testcase_41 AC 1,147 ms
86,016 KB
testcase_42 AC 1,136 ms
86,144 KB
testcase_43 AC 1,117 ms
86,144 KB
testcase_44 AC 1,113 ms
86,144 KB
testcase_45 AC 1,118 ms
86,144 KB
testcase_46 AC 1,136 ms
86,016 KB
testcase_47 AC 1,102 ms
86,144 KB
testcase_48 AC 1,119 ms
86,016 KB
testcase_49 AC 1,000 ms
111,488 KB
testcase_50 AC 1,218 ms
59,392 KB
testcase_51 AC 1,227 ms
59,520 KB
testcase_52 AC 1,209 ms
59,520 KB
testcase_53 AC 1,237 ms
59,520 KB
testcase_54 AC 1,231 ms
59,392 KB
testcase_55 AC 960 ms
88,704 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 1,090 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:
      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