This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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=2e5+25;
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){
set<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({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,greater<int>>take,rest;
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);
}
}
}
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});
}
for(int root=1;root<=n;++root){
for(int i=1;i<=n;++i){
dist[i]=down[i]=up[i]=a[i]=val[i]=0;
}
take.clear();
rest.clear();
pre_calc1(root,root);
up[root]=root;
pre_calc2(root,root);
dfs(root,root);
int cnt=0;
while(cnt++<k){
int beg=*rest.begin();
take.insert(beg);
rest.erase(rest.find(beg));
}
int ans=0;
for(int it:take){
ans+=it;
}
cout<<ans<<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
**/
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int t=1;
// cin>>t;
while(t--){
solve();
}
}
# | 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... |