#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){
if (a.size() > b.size){
a.insert(a.end(),b.begin(),b.end());
return a;
}
else {
b.insert(b.end(),a.begin(),a.end());
return b;
}
}
bool asd;
int n,k;
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];
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 'std::vector<long long int> operator+(std::vector<long long int>, std::vector<long long int>)':
Main.cpp:18:20: error: invalid use of member function 'std::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::size() const [with _Tp = long long int; _Alloc = std::allocator<long long int>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]' (did you forget the '()' ?)
18 | if (a.size() > b.size){
| ~~^~~~
| ()
Main.cpp: In function 'void dfs1(long long int, long long int)':
Main.cpp:43:22: 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]
43 | 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:53: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]
53 | if (tmp.size() > k) tmp.resize(k);
| ~~~~~~~~~~~^~~
Main.cpp:68: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]
68 | if (tmp.size() > k) tmp.resize(k);
| ~~~~~~~~~~~^~~
Main.cpp: In function 'std::vector<long long int> operator+(std::vector<long long int>, std::vector<long long int>)':
Main.cpp:26:1: warning: control reaches end of non-void function [-Wreturn-type]
26 | }
| ^