結果

問題 No.1488 Max Score of the Tree
ユーザー kimiyukikimiyuki
提出日時 2021-04-23 23:09:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,574 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 208,524 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-17 13:03:31
合計ジャッジ時間 3,421 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,380 KB
testcase_01 AC 6 ms
4,376 KB
testcase_02 AC 6 ms
4,376 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 AC 6 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 7 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

int64_t solve(int n, int k, const vector<vector<pair<int, int64_t> > >& g) {
    vector<pair<int, int64_t> > f;
    int64_t sum_depth = 0;
    auto go = [&](auto&& go, int x, int parent, int64_t depth) -> int {
        int cnt_x = 0;
        for (auto [y, c] : g[x]) {
            if (y != parent) {
                int cnt_y = go(go, y, x, depth + c);
                f.emplace_back(cnt_y, c);
                cnt_x += cnt_y;
            }
        }
        if (cnt_x == 0) {
            sum_depth += depth;
            return 1;
        }
        return cnt_x;
    };
    go(go, 0, -1, 0);
    vector<int64_t> dp(k + 1);
    for (auto [cnt, c] : f) {
        REP_R (i, k + 1 - c) {
            dp[i + c] = max(dp[i + c], dp[i] + cnt * c);
        }
    }
    return sum_depth + dp[k];
}

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
    int n, k;
    cin >> n >> k;
    vector<vector<pair<int, int64_t> > > g(n);
    REP (i, n - 1) {
        int a, b; int64_t c;
        cin >> a >> b >> c;
        -- a;
        -- b;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    auto ans = solve(n, k, g);
    cout << ans << endl;
    return 0;
}
0