結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
11,008 KB
testcase_01 AC 27 ms
11,008 KB
testcase_02 AC 26 ms
11,008 KB
testcase_03 WA -
testcase_04 AC 27 ms
11,008 KB
testcase_05 AC 25 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 WA -
testcase_37 AC 957 ms
60,672 KB
testcase_38 AC 27 ms
10,880 KB
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 AC 1,331 ms
60,816 KB
testcase_51 AC 1,348 ms
60,816 KB
testcase_52 AC 1,336 ms
61,108 KB
testcase_53 AC 1,308 ms
60,836 KB
testcase_54 AC 1,310 ms
61,080 KB
testcase_55 AC 984 ms
88,704 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 1,093 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:
      for j in vec[i]:
        res+=-j
    else:
      for j in range(2):
        res+=-heappop(vec[i])
    return res
  ans=dfs(0,-1)
  print(ans)
  return

main()
0