結果

問題 No.1488 Max Score of the Tree
ユーザー milanis48663220milanis48663220
提出日時 2021-04-23 21:47:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,814 bytes
コンパイル時間 869 ms
コンパイル使用メモリ 100,480 KB
実行使用メモリ 79,104 KB
最終ジャッジ日時 2023-09-17 12:02:36
合計ジャッジ時間 3,456 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
72,956 KB
testcase_01 AC 92 ms
71,412 KB
testcase_02 AC 100 ms
77,656 KB
testcase_03 AC 100 ms
77,628 KB
testcase_04 AC 102 ms
79,104 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 25 ms
20,516 KB
testcase_07 AC 60 ms
46,720 KB
testcase_08 AC 40 ms
31,996 KB
testcase_09 AC 31 ms
24,004 KB
testcase_10 AC 59 ms
46,760 KB
testcase_11 AC 103 ms
78,308 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 17 ms
14,528 KB
testcase_14 AC 48 ms
38,928 KB
testcase_15 AC 27 ms
21,720 KB
testcase_16 AC 7 ms
7,200 KB
testcase_17 AC 19 ms
15,976 KB
testcase_18 AC 66 ms
52,228 KB
testcase_19 AC 36 ms
28,716 KB
testcase_20 AC 16 ms
14,120 KB
testcase_21 AC 9 ms
8,024 KB
testcase_22 AC 30 ms
24,804 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 21 ms
17,204 KB
testcase_27 AC 5 ms
5,800 KB
testcase_28 AC 12 ms
11,016 KB
testcase_29 AC 15 ms
13,112 KB
testcase_30 AC 75 ms
58,140 KB
testcase_31 AC 105 ms
78,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

int n, k;
int par[101];
int par_cost[101];
bool used[101];
int ch[101];

struct edge{
    int to, cost;
};

vector<edge> g[101];

void dfs(int v){
    used[v] = true;
    int cnt = 0;
    for(edge e : g[v]){
        if(!used[e.to]){
            par_cost[e.to] = e.cost;
            par[e.to] = v;
            dfs(e.to);
            ch[v] += ch[e.to];
            cnt++;
        }
    }
    if(cnt == 0) ch[v] = 1;
}

using P = pair<ll, int>;
ll dp[101][100005];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> n >> k;
    for(int i = 0; i < n-1; i++){
        int a, b, c; cin >> a >> b >> c; a--; b--;
        g[a].push_back(edge{b, c});
        g[b].push_back(edge{a, c});
    }
    dfs(0);
    vector<ll> w(n-1), v(n-1);
    for(int i = 1; i < n; i++){
        w[i-1] = par_cost[i];
        v[i-1] = par_cost[i]*ch[i];
    }
    for(int i = 0; i < n-1; i++){
        for(int j = 0; j <= k; j++){
            chmax(dp[i+1][j], dp[i][j]);
            if(j+w[i] <= k) chmax(dp[i+1][j+w[i]], dp[i][j]+v[i]);
        }
    }
    ll ans = 0;
    for(int i = 0; i <= k; i++) chmax(ans, dp[n-1][i]);
    for(int i = 1; i < n; i++){
        ans += v[i-1];
    }
    cout << ans << endl;
}
0