結果

問題 No.1488 Max Score of the Tree
ユーザー chocono2230chocono2230
提出日時 2021-04-23 22:17:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,671 bytes
コンパイル時間 2,277 ms
コンパイル使用メモリ 213,596 KB
実行使用メモリ 82,512 KB
最終ジャッジ日時 2023-09-17 12:26:48
合計ジャッジ時間 5,113 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
78,828 KB
testcase_01 AC 91 ms
75,600 KB
testcase_02 AC 97 ms
79,264 KB
testcase_03 AC 98 ms
82,304 KB
testcase_04 AC 100 ms
82,512 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 26 ms
22,848 KB
testcase_07 AC 57 ms
48,408 KB
testcase_08 AC 40 ms
34,844 KB
testcase_09 AC 30 ms
25,244 KB
testcase_10 AC 58 ms
49,768 KB
testcase_11 AC 98 ms
82,300 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 16 ms
14,392 KB
testcase_14 AC 48 ms
40,808 KB
testcase_15 AC 31 ms
26,464 KB
testcase_16 AC 7 ms
7,796 KB
testcase_17 AC 19 ms
17,488 KB
testcase_18 AC 66 ms
56,084 KB
testcase_19 AC 37 ms
32,476 KB
testcase_20 AC 16 ms
14,640 KB
testcase_21 AC 9 ms
9,248 KB
testcase_22 AC 29 ms
25,996 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 23 ms
21,040 KB
testcase_27 AC 5 ms
5,748 KB
testcase_28 AC 12 ms
11,624 KB
testcase_29 AC 15 ms
14,144 KB
testcase_30 AC 80 ms
66,360 KB
testcase_31 AC 99 ms
82,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int dfs(vector<vector<tuple<int, int, int>>> &tree, vector<ll> &p, ll &ans, int now, int bf, int adans) {
  int res = 0;
  bool f = false;
  for(auto [nx, c, i] : tree.at(now)) {
    if(nx == bf) continue;
    int r = dfs(tree, p, ans, nx, now, adans+c);
    p.at(i) = r*c;
    res += r;
    f = true;
  }
  if(!f) {
    ans += adans;
    res++;
  }
  // cerr << now << " " << res << endl;
  return res;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector tree(n, vector<tuple<int, int, int>>());
  vector<int> ip(n-1);
  rep(i, n-1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--; b--;
    tree.at(a).push_back({b, c, i});
    tree.at(b).push_back({a, c, i});
    ip.at(i) = c;
  }
  vector<ll> p(n-1, 0);
  ll ans = 0;
  dfs(tree, p, ans, 0, -1, 0);
  // rep(i, n-1) cerr << p.at(i) << " ";
  // cerr << endl;
  // cerr << ans << endl;
  vector dp(n, vector<ll>(k+1, 0));
  rep(i, n-1) {
    rep(j, k+1) {
      dp.at(i+1).at(j) = max(dp.at(i+1).at(j), dp.at(i).at(j));
      if(j + ip.at(i) <= k) {
        dp.at(i+1).at(j+ip.at(i)) = max(dp.at(i+1).at(j+ip.at(i)), dp.at(i).at(j)+p.at(i));
      }
    }
  }
  ans += dp.back().back();
  cout << ans << endl;
  return 0;
}
0