結果

問題 No.1221 木 *= 3
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-03-19 15:03:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 305 ms / 2,000 ms
コード長 1,245 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 82,448 KB
実行使用メモリ 123,980 KB
最終ジャッジ日時 2024-04-29 08:01:31
合計ジャッジ時間 5,963 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,480 KB
testcase_01 AC 39 ms
52,352 KB
testcase_02 AC 41 ms
52,352 KB
testcase_03 AC 42 ms
52,224 KB
testcase_04 AC 39 ms
52,224 KB
testcase_05 AC 39 ms
52,480 KB
testcase_06 AC 38 ms
52,224 KB
testcase_07 AC 237 ms
112,532 KB
testcase_08 AC 240 ms
114,792 KB
testcase_09 AC 292 ms
120,516 KB
testcase_10 AC 299 ms
121,244 KB
testcase_11 AC 298 ms
121,524 KB
testcase_12 AC 242 ms
123,980 KB
testcase_13 AC 247 ms
122,868 KB
testcase_14 AC 256 ms
123,420 KB
testcase_15 AC 256 ms
123,936 KB
testcase_16 AC 256 ms
123,196 KB
testcase_17 AC 292 ms
120,888 KB
testcase_18 AC 302 ms
119,000 KB
testcase_19 AC 303 ms
117,608 KB
testcase_20 AC 292 ms
119,348 KB
testcase_21 AC 305 ms
115,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main1(n,a,b,uv):
  if n==1:
    return max(a[0],0)
  ki=[[] for _ in range(n)]
  for u,v in uv:
    u,v=u-1,v-1
    ki[u].append(v)
    ki[v].append(u)
  # 頂点vを消したときの最大値
  # 頂点vを消さないときの最大値
  tree_order=[]
  parent=[-1]*n
  todo=[[0,-1]]
  while todo:
    v,p=todo.pop()
    tree_order.append(v)
    parent[v]=p
    for nv in ki[v]:
      if nv==p:continue
      todo.append([nv,v])
  tree_order.reverse()
  ary=[[] for _ in range(n)]
  # ary[v]:頂点vが子から遷移してきた値[子のidx,子を消すときの最大,子を消さないときの最大]の配列
  ans=0
  for v in tree_order:
    p=parent[v]
    if len(ary[v])==0:
      ary[p].append([v,a[v],0])
    else:
      num1=a[v] # 頂点vを消す
      num2=0 # 頂点vを消さない
      for i,ai,bi in ary[v]:
        num1+=max(ai,bi)
        num2+=max(ai,bi+b[i]+b[v])
      if p>=0:
        ary[p].append([v,num1,num2])
      else:
        ans=max(num1,num2)
  return ans

import sys
input=sys.stdin.readline
if __name__=='__main__':
  n=int(input())
  a=list(map(int,input().split()))
  b=list(map(int,input().split()))
  uv=[list(map(int,input().split())) for _ in range(n-1)]
  ret1=main1(n,a,b,uv)
  print(ret1)
0