Submission #1291570

#TimeUsernameProblemLanguageResultExecution timeMemory
1291570jahongirPaths (RMI21_paths)C++20
36 / 100
683 ms32016 KiB
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
template<typename T> using ordered_set = tree<T,null_type,less_equal<T>,rb_tree_tag,
					 tree_order_statistics_node_update>;

#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define pb push_back
#define all(a) a.begin(),a.end()  


const int mxn = 2001;
vector<pi> g[mxn];
ll dp[mxn][mxn], res[mxn];
int sub[mxn];

int n,k;

void pre_dfs(int u, int p){
    sub[u] = 1;

    for(auto [v,c] : g[u]) if(v!=p){
        pre_dfs(v,u);

        for(int i = sub[u]; i >= 0; i--)
            for(int j = sub[v]; j >= 1; j--)
                dp[u][i+j] = max(dp[u][i+j],dp[u][i]+dp[v][j]+c);
            

        sub[u] += sub[v];
    }
}


void dfs(int u, int p){
    for(int i = 1; i <= k; i++) dp[u][i] = 0;

    sub[u] = 1;
    for(auto [v,c] : g[u]){
        for(int i = sub[u]; i >= 0; i--)
            for(int j = sub[v]; j >= 1; j--)
                dp[u][i+j] = max(dp[u][i+j],dp[u][i]+dp[v][j]+c);
        sub[u]+=sub[v];
    }

    res[u] = dp[u][k];


    for(auto [v,c] : g[u]) if(v!=p){
        sub[v] = n;

        for(int i = 1; i <= n; i++) dp[u][i] = 0;
        
        sub[u] = 1;
        for(auto [v2,c2] : g[u]) if(v2!=v){
            for(int i = sub[u]; i >= 0; i--)
                for(int j = sub[v2]; j >= 1; j--)
                    dp[u][i+j] = max(dp[u][i+j],dp[u][i]+dp[v2][j]+c2);
            sub[u] += sub[v2];
        }

        dfs(v,u);
        sub[v] = n-sub[u];
    }

    for(int i = 1; i <= n; i++) dp[u][i] = 0;
    
    sub[u] = 1;
    for(auto [v,c] : g[u]) if(v!=p){
        for(int i = sub[u]; i >= 0; i--)
            for(int j = sub[v]; j >= 1; j--)
                dp[u][i+j] = max(dp[u][i+j],dp[u][i]+dp[v][j]+c);
        sub[u] += sub[v];
    }

}





void solve(){
    cin >> n >> k;

    if(n > 2000) return;

    for(int i = 1; i < n; i++){
        int u,v,c; cin >> u >> v >> c;
        g[u].pb({v,c}); g[v].pb({u,c});
    }


    pre_dfs(1,1);
    dfs(1,1);

    for(int i = 1; i <= n; i++)
        cout << res[i] << '\n';

}



signed main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    while(t--){solve();}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...