結果

問題 No.1488 Max Score of the Tree
ユーザー rogi52rogi52
提出日時 2022-10-15 07:40:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 1,319 bytes
コンパイル時間 2,005 ms
コンパイル使用メモリ 208,020 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 02:14:28
合計ジャッジ時間 3,852 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    // (w,v) = (c(e), leaf_cnt(e.c) * c(e))
    int N,K; cin >> N >> K;
    vector<vector<pair<int,int>>> G(N);
    rep(i,N-1) {
        int a,b,c; cin >> a >> b >> c; a--; b--;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }

    int ROOT = 0, SUM = 0;
    vector<int> leaf_cnt(N, 0);
    vector<pair<int,int>> wv;
    function<void(int,int)> get_leaf = [&](int v, int p) -> void {
        int is_leaf = 1;
        for(auto [to, c] : G[v]) {
            if(to != p) {
                is_leaf = 0;
                get_leaf(to, v);
                leaf_cnt[v] += leaf_cnt[to];
            }
        }
        leaf_cnt[v] += is_leaf;
        for(auto [to, c] : G[v]) {
            if(to != p) {
                wv.push_back({c, leaf_cnt[to] * c});
                SUM += leaf_cnt[to] * c;
            }
        }
    }; get_leaf(ROOT, -1);

    vector<int> dp(K + 1, 0);
    for(auto [w, v] : wv) {
        vector<int> nt = dp;
        for(int x = 0; x <= K; x++)
            if(x + w <= K) nt[x + w] = max(nt[x + w], dp[x] + v);
        swap(dp, nt);
    }

    cout << SUM + *max_element(dp.begin(), dp.end()) << endl;
}
0