結果

問題 No.1221 木 *= 3
ユーザー marroncastlemarroncastle
提出日時 2020-12-31 18:36:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 286 ms / 2,000 ms
コード長 1,824 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 82,300 KB
実行使用メモリ 108,504 KB
最終ジャッジ日時 2024-04-17 23:55:52
合計ジャッジ時間 6,040 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,708 KB
testcase_01 AC 45 ms
55,264 KB
testcase_02 AC 43 ms
54,980 KB
testcase_03 AC 46 ms
55,256 KB
testcase_04 AC 41 ms
55,460 KB
testcase_05 AC 41 ms
54,336 KB
testcase_06 AC 41 ms
54,888 KB
testcase_07 AC 242 ms
104,908 KB
testcase_08 AC 221 ms
104,772 KB
testcase_09 AC 237 ms
104,896 KB
testcase_10 AC 237 ms
104,872 KB
testcase_11 AC 233 ms
104,912 KB
testcase_12 AC 221 ms
108,504 KB
testcase_13 AC 224 ms
107,972 KB
testcase_14 AC 244 ms
107,948 KB
testcase_15 AC 253 ms
107,988 KB
testcase_16 AC 253 ms
107,852 KB
testcase_17 AC 276 ms
105,704 KB
testcase_18 AC 283 ms
103,636 KB
testcase_19 AC 280 ms
105,540 KB
testcase_20 AC 284 ms
105,544 KB
testcase_21 AC 286 ms
103,644 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:
        pass
        #根の帰りがけまとめ処理
      else:
        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
  
  def bfs(self, start):
    d = deque()
    self.min_cost = [-1]*self.V
    self.min_cost[start]=0
    d.append(start)
    while len(d)>0:
      v = d.popleft()
      for w in self.edge[v]:
        if self.min_cost[w]==-1:
          self.min_cost[w]=self.min_cost[v]+1
          d.append(w)
    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