結果

問題 No.1488 Max Score of the Tree
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-06-19 14:42:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 1,082 bytes
コンパイル時間 2,643 ms
コンパイル使用メモリ 212,260 KB
実行使用メモリ 82,264 KB
最終ジャッジ日時 2023-09-09 09:54:49
合計ジャッジ時間 4,979 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
78,724 KB
testcase_01 AC 72 ms
75,616 KB
testcase_02 AC 73 ms
79,704 KB
testcase_03 AC 77 ms
82,244 KB
testcase_04 AC 77 ms
82,260 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 21 ms
22,860 KB
testcase_07 AC 45 ms
48,420 KB
testcase_08 AC 31 ms
34,696 KB
testcase_09 AC 23 ms
25,456 KB
testcase_10 AC 46 ms
50,028 KB
testcase_11 AC 77 ms
82,164 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 13 ms
14,476 KB
testcase_14 AC 38 ms
40,836 KB
testcase_15 AC 23 ms
26,636 KB
testcase_16 AC 6 ms
7,488 KB
testcase_17 AC 15 ms
17,656 KB
testcase_18 AC 52 ms
55,804 KB
testcase_19 AC 30 ms
32,300 KB
testcase_20 AC 13 ms
14,608 KB
testcase_21 AC 7 ms
9,040 KB
testcase_22 AC 24 ms
26,096 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,384 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 19 ms
21,012 KB
testcase_27 AC 4 ms
5,700 KB
testcase_28 AC 10 ms
11,508 KB
testcase_29 AC 13 ms
14,116 KB
testcase_30 AC 63 ms
66,636 KB
testcase_31 AC 77 ms
82,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll N, K;
vector<vector<pair<ll, ll>>> E;
vector<ll> subtree;
vector<vector<ll>> dp;
vector<pair<ll, ll>> D;

ll subtree_size(ll from, ll p){
    ll cnt = 0;
    for (auto [to, C] : E[from]){
        if (to == p) continue;
        cnt += subtree_size(to, from);
        D.push_back({C, C*subtree[to]});
    }

    if (cnt == 0) cnt = 1;

    return subtree[from] = cnt;
}

int main(){

    ll A, B, C, x, y, ans=0;
    cin >> N >> K;
    
    E.resize(N);
    subtree.resize(N);
    
    for (int i=0; i < N-1; i++){
        cin >> A >> B >> C;
        A--; B--;
        E[A].push_back({B, C});
        E[B].push_back({A, C});
    }

    subtree_size(0, -1); 

    vector<vector<ll>> dp(N, vector<ll>(K+1));
    
    for (int i=1; i<=N-1; i++){
        tie(x, y) = D[i-1];
        ans += y;
        for (int j=0; j<=K; j++){
            if (j-x>=0) dp[i][j] = max(dp[i][j], dp[i-1][j-x]+y);
            dp[i][j] = max(dp[i][j], dp[i-1][j]);
        }
    }

    cout << ans + dp[N-1][K] << endl;

    return 0;
}
0