結果

問題 No.1488 Max Score of the Tree
ユーザー tabae326tabae326
提出日時 2021-04-23 22:11:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 1,704 bytes
コンパイル時間 2,346 ms
コンパイル使用メモリ 214,084 KB
実行使用メモリ 90,236 KB
最終ジャッジ日時 2023-09-17 12:23:45
合計ジャッジ時間 4,854 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
86,484 KB
testcase_01 AC 85 ms
83,348 KB
testcase_02 AC 89 ms
87,276 KB
testcase_03 AC 91 ms
90,236 KB
testcase_04 AC 91 ms
90,164 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 27 ms
28,232 KB
testcase_07 AC 54 ms
54,816 KB
testcase_08 AC 39 ms
41,652 KB
testcase_09 AC 27 ms
27,912 KB
testcase_10 AC 54 ms
57,152 KB
testcase_11 AC 91 ms
90,156 KB
testcase_12 AC 4 ms
5,892 KB
testcase_13 AC 14 ms
15,708 KB
testcase_14 AC 44 ms
47,092 KB
testcase_15 AC 29 ms
31,916 KB
testcase_16 AC 9 ms
11,084 KB
testcase_17 AC 20 ms
22,628 KB
testcase_18 AC 61 ms
61,864 KB
testcase_19 AC 38 ms
38,616 KB
testcase_20 AC 16 ms
17,248 KB
testcase_21 AC 11 ms
14,124 KB
testcase_22 AC 28 ms
29,468 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 26 ms
28,704 KB
testcase_27 AC 5 ms
6,476 KB
testcase_28 AC 11 ms
13,200 KB
testcase_29 AC 15 ms
17,072 KB
testcase_30 AC 74 ms
74,644 KB
testcase_31 AC 90 ms
90,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

using pl = pair<ll,ll>;
using tl = tuple<ll,ll,ll>;

void chmax(ll &a, ll b) {
    a = max(a, b);
}

void solve() {
    ll n, k;
    cin >> n >> k;
    vector<vector<tl>> G(n);
    vector<ll> weight(n-1);
    rep(i, 0, n-1) {
        ll u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        G[u].push_back({v, w, i});
        G[v].push_back({u, w, i});
        weight[i] = w;
    }
    vector<ll> cnt(n-1, 0), sub(n, 0);
    auto dfs1 = [&](auto dfs1, ll cur, ll par) -> ll {
        ll res = 0;
        bool leaf = true;
        for(auto [to, cost, id] : G[cur]) {
            if(to == par) continue;
            leaf = false;
            res += dfs1(dfs1, to, cur);
            cnt[id] += sub[to];
        }
        if(leaf) res = 1;
        sub[cur] = res;
        return res;
    };
    dfs1(dfs1, 0, -1);
    vector<ll> depth(n, 0);
    ll sum = 0;
    auto dfs2 = [&](auto dfs2, ll cur, ll par) -> void {
        bool leaf = true;
        for(auto [to, cost, id] : G[cur]) {
            if(to == par) continue;
            leaf = false;
            depth[to] = depth[cur] + cost;
            dfs2(dfs2, to, cur);
        }
        if(leaf) sum += depth[cur];
    };
    dfs2(dfs2, 0, -1);
    vector dp(n+10, vector<ll>(k+100, 0));
    rep(i, 0, n-1) {
        rep(j, 0, k+1) chmax(dp[i+1][j], dp[i][j]);
        rep(j, 0, k) if(j+weight[i] <= k) chmax(dp[i+1][j+weight[i]], dp[i][j]+cnt[i]*weight[i]);
    }
    ll ans = 0;
    rep(i, 0, k+1) chmax(ans, dp[n-1][i]);
    cout << sum+ans << endl;
}

int main() {
    solve();
    return 0;
}
0