結果

問題 No.1221 木 *= 3
ユーザー marroncastlemarroncastle
提出日時 2020-12-31 18:37:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 316 ms / 2,000 ms
コード長 1,453 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 108,360 KB
最終ジャッジ日時 2024-04-18 00:00:31
合計ジャッジ時間 5,803 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
54,016 KB
testcase_01 AC 43 ms
53,632 KB
testcase_02 AC 47 ms
53,888 KB
testcase_03 AC 45 ms
54,272 KB
testcase_04 AC 43 ms
54,016 KB
testcase_05 AC 44 ms
53,760 KB
testcase_06 AC 43 ms
53,760 KB
testcase_07 AC 242 ms
105,100 KB
testcase_08 AC 240 ms
105,004 KB
testcase_09 AC 254 ms
105,172 KB
testcase_10 AC 248 ms
105,124 KB
testcase_11 AC 250 ms
104,912 KB
testcase_12 AC 235 ms
108,360 KB
testcase_13 AC 232 ms
107,748 KB
testcase_14 AC 259 ms
107,816 KB
testcase_15 AC 262 ms
108,068 KB
testcase_16 AC 266 ms
107,936 KB
testcase_17 AC 292 ms
105,544 KB
testcase_18 AC 305 ms
103,876 KB
testcase_19 AC 298 ms
105,508 KB
testcase_20 AC 302 ms
105,420 KB
testcase_21 AC 316 ms
103,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque
class Tree:
  def __init__(self, N):
    self.V = N
    self.edge = [[] for _ in range(N)]
  
  def add_edges(self, ind=1, bi=True):
    for i in range(self.V-1):
      a,b = map(int, input().split())
      a -= ind; b -= ind
      self.edge[a].append(b)
      if bi:
        self.edge[b].append(a)

  def add_edge(self, a, b, bi=True):
    self.edge[a].append(b)
    if bi:
      self.edge[b].append(a)

  def dfs(self, start):
    stack = deque([start])
    self.parent = [N]*N
    self.parent[start] = -1
    order = [start]
    self.offs = A[:]
    self.ons = [0]*self.V
    #記録したい値の配列を定義
    while stack:
      v = stack.pop()
      for u in self.edge[v]:
        if u==self.parent[v]:
          continue
        self.parent[u]=v
        stack.append(u)
        order.append(u)
    for v in order[::-1]:
      for u in self.edge[v]:
        if u==self.parent[v]:
          continue
        # 帰りがけ処理
        self.offs[v] += self.offs[u]
        self.ons[v] += self.ons[u]
      if v!=start:
        self.offs[v], self.ons[v] = max(self.offs[v],self.ons[v]), max(self.offs[v],self.ons[v]+B[v]+B[self.parent[v]])
        #根以外の帰りがけまとめ処理
    return
  
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
G = Tree(N)
G.add_edges(ind=1, bi=True)
G.dfs(0)
print(max(G.offs[0], G.ons[0]))
0