#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 1e6 + 15;
const ll inf = 1e18;
vector<pii> node[N];
vector<int> suff[N];
vector<int> operator+(vector<int> a,vector<int> b){
a.insert(a.end(),b.begin(),b.end());
return a;
}
bool asd;
void dfs1(int i,int p){
// to calc the suff
if (sz(node[i])==1&&p!=-1){
suff[i] = {0};
return;
}
for (auto [it,w] : node[i]){
if (it==p) continue;
dfs1(it,i);
suff[it].front() += w;
suff[i] = suff[i]+suff[it];
suff[it].front() -= w;
}
sort(all(suff[i]),greater<int>());
if (suff[i].size() > k) suff[i].resize(k);
}
int ans[N];
int n,k;
void dfs2(int i,int p,vector<int> cur){
// calc the answer
// fix the cur for the next child
// idk
vector<int> tmp = cur + suff[i];
sort(all(tmp),greater<int>());
if (tmp.size() > k) tmp.resize(k);
for (int j=0;j<k;j++){
ans[i] += tmp[j];
}
for (auto [it1,w1] : node[i]){
if (it1==p) continue;
tmp = cur;
for (auto [it2,w2] : node[i]){
if (it2!=p&&it2!=it1){
suff[it2].front() += w2;
tmp = tmp + suff[it2];
suff[it2].front() -= w2;
}
}
sort(all(tmp),greater<int>());
if (tmp.size() > k) tmp.resize(k);
tmp.front() += w1;
dfs2(it1,i,tmp);
}
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> k;
if (k==1) asd = 1;
for (int i=0;i<n-1;i++){
int u,v,w;
cin >> u >> v >> w;
node[u].pb({v,w});
node[v].pb({u,w});
}
dfs1(1,-1);
dfs2(1,-1,{});
for (int i=1;i<=n;i++){
cout << ans[i] << endl;
}
}
Compilation message
Main.cpp: In function 'void dfs1(long long int, long long int)':
Main.cpp:36:24: error: 'k' was not declared in this scope
36 | if (suff[i].size() > k) suff[i].resize(k);
| ^
Main.cpp: In function 'void dfs2(long long int, long long int, std::vector<long long int>)':
Main.cpp:46:18: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
46 | if (tmp.size() > k) tmp.resize(k);
| ~~~~~~~~~~~^~~
Main.cpp:61:20: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
61 | if (tmp.size() > k) tmp.resize(k);
| ~~~~~~~~~~~^~~