#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int N = 2e5 + 5;
vector<pair<int,ll>> adj[N];
pair<int,ll> P[N];
vector<pair<int,int>> leaves;
void dfs(int node,int par,int dep){
if(adj[node].size() == 1 && node != 1) leaves.push_back({dep,node});
for(auto [ch,c] : adj[node]){
if(ch == par) continue;
P[ch] = {node,c};
dfs(ch,node,dep+1);
}
}
void solve(){
int n,k;
cin>>n>>k;
for(int i=0;i<n-1;i++){
int a,b,c;
cin>>a>>b>>c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
dfs(1,0,0);
ll l = 0;
ll r = 1e18;
ll ans = 0;
vector<int> v;
while(l <= r){
ll md = l + r >> 1;
priority_queue<pair<int,int>> q;
vector<bool> vis(n+1);
for(auto i : leaves) q.push(i) , vis[i.second] = 1;
vector<ll> cost(n+1);
vector<ll> covered(n+1,1e18);
int cnt = 0;
vector<int> tmp;
while(q.size()){
auto [dep,node] = q.top(); q.pop();
auto [par,c] = P[node];
if(covered[node] <= md){
covered[par] = min(covered[par] , covered[node] + c);
if(!vis[par]) q.push({dep-1,par}) , vis[par] = 1;
}
else {
if(cost[node] + c <= md){
cost[par] = max(cost[par],cost[node] + c);
if(!vis[par] && par) q.push({dep-1,par}) , vis[par] = 1;
}
else {
tmp.push_back(node);
covered[par] = min(covered[par] , c);
if(!vis[par] && par) q.push({dep-1,par}) , vis[par] = 1;
}
}
}
if((tmp.empty() || tmp.back() != 1) && covered[1] > md) tmp.push_back(1);
if(tmp.size() <= k){
ans = md;
v = tmp;
r = md - 1;
}
else l = md + 1;
}
cout<<ans<<endl;
vector<bool> vis(n+1);
for(auto i : v) vis[i] = 1;
for(int i=1;i<=n&&v.size()!=k;i++){
if(!vis[i]) v.push_back(i);
}
for(auto i : v) cout<<i<<' ';
cout<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
Main.cpp: In function 'void dfs(int, int, int)':
Main.cpp:11:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
11 | for(auto [ch,c] : adj[node]){
| ^
Main.cpp: In function 'void solve()':
Main.cpp:32:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | ll md = l + r >> 1;
| ~~^~~
Main.cpp:41:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | auto [dep,node] = q.top(); q.pop();
| ^
Main.cpp:42:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
42 | auto [par,c] = P[node];
| ^
Main.cpp:60:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | if(tmp.size() <= k){
| ~~~~~~~~~~~^~~~
Main.cpp:38:13: warning: unused variable 'cnt' [-Wunused-variable]
38 | int cnt = 0;
| ^~~
Main.cpp:70:31: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
70 | for(int i=1;i<=n&&v.size()!=k;i++){
| ~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5980 KB |
Output is correct |
4 |
Correct |
2 ms |
6060 KB |
Output is correct |
5 |
Correct |
2 ms |
5976 KB |
Output is correct |
6 |
Correct |
2 ms |
5980 KB |
Output is correct |
7 |
Incorrect |
2 ms |
5848 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
353 ms |
36716 KB |
Output is correct |
2 |
Correct |
350 ms |
39724 KB |
Output is correct |
3 |
Correct |
1622 ms |
30176 KB |
Output is correct |
4 |
Incorrect |
1784 ms |
27796 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
349 ms |
38732 KB |
Output is correct |
2 |
Correct |
345 ms |
39416 KB |
Output is correct |
3 |
Correct |
322 ms |
37828 KB |
Output is correct |
4 |
Correct |
329 ms |
37720 KB |
Output is correct |
5 |
Correct |
363 ms |
43656 KB |
Output is correct |
6 |
Correct |
371 ms |
41508 KB |
Output is correct |
7 |
Correct |
375 ms |
43116 KB |
Output is correct |
8 |
Correct |
353 ms |
40056 KB |
Output is correct |
9 |
Correct |
349 ms |
39580 KB |
Output is correct |
10 |
Correct |
343 ms |
38920 KB |
Output is correct |
11 |
Correct |
328 ms |
37760 KB |
Output is correct |
12 |
Correct |
375 ms |
42864 KB |
Output is correct |
13 |
Correct |
384 ms |
43156 KB |
Output is correct |
14 |
Correct |
374 ms |
41692 KB |
Output is correct |
15 |
Correct |
356 ms |
38188 KB |
Output is correct |
16 |
Correct |
343 ms |
36984 KB |
Output is correct |
17 |
Correct |
342 ms |
36848 KB |
Output is correct |
18 |
Correct |
352 ms |
38420 KB |
Output is correct |
19 |
Correct |
354 ms |
40792 KB |
Output is correct |
20 |
Correct |
372 ms |
40072 KB |
Output is correct |
21 |
Correct |
364 ms |
39804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
5980 KB |
Output is correct |
4 |
Correct |
2 ms |
6060 KB |
Output is correct |
5 |
Correct |
2 ms |
5976 KB |
Output is correct |
6 |
Correct |
2 ms |
5980 KB |
Output is correct |
7 |
Incorrect |
2 ms |
5848 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |