結果

問題 No.1488 Max Score of the Tree
ユーザー tarattata1tarattata1
提出日時 2021-04-23 22:07:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 2,287 bytes
コンパイル時間 1,027 ms
コンパイル使用メモリ 102,964 KB
実行使用メモリ 82,540 KB
最終ジャッジ日時 2023-09-17 12:20:30
合計ジャッジ時間 3,269 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
78,812 KB
testcase_01 AC 78 ms
75,632 KB
testcase_02 AC 80 ms
79,628 KB
testcase_03 AC 84 ms
82,540 KB
testcase_04 AC 80 ms
82,164 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 20 ms
22,896 KB
testcase_07 AC 47 ms
48,520 KB
testcase_08 AC 30 ms
34,776 KB
testcase_09 AC 26 ms
25,488 KB
testcase_10 AC 46 ms
49,792 KB
testcase_11 AC 83 ms
82,304 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 14 ms
14,392 KB
testcase_14 AC 39 ms
40,896 KB
testcase_15 AC 24 ms
26,460 KB
testcase_16 AC 6 ms
7,580 KB
testcase_17 AC 16 ms
17,668 KB
testcase_18 AC 53 ms
55,848 KB
testcase_19 AC 29 ms
32,396 KB
testcase_20 AC 14 ms
14,964 KB
testcase_21 AC 7 ms
9,176 KB
testcase_22 AC 25 ms
25,944 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 18 ms
21,036 KB
testcase_27 AC 4 ms
5,928 KB
testcase_28 AC 10 ms
11,476 KB
testcase_29 AC 13 ms
14,132 KB
testcase_30 AC 64 ms
66,672 KB
testcase_31 AC 81 ms
82,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#include <ctime>
#pragma warning(disable:4996) 

//#define ATCODER
#ifdef ATCODER
#include <atcoder/all>
#endif

typedef long long ll;
typedef unsigned long long ull;
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;

using namespace std;
#ifdef ATCODER
using namespace atcoder;
#endif

vector<vector<pair<int,int>>> g;  // to,edge
vector<int> c;
vector<int> num0;
ll ans0;

int dfs(int par, int curr, ll dd)
{
    int sum = 0;
    for (int i = 0; i < (int)g[curr].size(); i++) {
        int ne=g[curr][i].first;
        int edge = g[curr][i].second;
        if (par == ne) continue;
        num0[edge] = dfs(curr, ne, dd+c[edge]);
        sum += num0[edge];
    }
    if (sum == 0) {
        ans0 += dd;
        sum++;
    }
    return sum;
}

void solve()
{
    int n, K;
    scanf("%d%d", &n, &K);
    c.resize(n - 1);
    g.resize(n); num0.resize(n - 1);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d%d", &a, &b, &c[i]); a--; b--;
        g[a].push_back(make_pair(b, i));
        g[b].push_back(make_pair(a, i));
    }
    int ret = dfs(-1, 0, 0);
    //assert(ret == n);

    vector<vector<ll>> dp(n, vector<ll>(K + 1, -LINF));
    dp[0][0] = 0;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j <= K; j++) {
            if (dp[i][j] < 0) continue;
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            int j2 = j + c[i];
            if (j2 <= K) {
                dp[i + 1][j2] = max(dp[i + 1][j2], dp[i][j] + (ll)c[i]*num0[i]);
            }
        }
    }

    ll ans = -LINF;
    for (int j = 0; j <= K; j++) {
        ans=max(ans, dp[n - 1][j]);
    }
    printf("%lld\n", ans+ans0);
    return;
}


int main()
{
#if 1
    solve();
#else
    int T, t;
    scanf("%d", &T);
    for (t = 0; t < T; t++) {
        //printf("Case #%d: ", t + 1);
        solve();
    }
#endif
    return 0;
}
0