This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, int> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
#define mod 1000000007
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
#define maxn 100005
int n,k;
vii AL[maxn];
ll ans[maxn],mx[maxn],cur;
multiset<ll> s1,s2;
void add(ll x){
s1.insert(x);
cur+=x;
if(sz(s1)>k){
cur-=*s1.begin();
s2.insert(*s1.begin());
s1.erase(s1.begin());
}
}
void rem(ll x){
if(s1.find(x)!=s1.end()){
cur-=x;
s1.erase(s1.find(x));
if(!s2.empty()){
ll v=*(--s2.end());
cur+=v;
s1.insert(v);
s2.erase(--s2.end());
}
}
else{
if(s2.find(x)!=s2.end())s2.erase(s2.find(x));
}
}
void dfs(int u,int p){
mx[u]=0;
if(sz(AL[u])==1&&p!=-1){
add(0);
return;
}
for(auto[v,w]:AL[u]){
if(v==p)continue;
dfs(v,u);
rem(mx[v]);
add(mx[v]+w);
mxto(mx[u],mx[v]+w);
}
}
void reroot(int u,int p){
//pf("reroot: %d %d\n",u,p);
ans[u]=cur;
pll f={0,u},s={0,u};
for(auto[v,w]:AL[u]){
if(mx[v]+w>=f.fi){
s=f;
f={mx[v]+w,v};
}
else if(mx[v]+w>s.fi){
s={mx[v]+w,v};
}
}
for(auto[v,w]:AL[u]){
if(v==p)continue;
//pf("%d\n",v);
ll t=mx[u];
if(f.se==v)mx[u]=s.fi;
else mx[u]=f.fi;
rem(mx[v]+w);
add(mx[v]);
rem(mx[u]);
add(mx[u]+w);
reroot(v,u);
rem(mx[u]+w);
add(mx[u]);
rem(mx[v]);
add(mx[v]+w);
mx[u]=t;
//pf("%d:\n",u);
//for(ll i:s1)pf("%lld ",i);pf("\n");
//for(ll i:s2)pf("%lld ",i);pf("\n");
}
}
int main(){
sf("%d%d",&n,&k);
for(int i=1;i<n;++i){
int x,y,c;
sf("%d%d%d",&x,&y,&c);
AL[x].pb({y,c});
AL[y].pb({x,c});
}
dfs(1,-1);
reroot(1,-1);
for(int i=1;i<=n;++i)pf("%lld\n",ans[i]);
}
/*
11 3
1 2 5
2 3 3
2 6 5
3 4 4
3 5 2
1 7 6
7 8 4
7 9 5
1 10 1
10 11 1
*/
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:141:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | sf("%d%d",&n,&k);
| ^
Main.cpp:144:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
144 | sf("%d%d%d",&x,&y,&c);
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |