#include <bits/stdc++.h>
#define pb push_back
#define int long long
using namespace std;
const int mod = 998244353;
const int inf = 1e17;
const int N = 2e5+5;
const int W = 1e9;
vector<pair<int, int> > adj[N];
vector<int> distopar(N);
vector<int> taken;
pair<int, int> dfs(int node, int par, int limit, bool tk){ // max distance, park count
vector<pair<int, int> > ch;
for(auto itr: adj[node]){
if(itr.first != par){
distopar[itr.first] = itr.second;
pair<int, int> p = dfs(itr.first, node, limit, tk);
ch.pb(p);
}
}
pair<int, int> calc = {0, 0};
for(auto itr: ch){
calc.second += itr.second;
calc.first = max(calc.first, itr.first);
}
if(calc.first + distopar[node] > limit){
calc.first = 0;
calc.second++;
if(tk) taken.pb(node);
}
else{
calc.first += distopar[node];
}
return calc;
}
int32_t main(){
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
int n, k;
cin>>n>>k;
for(int i = 0; i < n-1; i++){
int u, v, c;
cin>>u>>v>>c;
adj[u].pb({v, c});
adj[v].pb({u, c});
}
int l = 0, r = W*N;
while(l < r){
int m = (l + r)/2;
pair<int, int> check = dfs(1, 1, m, 0);
if(check.second <= k) r = m;
else l = m + 1;
}
cout<<l<<endl;
dfs(1, 1, l, 1);
for(auto itr: taken){
cout<<itr<<" ";
}
cout<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
473 ms |
41804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
595 ms |
44620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |