結果

問題 No.1488 Max Score of the Tree
ユーザー magstamagsta
提出日時 2021-04-23 21:58:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,718 bytes
コンパイル時間 959 ms
コンパイル使用メモリ 91,000 KB
実行使用メモリ 4,736 KB
最終ジャッジ日時 2023-09-17 12:10:35
合計ジャッジ時間 3,233 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
4,556 KB
testcase_01 AC 63 ms
4,676 KB
testcase_02 AC 65 ms
4,600 KB
testcase_03 AC 70 ms
4,592 KB
testcase_04 AC 69 ms
4,548 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 19 ms
4,380 KB
testcase_07 AC 41 ms
4,480 KB
testcase_08 AC 32 ms
4,388 KB
testcase_09 AC 20 ms
4,380 KB
testcase_10 AC 46 ms
4,376 KB
testcase_11 AC 71 ms
4,596 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 34 ms
4,380 KB
testcase_15 AC 21 ms
4,380 KB
testcase_16 AC 6 ms
4,376 KB
testcase_17 AC 13 ms
4,376 KB
testcase_18 AC 48 ms
4,412 KB
testcase_19 AC 27 ms
4,464 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 22 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 18 ms
4,536 KB
testcase_27 AC 4 ms
4,380 KB
testcase_28 AC 10 ms
4,376 KB
testcase_29 AC 11 ms
4,376 KB
testcase_30 AC 60 ms
4,548 KB
testcase_31 AC 65 ms
4,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <vector>
using namespace std;
#define PAIR pair<long long, long long>
#define m_p make_pair
using Graph = vector<vector<PAIR>>;

long long N, K;

vector<int> depth;
vector<int> subtree_size;
vector<long long> dp;
long long sum = 0;
void dfs(const Graph& G, int v, int p, int d) {
    depth[v] = d;
    int count = 0;
    for (auto nv : G[v]) {
        if (nv.first == p) continue;
        count++;
        dfs(G, nv.first, v, d + 1);
    }

    subtree_size[v] = 1;
    if (count > 0) subtree_size[v] = 0;
    for (auto c : G[v]) {
        if (c.first == p) continue;
        subtree_size[v] += subtree_size[c.first];
    }
    vector<long long> dp_(K + 1, 0);
    for (auto c : G[v]) {
        if (c.first == p) {
            long long a = c.second;
            for (int i = 0; i <= K; i++) {
                if (i + a < K + 1) dp_[i + a] = max(dp[i + a] + a * subtree_size[v], dp[i] + 2 * a * subtree_size[v]);
                else dp_[(i + a) % (K + 1)] = dp[(i + a) % (K + 1)] + a * subtree_size[v];
            }
        }
    }
    if (v != 0) for (int i = 0; i <= K; i++) dp[i] = dp_[i];
}

int main() {
    cin >> N >> K;

    Graph G(N);
    for (int i = 0; i < N - 1; i++) {
        long long a, b, c;
        cin >> a >> b >> c;
        G[a - 1].push_back(m_p(b - 1, c));
        G[b - 1].push_back(m_p(a - 1, c));
    }

    depth.assign(N, 0);
    subtree_size.assign(N, 0);
    dp.assign(K + 1, 0);

    dfs(G, 0, -1, 0);
    long long max_ = 0;
    for (int i = 0; i < K + 1; i++) max_ = max(max_, dp[i]);
    cout << max_ << endl;
}
0