#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(md == 3) cout<<cost[1]<<endl;
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++){
| ~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
5980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
345 ms |
36236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
343 ms |
38852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
5980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |