#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3ulnVOy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<"\n";}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
const int inf=1e9+11;
const ll lnf=1e15+11;
using pll=pair<ll,ll>;
using vp=vec(pll);
signed main(){
_3ulnVOy;
int n,k;
cin>>n>>k;
vec(vp) adj(n);
rep(i,n-1){
int u,v,w;
cin>>u>>v>>w;
u-=1,v-=1;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
int cnt=0;
vi pns;
auto dfs=[&](auto self,int v,int par,int can)->pll{
pll now={0,-lnf};
vec(pair<int,pll>) cands;
for(auto e:adj[v]){
auto [u,w]=e;
if(u==par) continue;
pll nep=self(self,u,v,can);
if(nep.fi+w>can){
pns.pb(u);
now.se=max(now.se,can-w);
cnt+=1;
}else{
now.se=max(now.se,nep.se-w);
cands.pb({w,nep});
}
}
for(auto tp:cands){
ll w=tp.fi;
pll p=tp.se;
now.fi=max(now.fi,p.fi+w);
}
if(now.fi<=now.se){
now.fi=-lnf;
}
return now;
};
auto quq=[&](int x)->bool{
cnt=0;
pns.clear();
pll p=dfs(dfs,0,-1,x);
if(p.fi!=-lnf){
cnt+=1;
pns.pb(0);
}
return cnt<=k;
// print("\n...",cnt);
};
int c;
{
int l=1,r=inf;
while(l<=r){
int m=(l+r)/2;
if(quq(m)){
c=m;
r=m-1;
}else{
l=m+1;
}
}
quq(c);
}
{
vi usd(n,0);
rep(i,sz(pns)){
usd[pns[i]]=1;
}
rep(i,n){
if(!usd[i] and sz(pns)<k){
pns.pb(i);
}
}
}
print(c);
for(auto v:pns){
cout<<v+1<<" ";
}
print();
//
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:17:43: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
17 | void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
| ~~~~~~~^~~~~
Main.cpp:79:6: note: 'c' was declared here
79 | int c;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
454 ms |
52396 KB |
Output is correct |
2 |
Incorrect |
474 ms |
55192 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
479 ms |
56088 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |