#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int c=1e5+5;
int n,k;
multiset<ll> nagy,kicsi;
ll ossz=0;
void ins(ll a){
if(nagy.size()<k) {ossz+=a; nagy.insert(a); return;}
ll nb=*nagy.begin();
if(nb>=a) {kicsi.insert(a); return;}
kicsi.insert(nb);
ossz-=nb;
nagy.erase(nagy.find(nb));
ossz+=a; nagy.insert(a);
}
void rem(ll a){
if(kicsi.find(a) != kicsi.end()){
kicsi.erase(kicsi.find(a)); return;
}
if(nagy.find(a) == nagy.end()) return;
ossz-=a;
nagy.erase(nagy.find(a));
if(!kicsi.empty()) {auto it=kicsi.end(); it--; ll b=*it; ossz+=b; nagy.insert(b); kicsi.erase(kicsi.find(b));}
}
vector<pair<int,ll>> adj[c];
ll dp[c][2];
ll ans[c];
ll dfs(int v, int p, ll len){
ll maxi=0;
for(pair<int,ll> x: adj[v]){
if(x.first != p){
ll cur=dfs(x.first,v,x.second);
if(cur>dp[v][1]) dp[v][1]=cur;
if(dp[v][1]>dp[v][0]) swap(dp[v][1],dp[v][0]);
if(cur>maxi){
swap(cur,maxi);
}
ins(cur);
}
}
return maxi+len;
}
void dfs2(int v, int p){
ans[v]=ossz;
for(pair<int,ll> x: adj[v]){
if(x.first != p){
ll A=dp[x.first][0],B=dp[x.first][1];
rem(dp[x.first][0]+x.second);
ins(dp[x.first][0]);
bool b=dp[v][0]==(dp[x.first][0]+x.second);
rem(!b?dp[v][0]:dp[v][1]);
ins((!b?dp[v][0]:dp[v][1])+x.second);
dp[x.first][1]=max((!b?dp[v][0]:dp[v][1])+x.second,dp[x.first][1]);
if(dp[x.first][0]<dp[x.first][1]) swap(dp[x.first][0],dp[x.first][1]);
dfs2(x.first,v);
dp[x.first][0]=A; dp[x.first][1]=B;
rem((!b?dp[v][0]:dp[v][1])+x.second);
ins(!b?dp[v][0]:dp[v][1]);
rem(dp[x.first][0]);
ins(dp[x.first][0]+x.second);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<n;i++){
int a,b,val;
cin>>a>>b>>val;
adj[a].push_back(make_pair(b,val));
adj[b].push_back(make_pair(a,val));
}
ins(dfs(1,0,0));
dfs2(1,0);
for(int i=1;i<=n;i++){
cout << ans[i] << '\n';
}
}
Compilation message
Main.cpp: In function 'void ins(ll)':
Main.cpp:13:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
13 | if(nagy.size()<k) {ossz+=a; nagy.insert(a); return;}
| ~~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
5 ms |
2772 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
5 ms |
2772 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
5 ms |
2900 KB |
Output is correct |
14 |
Correct |
5 ms |
3028 KB |
Output is correct |
15 |
Correct |
5 ms |
2900 KB |
Output is correct |
16 |
Correct |
6 ms |
2900 KB |
Output is correct |
17 |
Correct |
4 ms |
2900 KB |
Output is correct |
18 |
Correct |
4 ms |
2900 KB |
Output is correct |
19 |
Correct |
6 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
280 ms |
15868 KB |
Output is correct |
2 |
Correct |
286 ms |
19960 KB |
Output is correct |
3 |
Correct |
208 ms |
17892 KB |
Output is correct |
4 |
Correct |
278 ms |
17856 KB |
Output is correct |
5 |
Correct |
291 ms |
18780 KB |
Output is correct |
6 |
Correct |
274 ms |
17768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
5 ms |
2772 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
5 ms |
2900 KB |
Output is correct |
14 |
Correct |
5 ms |
3028 KB |
Output is correct |
15 |
Correct |
5 ms |
2900 KB |
Output is correct |
16 |
Correct |
6 ms |
2900 KB |
Output is correct |
17 |
Correct |
4 ms |
2900 KB |
Output is correct |
18 |
Correct |
4 ms |
2900 KB |
Output is correct |
19 |
Correct |
6 ms |
2900 KB |
Output is correct |
20 |
Correct |
280 ms |
15868 KB |
Output is correct |
21 |
Correct |
286 ms |
19960 KB |
Output is correct |
22 |
Correct |
208 ms |
17892 KB |
Output is correct |
23 |
Correct |
278 ms |
17856 KB |
Output is correct |
24 |
Correct |
291 ms |
18780 KB |
Output is correct |
25 |
Correct |
274 ms |
17768 KB |
Output is correct |
26 |
Correct |
318 ms |
18424 KB |
Output is correct |
27 |
Correct |
306 ms |
19980 KB |
Output is correct |
28 |
Correct |
304 ms |
20416 KB |
Output is correct |
29 |
Correct |
216 ms |
17996 KB |
Output is correct |
30 |
Correct |
307 ms |
18256 KB |
Output is correct |
31 |
Correct |
293 ms |
17612 KB |
Output is correct |
32 |
Correct |
315 ms |
19036 KB |
Output is correct |
33 |
Correct |
296 ms |
18328 KB |
Output is correct |
34 |
Correct |
204 ms |
18036 KB |
Output is correct |
35 |
Correct |
316 ms |
18224 KB |
Output is correct |
36 |
Correct |
285 ms |
20992 KB |
Output is correct |