#pragma GCC optimize("Ofast")
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma GCC optimize("inline,Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,avx,abm")
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define sz(x) (int)(x).size()
#define int long long
#define FOR(i,l,r) for(int i=l;i<=(r);++i)
#define FORd(i,r,l) for(int i=r;i>=(l);--i)
#define pb push_back
#define x first
#define y second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(),x.end()
#define ins insert
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
typedef vector<ll>vll;
typedef vector<int>vi;
typedef vector<bool>vb;
typedef vector<vi>vvi;
typedef vector<vll>vvll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
const int N=8;
const int MOD=1e9+7;
const int INF=1e18;
const char nl='\n';
int n,k;
vpii g[N];
int dist[N];
int down[N];
int up[N];
void pre_calc1(int v,int p){
for(auto [to,w]:g[v]){
if(to!=p){
dist[to]=dist[v]+w;
pre_calc1(to,v);
}
}
down[v]=v;
for(auto [to,w]:g[v]){
if(to!=p){
if(dist[down[to]]>=dist[down[v]]){
down[v]=down[to];
}
}
}
}
void pre_calc2(int v,int p){
multiset<pii,greater<pii>>st;
for(auto [to,w]:g[v]){
if(to!=p){
st.insert({dist[down[to]]-dist[v],down[to]});
}
}
for(auto [to,w]:g[v]){
if(to!=p){
up[to]=up[v];
st.erase(st.find({dist[down[to]]-dist[v],down[to]}));
pii best=*st.begin();
if(!st.empty() && w+best.x>dist[to]+dist[up[to]]){
up[to]=best.y;
}
st.insert({dist[down[to]]-dist[v],down[to]});
}
}
for(auto [to,w]:g[v]){
if(to!=p){
pre_calc2(to,v);
}
}
}
int a[N];
int val[N];
multiset<int>take,rest;
int sum=0;
void change(int x,int y){
if(rest.find(x)!=rest.end()){
int z=*take.upper_bound(0);
if(y>z){
take.insert(y);
take.erase(take.find(z));
rest.erase(rest.find(x));
rest.insert(z);
sum+=y-z;
}
else{
rest.erase(rest.find(x));
rest.insert(y);
}
}
else{
int z=*rest.rbegin();
if(z>y){
take.insert(z);
take.erase(take.find(x));
rest.erase(rest.find(z));
rest.insert(y);
sum+=z-x;
}
else{
take.erase(take.find(x));
take.insert(y);
sum+=y-x;
}
}
}
void dfs(int v,int p){
if(!a[down[v]]){
a[down[v]]=p;
val[down[v]]=dist[down[v]]-dist[p];
rest.insert(val[down[v]]);
}
for(auto [to,w]:g[v]){
if(to!=p){
dfs(to,v);
}
}
}
int ans[N];
void re_root(int v,int p){
for(auto [to,w]:g[v]){
if(to!=p){
int x1,y1,x2,y2;
x1=dist[v]+dist[up[to]];
y1=x1+w;
x2=dist[down[to]]-dist[v];
y2=x2-w;
// cout<<v<<" -> "<<to<<": "<<x1<<' '<<y1<<" and "<<x2<<' '<<y2<<nl;
change(x1,y1);
change(x2,y2);
ans[to]=sum;
re_root(to,v);
change(y1,x1);
change(y2,x2);
}
}
}
void solve(){
cin>>n>>k;
for(int i=1;i<n;++i){
int u,v,w;
cin>>u>>v>>w;
g[u].pb({v,w});
g[v].pb({u,w});
}
pre_calc1(1,1);
up[1]=1;
pre_calc2(1,1);
dfs(1,1);
int cnt=0;
for(int i=1;i<=n+k;++i){
rest.insert(0);
}
while(cnt++<k){
int beg=*rest.rbegin();
take.insert(beg);
sum+=beg;
rest.erase(rest.find(beg));
}
ans[1]=sum;
re_root(1,1);
for(int i=1;i<=n;++i){
cout<<ans[i]<<nl;
}
}
/**
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
5 1
1 2 1
2 3 2
1 4 1
4 5 5
18 1
1 2 5
1 3 4
1 4 11
2 5 10
2 6 10
2 7 6
5 11 5
5 12 3
7 13 1
3 8 7
4 9 1
4 10 4
9 14 3
9 15 5
10 16 2
10 17 2
10 18 3
7 1
1 2 1
2 3 2
1 4 3
4 5 2
1 6 5
6 7 1
**/
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int t=1;
// cin>>t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
392 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |