結果

問題 No.1488 Max Score of the Tree
ユーザー maguroflymagurofly
提出日時 2021-04-23 22:03:21
言語 Ruby
(3.2.2)
結果
AC  
実行時間 1,368 ms / 2,000 ms
コード長 829 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 11,196 KB
実行使用メモリ 74,448 KB
最終ジャッジ日時 2023-09-17 12:17:15
合計ジャッジ時間 22,251 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,263 ms
74,448 KB
testcase_01 AC 1,246 ms
71,296 KB
testcase_02 AC 1,325 ms
74,368 KB
testcase_03 AC 1,365 ms
73,844 KB
testcase_04 AC 1,357 ms
73,844 KB
testcase_05 AC 81 ms
15,224 KB
testcase_06 AC 383 ms
34,340 KB
testcase_07 AC 793 ms
53,928 KB
testcase_08 AC 559 ms
46,176 KB
testcase_09 AC 441 ms
35,152 KB
testcase_10 AC 802 ms
54,852 KB
testcase_11 AC 1,346 ms
73,848 KB
testcase_12 AC 84 ms
15,740 KB
testcase_13 AC 265 ms
26,160 KB
testcase_14 AC 679 ms
52,208 KB
testcase_15 AC 460 ms
38,088 KB
testcase_16 AC 138 ms
19,220 KB
testcase_17 AC 309 ms
29,512 KB
testcase_18 AC 906 ms
45,608 KB
testcase_19 AC 539 ms
44,028 KB
testcase_20 AC 264 ms
26,604 KB
testcase_21 AC 166 ms
20,768 KB
testcase_22 AC 435 ms
37,656 KB
testcase_23 AC 80 ms
15,220 KB
testcase_24 AC 82 ms
15,120 KB
testcase_25 AC 82 ms
15,096 KB
testcase_26 AC 349 ms
32,376 KB
testcase_27 AC 121 ms
17,692 KB
testcase_28 AC 206 ms
23,484 KB
testcase_29 AC 258 ms
26,284 KB
testcase_30 AC 1,085 ms
63,396 KB
testcase_31 AC 1,368 ms
73,856 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N, K = gets.split.map(&:to_i)
graph = Array.new(N) { [] }
weight = []
(N - 1).times do
    a, b, c = gets.split.map(&:to_i)
    e = weight.size
    graph[a - 1] << [b - 1, e]
    graph[b - 1] << [a - 1, e]
    weight << c
end

value = [0] * (N - 1)
path = []
visited = [false] * N
dfs = ->(u) {
    visited[u] = true
    has_children = false
    graph[u].each do |(v, e)|
        next if visited[v]
        has_children = true
        path << e
        dfs[v]
        path.pop
    end
    unless has_children
      path.each do |e|
        value[e] += weight[e]
      end
    end
}
dfs[0]

dp = Array.new(K + 1, 0)
(0 ... N - 1).each do |i|
  dp2 = Array.new(K + 1, 0)
  (0 .. K).each do |w|
    v = dp[w]
    v = [v, dp[w - weight[i]] + value[i]].max if w >= weight[i]
    dp2[w] = v
  end
  dp = dp2
end

puts value.sum + dp[K]
0