結果

問題 No.1488 Max Score of the Tree
ユーザー MZKiMZKi
提出日時 2021-04-05 00:49:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 1,565 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 157,048 KB
実行使用メモリ 82,480 KB
最終ジャッジ日時 2023-08-28 13:05:03
合計ジャッジ時間 5,031 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
78,796 KB
testcase_01 AC 70 ms
75,660 KB
testcase_02 AC 75 ms
79,556 KB
testcase_03 AC 74 ms
82,200 KB
testcase_04 AC 77 ms
82,236 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 20 ms
22,860 KB
testcase_07 AC 45 ms
48,540 KB
testcase_08 AC 31 ms
34,712 KB
testcase_09 AC 24 ms
25,604 KB
testcase_10 AC 46 ms
49,820 KB
testcase_11 AC 76 ms
82,480 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 12 ms
14,444 KB
testcase_14 AC 37 ms
40,692 KB
testcase_15 AC 24 ms
26,524 KB
testcase_16 AC 6 ms
7,564 KB
testcase_17 AC 15 ms
17,504 KB
testcase_18 AC 51 ms
55,860 KB
testcase_19 AC 29 ms
32,336 KB
testcase_20 AC 12 ms
14,624 KB
testcase_21 AC 7 ms
9,132 KB
testcase_22 AC 23 ms
25,968 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 18 ms
21,016 KB
testcase_27 AC 4 ms
5,776 KB
testcase_28 AC 9 ms
11,584 KB
testcase_29 AC 12 ms
14,448 KB
testcase_30 AC 62 ms
66,620 KB
testcase_31 AC 76 ms
82,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

struct edge{
    ll to, cost;
};

vector<int> cnt(110);
vector<P> vec;

void dfs(const vector<vector<edge>> &G, int v, int p){
    int ans = 0;
    for(auto nv : G[v]){
        if(nv.to == p)continue;
        dfs(G, nv.to, v);
        ans += cnt[nv.to];
        vec.push_back({nv.cost * cnt[nv.to], nv.cost});
    }
    cnt[v] = ans;
    if(ans == 0)cnt[v]++;
}

int main() {
    int n, k;
    cin >> n >> k;
    vector<vector<edge>> G(n);
    rep(i, n-1){
        ll a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }
    dfs(G, 0, -1);
    vector<vector<ll>> dp(n, vector<ll>(k+1, 0));
    ll ans = 0;
    rep(i, n-1){
        ans += vec[i].first;
        rep(j, k+1){
            dp[i+1][j] = dp[i][j];
            if(j-vec[i].second >= 0){
                chmax(dp[i+1][j], dp[i][j-vec[i].second] + vec[i].first);
            }
        }
    }
    cout << ans + dp[n-1][k] << endl;
}
0