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<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<cassert>
#include<limits.h>
#include<cmath>
#include<set>
#include<numeric> //gcd(a,b)
#include<algorithm>
#include<bitset>
#include<stack>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define pppiiii pair<pii,pii>
#define ppii pair<int,pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#define int long long
const int mod=1e9+7,mxn=2e5+5,lg=30,inf=1e18,minf=-1e18,Mxn=100000,root=700;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int n,k,mid;
vector<pii>adj[mxn+10];
int cnt=0;
int dist[mxn+10],last[mxn+10],seen[mxn+10];
vector<int>path;
void putmx(pii &a,int k){
if(dist[a.f]<dist[k])swap(a.f,a.s),a.f=k;
else if(dist[a.s]<dist[k])a.s=k;
}
void check(int cur,int p,int up){
for(auto i:adj[cur])if(i.f!=p)check(i.f,cur,i.s);
seen[cur]=last[cur]=-1;
int c=0;
for(auto i:adj[cur])if(i.f!=p){
c++;
if(seen[i.f]!=-1&&(seen[cur]==-1||dist[seen[cur]]<dist[seen[i.f]]))seen[cur]=seen[i.f];
if(last[i.f]!=-1&&(last[cur]==-1||dist[last[cur]]>dist[last[i.f]]))last[cur]=last[i.f];
}
if(seen[cur]!=-1&&last[cur]!=-1&&dist[seen[cur]]+dist[last[cur]]-2*dist[cur]<=mid)seen[cur]=-1;
if(c==0||(dist[last[cur]]-dist[cur]>mid&&seen[cur]==-1))seen[cur]=cur;
if(seen[cur]!=-1&&dist[seen[cur]]-dist[cur]+up>mid)last[cur]=cur,cnt++,path.pb(cur),seen[cur]=-1;
if(seen[cur]!=-1&&cur==1)last[cur]=cur,cnt++,path.pb(cur),seen[cur]=-1;
}
void getdist(int cur,int p){for(auto i:adj[cur])if(i.f!=p)dist[i.f]=dist[cur]+i.s,getdist(i.f,cur);}
bool on[mxn+10];
int32_t main(){
//setIO("mooriokart");
fastio
cin>>n>>k;
int l=0,r=0;
for(int i=0;i<n-1;i++){
int a,b,c;cin>>a>>b>>c;
r+=c;
adj[a].pb({b,c});
adj[b].pb({a,c});
}
getdist(1,-1);
pair<int,vector<int>>ans={inf,{0}};
while(l<=r){
mid=l+(r-l)/2;
path.clear();
cnt=0;
check(1,-1,0);
if(cnt<=k)r=mid-1,ans=min(ans,{mid,path});
else l=mid+1;
}
for(auto i:ans.s)on[i]=1;
cnt=k-ans.s.size();
for(int i=1;i<=n;i++)if(!on[i]&&cnt)ans.s.pb(i),cnt--;
cout<<ans.f<<'\n';
for(auto i:ans.s)cout<<i<<" ";
}
Compilation message (stderr)
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:36:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:37:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |