結果

問題 No.1221 木 *= 3
ユーザー marroncastlemarroncastle
提出日時 2020-09-13 04:11:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 501 ms / 2,000 ms
コード長 881 bytes
コンパイル時間 73 ms
コンパイル使用メモリ 10,836 KB
実行使用メモリ 45,024 KB
最終ジャッジ日時 2023-08-30 22:14:29
合計ジャッジ時間 8,624 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,868 KB
testcase_01 AC 15 ms
7,812 KB
testcase_02 AC 16 ms
7,916 KB
testcase_03 AC 17 ms
7,820 KB
testcase_04 AC 16 ms
7,856 KB
testcase_05 AC 17 ms
7,848 KB
testcase_06 AC 16 ms
7,772 KB
testcase_07 AC 482 ms
43,580 KB
testcase_08 AC 472 ms
45,024 KB
testcase_09 AC 488 ms
42,988 KB
testcase_10 AC 480 ms
42,960 KB
testcase_11 AC 479 ms
43,040 KB
testcase_12 AC 433 ms
36,644 KB
testcase_13 AC 443 ms
39,824 KB
testcase_14 AC 445 ms
38,480 KB
testcase_15 AC 449 ms
38,732 KB
testcase_16 AC 451 ms
37,172 KB
testcase_17 AC 491 ms
37,584 KB
testcase_18 AC 489 ms
41,396 KB
testcase_19 AC 492 ms
39,000 KB
testcase_20 AC 487 ms
39,000 KB
testcase_21 AC 501 ms
39,076 KB
権限があれば一括ダウンロードができます

ソースコード

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