結果

問題 No.1488 Max Score of the Tree
ユーザー rodearodea
提出日時 2021-04-24 13:42:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 2,116 bytes
コンパイル時間 1,374 ms
コンパイル使用メモリ 122,024 KB
実行使用メモリ 42,660 KB
最終ジャッジ日時 2023-09-17 13:35:26
合計ジャッジ時間 3,298 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
41,000 KB
testcase_01 AC 47 ms
39,420 KB
testcase_02 AC 49 ms
41,532 KB
testcase_03 AC 51 ms
42,584 KB
testcase_04 AC 51 ms
42,640 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 14 ms
13,020 KB
testcase_07 AC 30 ms
25,812 KB
testcase_08 AC 22 ms
18,948 KB
testcase_09 AC 15 ms
14,144 KB
testcase_10 AC 30 ms
26,476 KB
testcase_11 AC 50 ms
42,580 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 9 ms
8,864 KB
testcase_14 AC 25 ms
22,120 KB
testcase_15 AC 16 ms
14,868 KB
testcase_16 AC 5 ms
5,264 KB
testcase_17 AC 10 ms
10,436 KB
testcase_18 AC 34 ms
29,392 KB
testcase_19 AC 20 ms
17,768 KB
testcase_20 AC 9 ms
8,796 KB
testcase_21 AC 5 ms
6,016 KB
testcase_22 AC 16 ms
14,516 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 13 ms
12,008 KB
testcase_27 AC 3 ms
4,420 KB
testcase_28 AC 6 ms
7,284 KB
testcase_29 AC 8 ms
8,536 KB
testcase_30 AC 41 ms
34,932 KB
testcase_31 AC 51 ms
42,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;

#define all(a) (a).begin(), (a).end()

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

vector<int> value(110, 1);
vector<int> children_cnt(110, 0);
void dfs(int cv, int pv, vector<vector<T>> &G){
    for(auto nxt : G[cv]){
        int nv, cost, idx; tie(nv, cost, idx) = nxt;
        if(nv == pv) continue;
        dfs(nv, cv, G);
        children_cnt[cv] += children_cnt[nv];
        value[idx] = children_cnt[nv];
    }
    if(children_cnt[cv] == 0) children_cnt[cv] = 1;
    return ;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, k; cin>>n>>k;
    vector<int> a(n-1), b(n-1), c(n-1);
    vector<vector<T>> G(n);
    for(int i=0; i<n-1; i++){
        cin>>a[i]>>b[i]>>c[i]; a[i]--, b[i]--;
        G[a[i]].emplace_back(b[i], c[i], i);
        G[b[i]].emplace_back(a[i], c[i], i);
    }

    dfs(0, -1, G);

    for(int i=0; i<n-1; i++) value[i] *= c[i];
    vector<vector<int>> dp(n, vector<int>(k + 1, 0));
    for(int i=0; i<n-1; i++){
        for(int j=0; j<=k; j++){
            chmax(dp[i + 1][j], dp[i][j]);
            if(k < c[i] + j) continue;
            chmax(dp[i + 1][c[i] + j], dp[i][j] + value[i]);
        }
    }

    ll ans = dp[n - 1][k];
    for(int i=0; i<n-1; i++) ans += value[i];
    cout << ans << endl;

    return 0;
}
0