結果

問題 No.1488 Max Score of the Tree
ユーザー maguromaguro
提出日時 2021-04-06 11:40:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 3,062 bytes
コンパイル時間 2,532 ms
コンパイル使用メモリ 215,352 KB
実行使用メモリ 83,392 KB
最終ジャッジ日時 2023-08-30 22:57:37
合計ジャッジ時間 4,908 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
79,176 KB
testcase_01 AC 32 ms
77,240 KB
testcase_02 AC 33 ms
81,192 KB
testcase_03 AC 34 ms
83,224 KB
testcase_04 AC 34 ms
83,392 KB
testcase_05 AC 11 ms
54,864 KB
testcase_06 AC 11 ms
31,624 KB
testcase_07 AC 22 ms
58,440 KB
testcase_08 AC 15 ms
40,008 KB
testcase_09 AC 19 ms
72,000 KB
testcase_10 AC 22 ms
54,500 KB
testcase_11 AC 34 ms
83,228 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 17 ms
71,412 KB
testcase_14 AC 19 ms
50,156 KB
testcase_15 AC 14 ms
39,688 KB
testcase_16 AC 4 ms
12,552 KB
testcase_17 AC 9 ms
25,380 KB
testcase_18 AC 26 ms
72,672 KB
testcase_19 AC 16 ms
39,972 KB
testcase_20 AC 11 ms
41,032 KB
testcase_21 AC 5 ms
13,128 KB
testcase_22 AC 16 ms
53,592 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
5,480 KB
testcase_25 AC 4 ms
13,596 KB
testcase_26 AC 9 ms
21,740 KB
testcase_27 AC 6 ms
26,208 KB
testcase_28 AC 11 ms
42,748 KB
testcase_29 AC 10 ms
36,880 KB
testcase_30 AC 28 ms
68,844 KB
testcase_31 AC 34 ms
83,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 2000000030;
constexpr ll INF= 2000000000000000001;
constexpr ll MOD = 1000000007;
const double PI = 3.14159265358979323846;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;

template<typename T> 
vector<T> make_vector(size_t s) {
    return vector<T>(s);
}

template<typename T,typename... Args>
auto make_vector(size_t s,Args... args) {
    return vector(s,make_vector<T>(args...));
}

template<typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}

template<typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

ll mod(ll val, ll M) {
    val = val % M;
    if(val < 0) {
        val += M;
    }
    return val;
}

template<typename T>
T RS(T N, T P, T M){
    if(P == 0) {
        return 1;
    }
    if(P < 0) {
        return 0;
    }
    if(P % 2 == 0){
        ll t = RS(N, P/2, M);
        if(M == -1) return t * t;
        return t * t % M;
    }
    if(M == -1) {
        return N * RS(N,P - 1,M);
    }
    return N * RS(N, P-1, M) % M;
}

int N,K;
vector<P> vec;
vector<ll> cost;
vector<int> graph[110];
vector<int> rgraph;
vector<int> cnt;
queue<int> leaf;
ll dp[110][100010];

void bfs() {
    vector<bool> used(N);
    queue<int> que;
    que.push(0);
    while(!que.empty()) {
        int p = que.front();
        que.pop();
        used.at(p) = true;
        bool isleaf = true;
        for(auto x:graph[p]) {
            if(!used.at(x)) {
                que.push(x);
                rgraph.at(x) = p;
                isleaf = false;
            }
        }
        if(isleaf) leaf.push(p);
    }
    return;
}

void bfs2() {
    cnt.resize(N,0);
    while(!leaf.empty()) {
        int p = leaf.front();
        leaf.pop();
        while(p != -1) {
            cnt.at(p)++;
            p = rgraph.at(p);
        }
    }
    return;
}

int main() {
    cin >> N >> K;
    vec.resize(N - 1);
    cost.resize(N - 1);
    rgraph.resize(N);
    rgraph.at(0) = -1;
    for(int i = 0;i < N - 1;i++) {
        int a,b,c;
        cin >> a >> b >> c;
        a--;
        b--;
        vec.at(i) = {a,b};
        cost.at(i) = c;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    bfs();
    bfs2();
    ll Sum = 0;
    vector<P> vec2(N - 1);
    for(int i = 0;i < N - 1;i++) {
        vec2.at(i) = {min(cnt.at(vec.at(i).first),cnt.at(vec.at(i).second)) * cost.at(i),cost.at(i)};
        Sum += min(cnt.at(vec.at(i).first),cnt.at(vec.at(i).second)) * cost.at(i);
    }
    for(int i = 0;i < N;i++) {
        for(int j = 0;j <= K;j++) {
            dp[i][j] = 0;
        }
    }
    for(int i = 0;i < N - 1;i++) {
        for(int j = 0;j <= K;j++) {
            if(j < vec2.at(i).second) {
                dp[i + 1][j] = dp[i][j];
            }
            else {
                dp[i + 1][j] = max(dp[i][j],dp[i][j - vec2.at(i).second] + vec2.at(i).first);
            }
        }
    }
    cout << Sum + dp[N - 1][K] << endl;
}
0