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<vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using plb = pair<ll, bool>;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define PB push_back
#define F first
#define S second
const ll INF = 200000000000010;
const int MAXN = 200010;
int n, k;
bool pres[MAXN];
vector<int> ans;
ll target;
vector<pii> g[MAXN];
plb dfs(int v, int p, ll cost){
ll mxD=0, mxS=-1;
for(pii to:g[v]) if(to.F!=p){
plb res = dfs(to.F, v, to.S);
if(res.S) mxS = max(mxS, res.F - to.S);
else mxD = max(mxD, res.F + to.S);
}
if(mxD <= mxS) return {mxS, true};
else if(mxD + cost > target){
ans.PB(v);
return {target, true};
}
else return {mxD, false};
}
int main(){
scanf("%d %d", &n, &k);
forn(i, n-1){
int a, b, c; scanf("%d %d %d", &a, &b, &c); --a, --b;
g[a].PB({b, c}), g[b].PB({a, c});
}
ll lo=-1, hi=INF;
while(hi-lo>1){
ll mid = (hi+lo)/2;
ans.clear(), target=mid;
dfs(0, 0, 2*INF);
if((int)ans.size()<=k) hi=mid;
else lo=mid;
}
ans.clear();
target=hi;
dfs(0, 0, 2*INF);
for(int el:ans) pres[el]=true;
forn(i, n) if(!pres[i] && (int)ans.size()<k) ans.PB(i), pres[i]=true;
printf("%lld\n", hi);
forn(i, n) if(pres[i]) printf("%d ", i+1);
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:39:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | int a, b, c; scanf("%d %d %d", &a, &b, &c); --a, --b;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |