結果

問題 No.1488 Max Score of the Tree
ユーザー simansiman
提出日時 2021-04-27 14:18:56
言語 Ruby
(3.3.0)
結果
AC  
実行時間 869 ms / 2,000 ms
コード長 756 bytes
コンパイル時間 421 ms
コンパイル使用メモリ 11,432 KB
実行使用メモリ 96,288 KB
最終ジャッジ日時 2023-09-20 13:18:03
合計ジャッジ時間 11,709 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 320 ms
63,996 KB
testcase_01 AC 869 ms
94,556 KB
testcase_02 AC 691 ms
74,908 KB
testcase_03 AC 592 ms
71,968 KB
testcase_04 AC 717 ms
71,340 KB
testcase_05 AC 91 ms
15,524 KB
testcase_06 AC 97 ms
16,484 KB
testcase_07 AC 248 ms
53,200 KB
testcase_08 AC 94 ms
15,944 KB
testcase_09 AC 336 ms
55,904 KB
testcase_10 AC 126 ms
24,364 KB
testcase_11 AC 744 ms
96,288 KB
testcase_12 AC 91 ms
15,520 KB
testcase_13 AC 219 ms
31,052 KB
testcase_14 AC 186 ms
36,964 KB
testcase_15 AC 121 ms
23,240 KB
testcase_16 AC 91 ms
15,392 KB
testcase_17 AC 107 ms
18,732 KB
testcase_18 AC 303 ms
52,680 KB
testcase_19 AC 111 ms
20,784 KB
testcase_20 AC 106 ms
19,640 KB
testcase_21 AC 88 ms
15,416 KB
testcase_22 AC 135 ms
26,232 KB
testcase_23 AC 89 ms
15,504 KB
testcase_24 AC 88 ms
15,496 KB
testcase_25 AC 90 ms
15,364 KB
testcase_26 AC 90 ms
15,524 KB
testcase_27 AC 92 ms
16,140 KB
testcase_28 AC 102 ms
17,644 KB
testcase_29 AC 128 ms
23,068 KB
testcase_30 AC 152 ms
30,280 KB
testcase_31 AC 603 ms
73,244 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N, K = gets.split.map(&:to_i)
E = Hash.new { |h, k| h[k] = [] }
C = Array.new(N + 1) { Array.new(N + 1, 0) }
L = []

(N - 1).times do
  a, b, c = gets.split.map(&:to_i)

  if a < b
    L << [a, b, c]
    E[a] << [b, c]
    E[b] << [a, c]
  else
    L << [b, a, c]
    E[a] << [b, c]
    E[b] << [a, c]
  end
end

def dfs(v, parent = -1)
  val = 0

  E[v].each do |u, c|
    next if u == parent

    a = dfs(u, v)
    C[v][u] = C[u][v] = a
    val += a
  end

  [1, val].max
end

dfs(1)

dp = Hash.new(0)
dp[0] = L.map { |u, v, c| C[u][v] * c }.sum

L.each do |u, v, c|
  temp = dp.dup

  dp.each do |k, val|
    nk = k + c
    nc = val + C[u][v] * c
    next if nk > K
    next if temp[nk] >= nc

    temp[nk] = nc
  end

  dp = temp
end

pp dp.values.max
0