結果
| 問題 | No.1488 Max Score of the Tree | 
| コンテスト | |
| ユーザー |  maguro | 
| 提出日時 | 2021-04-06 11:40:57 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 87 ms / 2,000 ms | 
| コード長 | 3,062 bytes | 
| コンパイル時間 | 3,107 ms | 
| コンパイル使用メモリ | 210,012 KB | 
| 最終ジャッジ日時 | 2025-01-20 12:22:09 | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 29 | 
ソースコード
#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;
}
            
            
            
        