結果

問題 No.1488 Max Score of the Tree
ユーザー m_tsubasam_tsubasa
提出日時 2021-04-23 21:53:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,104 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 208,568 KB
実行使用メモリ 4,736 KB
最終ジャッジ日時 2023-09-17 12:06:33
合計ジャッジ時間 3,710 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
4,632 KB
testcase_01 AC 13 ms
4,572 KB
testcase_02 AC 15 ms
4,612 KB
testcase_03 AC 14 ms
4,620 KB
testcase_04 AC 15 ms
4,668 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 9 ms
4,468 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 9 ms
4,684 KB
testcase_11 AC 14 ms
4,616 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 3 ms
4,384 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 7 ms
4,376 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 6 ms
4,568 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 12 ms
4,736 KB
testcase_31 AC 15 ms
4,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;

int n, k;
vector<vector<P>> g;
vector<long long> c, cnt, memo;

long long solve();
int dfs(int now = 0, int par = -1);

int main() {
  cin >> n >> k;
  g.resize(n);
  for (int i = 1; i < n; ++i) {
    int a, b, p;
    cin >> a >> b >> p;
    g[--a].emplace_back(--b, i - 1);
    g[b].emplace_back(a, i - 1);
    c.push_back(p);
  }
  cout << solve() << endl;
  return 0;
}

long long solve() {
  cnt.assign(n, 0);
  memo.assign(n - 1, 0);
  dfs();
  long long res = 0;
  vector<long long> dp(k + 1, -1e17);
  dp[0] = 0;
  for (int i = 0; i < n - 1; ++i) {
    res += memo[i] * c[i];
    vector<long long> v = dp;
    for (int j = c[i]; j <= k; ++j)
      v[j] = max(v[j], dp[j - c[i]] + memo[i] * c[i]);
    swap(v, dp);
  }
  long long tmp = -1;
  for (auto p : dp) tmp = max(tmp, p);
  return res + tmp;
}

int dfs(int now, int par) {
  int leaf = 0;
  for (auto [to, id] : g[now])
    if (to != par) {
      int tmp = dfs(to, now);
      leaf += tmp;
      memo[id] = tmp;
    }
  if (!leaf) ++leaf;
  return cnt[now] = leaf;
}
0