結果

問題 No.1221 木 *= 3
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-03-19 14:57:21
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,280 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,732 KB
実行使用メモリ 124,184 KB
最終ジャッジ日時 2024-04-29 07:56:40
合計ジャッジ時間 6,640 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,224 KB
testcase_01 AC 41 ms
51,840 KB
testcase_02 AC 41 ms
51,968 KB
testcase_03 AC 41 ms
52,352 KB
testcase_04 WA -
testcase_05 AC 38 ms
52,480 KB
testcase_06 AC 38 ms
51,968 KB
testcase_07 AC 225 ms
112,348 KB
testcase_08 AC 221 ms
114,340 KB
testcase_09 AC 274 ms
122,176 KB
testcase_10 AC 275 ms
122,408 KB
testcase_11 AC 293 ms
122,612 KB
testcase_12 AC 238 ms
124,184 KB
testcase_13 AC 228 ms
123,556 KB
testcase_14 AC 250 ms
123,352 KB
testcase_15 AC 248 ms
123,512 KB
testcase_16 AC 254 ms
123,492 KB
testcase_17 AC 282 ms
121,724 KB
testcase_18 AC 283 ms
120,432 KB
testcase_19 AC 294 ms
118,292 KB
testcase_20 AC 288 ms
120,568 KB
testcase_21 AC 306 ms
116,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main1(n,a,b,uv):
  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,子を消すときの最大,子を消さないときの最大]の配列
  dp1=[0]*n
  # dp1[v]:頂点vを根とした部分木に対する解
  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