結果

問題 No.1221 木 *= 3
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2020-09-09 14:18:49
言語 Ruby
(3.3.0)
結果
AC  
実行時間 577 ms / 2,000 ms
コード長 896 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 11,376 KB
実行使用メモリ 39,056 KB
最終ジャッジ日時 2023-08-21 16:06:58
合計ジャッジ時間 13,298 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
15,108 KB
testcase_01 AC 82 ms
15,312 KB
testcase_02 AC 80 ms
15,080 KB
testcase_03 AC 81 ms
15,124 KB
testcase_04 AC 81 ms
15,288 KB
testcase_05 AC 81 ms
15,140 KB
testcase_06 AC 80 ms
15,100 KB
testcase_07 AC 540 ms
36,384 KB
testcase_08 AC 547 ms
36,100 KB
testcase_09 AC 545 ms
36,140 KB
testcase_10 AC 544 ms
36,024 KB
testcase_11 AC 550 ms
36,176 KB
testcase_12 AC 543 ms
37,736 KB
testcase_13 AC 539 ms
37,336 KB
testcase_14 AC 557 ms
37,308 KB
testcase_15 AC 543 ms
37,588 KB
testcase_16 AC 549 ms
37,404 KB
testcase_17 AC 571 ms
39,056 KB
testcase_18 AC 566 ms
38,708 KB
testcase_19 AC 569 ms
38,892 KB
testcase_20 AC 570 ms
38,956 KB
testcase_21 AC 577 ms
38,836 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)
G = Array.new(N + 1){ [] }

def max(a,b); a > b ? a : b; end
class << G
  def bfs_order
    stack = [1]
    used = Array.new(size)
    order = []
    until stack.empty?
      u = stack.pop
      
      next if used[u]
      
      used[u] = order.size
      order << u
      self[u].each do |v|
        next if used[v]
        stack << v
      end
    end
    order.reverse
  end
end

(1 ... N).each do 
  a,b = gets.split.map(&:to_i)
  G[a] << b
  G[b] << a
end

order = G.bfs_order

dp0 = Array.new(N + 1)
dp1 = Array.new(N + 1)
used = Array.new(N + 1)

order.each do |u|
  used[u] = true
  
  dp0[u] = G[u].map do |v|
    next 0 if !used[v]
    max(dp0[v], dp1[v])
  end.sum + A[u - 1]
  
  dp1[u] = G[u].map do |v|
    next 0 if !used[v]
    max(dp1[v] + B[v - 1] + B[u - 1], dp0[v])
  end.sum
end

puts max(dp0[1], dp1[1])
0