結果

問題 No.1488 Max Score of the Tree
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2021-04-23 22:40:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,118 bytes
コンパイル時間 1,843 ms
コンパイル使用メモリ 178,308 KB
実行使用メモリ 82,372 KB
最終ジャッジ日時 2023-09-17 12:44:26
合計ジャッジ時間 4,109 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
78,920 KB
testcase_01 AC 68 ms
75,620 KB
testcase_02 AC 71 ms
79,588 KB
testcase_03 AC 74 ms
82,372 KB
testcase_04 AC 74 ms
82,228 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 20 ms
22,836 KB
testcase_07 AC 42 ms
48,516 KB
testcase_08 AC 30 ms
34,988 KB
testcase_09 AC 22 ms
25,524 KB
testcase_10 AC 43 ms
49,704 KB
testcase_11 AC 75 ms
82,352 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 12 ms
14,336 KB
testcase_14 AC 37 ms
41,084 KB
testcase_15 AC 23 ms
26,672 KB
testcase_16 AC 5 ms
7,656 KB
testcase_17 AC 15 ms
17,624 KB
testcase_18 AC 50 ms
55,964 KB
testcase_19 AC 29 ms
32,400 KB
testcase_20 AC 13 ms
14,912 KB
testcase_21 AC 7 ms
9,108 KB
testcase_22 AC 23 ms
25,948 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 18 ms
20,924 KB
testcase_27 AC 4 ms
5,968 KB
testcase_28 AC 10 ms
11,612 KB
testcase_29 AC 12 ms
14,120 KB
testcase_30 AC 61 ms
66,648 KB
testcase_31 AC 76 ms
82,256 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int dfs(int, int)’ 内:
main.cpp:16:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   16 |     for(auto [u,c,idx] : g[v]) {
      |              ^

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2021.04.23 22:40:53
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

vector<vector<array<int,3>>> g;
vector<array<ll,2>> edge;
ll ans;
int dfs(int v,int p = -1) {
    int sum = 0;
    for(auto [u,c,idx] : g[v]) {
        if (u == p) continue;
        int d = dfs(u,v);
        sum += d;
        edge[idx][1] = c*d;
        ans += c*d;
    }
    if (sum == 0) sum++;
    
    return sum;
}
int main() {
    int n,k;cin >> n >> k;
    g.resize(n);
    edge.resize(n-1);
    for (int i = 0; i < n-1; i++) {
        int a,b,c;cin >> a >> b >> c;
        a--;b--;
        g[a].push_back({b,c,i});
        g[b].push_back({a,c,i});
        edge[i][0] = c;
        edge[i][1] = 0;
    }
    dfs(0);
    vector<vector<ll>> dp(n,vector<ll>(k+1));
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j <= k; j++) {
            dp[i+1][j] = dp[i][j];
            if (edge[i][0] <= j) dp[i+1][j] = max(dp[i+1][j],dp[i][j-edge[i][0]]+edge[i][1]);
        }
    }
    
    
    
    
    cout << ans + dp[n-1][k] << endl;
    return 0;
}
0