結果

問題 No.1221 木 *= 3
ユーザー marroncastle
提出日時 2020-09-13 04:11:01
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 494 ms / 2,000 ms
コード長 881 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 46,988 KB
最終ジャッジ日時 2025-01-02 20:10:55
合計ジャッジ時間 8,487 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
edge = [[] for _ in range(N)]
for i in range(N-1):
  a,b = map(int, input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)

def dfs2(start):
  stack = [start]
  parent = [N]*N
  parent[start] = -1
  offs = A[:]
  ons = [0]*N
  while stack:
    v = stack[-1]
    marker = 0
    for u in edge[v]:
      if u==parent[v]:
        continue
      if parent[u]==N: #前半
        marker = 1
        parent[u]=v
        stack.append(u)
      else: #後半
        offs[v] += offs[u]
        ons[v] += ons[u]
    if marker==0: #後半だったら
      stack.pop()
      if v==start:
        continue
      offs[v], ons[v] = max(offs[v],ons[v]), max(offs[v],ons[v]+B[v]+B[parent[v]])
  return offs[0], ons[0]

off2, on2 = dfs2(0)
print(max(off2,on2))

0