#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long int
#define ins insert
#define pb push_back
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
#define _ << " " <<
const int mod = 1e9+7 ,
sze= 2e5 + 23 ,
inf = 2e14,
L = 31 ;
vector<pair<int,int>> graph[sze];
vector<int> alst;
int dp[sze][2];
void dfs(int node,int par,int mid){
for(auto [a,b]:graph[node]){
if(a!=par){
dfs(a,node,mid);
dp[node][0] = min(dp[node][0],dp[a][0] + b);
if(dp[a][1] + b <= mid){
dp[node][1]=max(dp[node][1],dp[a][1] + b);
}
else{
// cout<<mid<<" => "<<a<<endl;
alst.pb(a);
dp[node][0]=min(dp[node][0],b);
}
}
}
if(dp[node][0] + dp[node][1] <= mid){
dp[node][1]=-inf;
}
// cout<<mid<<"=> " _ node _ dp[node][0] _ dp[node][1]<<endl;
}
int check(int mid){
alst.clear();
for(int i=0;i<sze;i++){
dp[i][0]=inf;
dp[i][1]=0;
}
dfs(1,0,mid);
if(dp[1][0] + dp[1][1] > mid){
alst.pb(1);
}
return alst.size();
}
void opt1z(){
int n,k;
cin>>n>>k;
for(int i =1;i<n;i++){
int u,v,w;
cin>>u>>v>>w;
graph[u].pb({v,w});
graph[v].pb({u,w});
}
int l =1;
int r = (int)2e9 * (int)2e5;
int ans=1;
while(l<=r){
int mid = (l+r)/2;
if(check(mid)<=k){
ans = mid;
r = mid -1;
}
else{
l = mid+1;
}
}
check(ans);
set<int> a;
for(auto v:alst){
a.ins(v);
}
for(int i=1;i<=n;i++){
if(a.size()<k && a.find(i)==a.end()){
a.ins(i);
// cout<< "new " _ i<<endl;
}
}
cout<<ans<<"\n";
for(auto v:a) cout<<v<<" ";cout<<endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
// cin>>tt;
while(tt--){
opt1z();
}
return 0;
}
Compilation message
Main.cpp: In function 'void dfs(long long int, long long int, long long int)':
Main.cpp:18:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
18 | for(auto [a,b]:graph[node]){
| ^
Main.cpp: In function 'void opt1z()':
Main.cpp:83:20: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
83 | if(a.size()<k && a.find(i)==a.end()){
| ~~~~~~~~^~
Main.cpp:89:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
89 | for(auto v:a) cout<<v<<" ";cout<<endl;
| ^~~
Main.cpp:89:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
89 | for(auto v:a) cout<<v<<" ";cout<<endl;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8536 KB |
Output is correct |
2 |
Correct |
9 ms |
8284 KB |
Output is correct |
3 |
Correct |
10 ms |
8284 KB |
Output is correct |
4 |
Correct |
10 ms |
8284 KB |
Output is correct |
5 |
Correct |
9 ms |
8284 KB |
Output is correct |
6 |
Correct |
10 ms |
8288 KB |
Output is correct |
7 |
Correct |
9 ms |
8284 KB |
Output is correct |
8 |
Correct |
11 ms |
8284 KB |
Output is correct |
9 |
Correct |
10 ms |
8284 KB |
Output is correct |
10 |
Correct |
10 ms |
8284 KB |
Output is correct |
11 |
Correct |
12 ms |
8288 KB |
Output is correct |
12 |
Correct |
10 ms |
8284 KB |
Output is correct |
13 |
Correct |
17 ms |
8284 KB |
Output is correct |
14 |
Correct |
10 ms |
8232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
276 ms |
37616 KB |
Output is correct |
2 |
Correct |
317 ms |
42576 KB |
Output is correct |
3 |
Correct |
158 ms |
22964 KB |
Output is correct |
4 |
Correct |
454 ms |
22088 KB |
Output is correct |
5 |
Correct |
426 ms |
21584 KB |
Output is correct |
6 |
Correct |
405 ms |
21824 KB |
Output is correct |
7 |
Correct |
442 ms |
20412 KB |
Output is correct |
8 |
Correct |
466 ms |
21064 KB |
Output is correct |
9 |
Correct |
406 ms |
21584 KB |
Output is correct |
10 |
Correct |
439 ms |
21584 KB |
Output is correct |
11 |
Correct |
403 ms |
23928 KB |
Output is correct |
12 |
Correct |
402 ms |
23892 KB |
Output is correct |
13 |
Correct |
430 ms |
25332 KB |
Output is correct |
14 |
Correct |
376 ms |
22356 KB |
Output is correct |
15 |
Correct |
379 ms |
21892 KB |
Output is correct |
16 |
Correct |
396 ms |
23476 KB |
Output is correct |
17 |
Correct |
351 ms |
22612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
299 ms |
38740 KB |
Output is correct |
2 |
Correct |
276 ms |
42320 KB |
Output is correct |
3 |
Correct |
250 ms |
40532 KB |
Output is correct |
4 |
Correct |
224 ms |
40276 KB |
Output is correct |
5 |
Correct |
348 ms |
53016 KB |
Output is correct |
6 |
Correct |
328 ms |
47816 KB |
Output is correct |
7 |
Correct |
352 ms |
49852 KB |
Output is correct |
8 |
Correct |
277 ms |
42832 KB |
Output is correct |
9 |
Correct |
289 ms |
42568 KB |
Output is correct |
10 |
Correct |
266 ms |
41772 KB |
Output is correct |
11 |
Correct |
273 ms |
40484 KB |
Output is correct |
12 |
Correct |
360 ms |
53960 KB |
Output is correct |
13 |
Correct |
371 ms |
51396 KB |
Output is correct |
14 |
Correct |
341 ms |
47812 KB |
Output is correct |
15 |
Correct |
279 ms |
41044 KB |
Output is correct |
16 |
Correct |
247 ms |
39760 KB |
Output is correct |
17 |
Correct |
242 ms |
39628 KB |
Output is correct |
18 |
Correct |
277 ms |
41556 KB |
Output is correct |
19 |
Correct |
275 ms |
50372 KB |
Output is correct |
20 |
Correct |
294 ms |
48764 KB |
Output is correct |
21 |
Correct |
269 ms |
46620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8536 KB |
Output is correct |
2 |
Correct |
9 ms |
8284 KB |
Output is correct |
3 |
Correct |
10 ms |
8284 KB |
Output is correct |
4 |
Correct |
10 ms |
8284 KB |
Output is correct |
5 |
Correct |
9 ms |
8284 KB |
Output is correct |
6 |
Correct |
10 ms |
8288 KB |
Output is correct |
7 |
Correct |
9 ms |
8284 KB |
Output is correct |
8 |
Correct |
11 ms |
8284 KB |
Output is correct |
9 |
Correct |
10 ms |
8284 KB |
Output is correct |
10 |
Correct |
10 ms |
8284 KB |
Output is correct |
11 |
Correct |
12 ms |
8288 KB |
Output is correct |
12 |
Correct |
10 ms |
8284 KB |
Output is correct |
13 |
Correct |
17 ms |
8284 KB |
Output is correct |
14 |
Correct |
10 ms |
8232 KB |
Output is correct |
15 |
Correct |
276 ms |
37616 KB |
Output is correct |
16 |
Correct |
317 ms |
42576 KB |
Output is correct |
17 |
Correct |
158 ms |
22964 KB |
Output is correct |
18 |
Correct |
454 ms |
22088 KB |
Output is correct |
19 |
Correct |
426 ms |
21584 KB |
Output is correct |
20 |
Correct |
405 ms |
21824 KB |
Output is correct |
21 |
Correct |
442 ms |
20412 KB |
Output is correct |
22 |
Correct |
466 ms |
21064 KB |
Output is correct |
23 |
Correct |
406 ms |
21584 KB |
Output is correct |
24 |
Correct |
439 ms |
21584 KB |
Output is correct |
25 |
Correct |
403 ms |
23928 KB |
Output is correct |
26 |
Correct |
402 ms |
23892 KB |
Output is correct |
27 |
Correct |
430 ms |
25332 KB |
Output is correct |
28 |
Correct |
376 ms |
22356 KB |
Output is correct |
29 |
Correct |
379 ms |
21892 KB |
Output is correct |
30 |
Correct |
396 ms |
23476 KB |
Output is correct |
31 |
Correct |
351 ms |
22612 KB |
Output is correct |
32 |
Correct |
299 ms |
38740 KB |
Output is correct |
33 |
Correct |
276 ms |
42320 KB |
Output is correct |
34 |
Correct |
250 ms |
40532 KB |
Output is correct |
35 |
Correct |
224 ms |
40276 KB |
Output is correct |
36 |
Correct |
348 ms |
53016 KB |
Output is correct |
37 |
Correct |
328 ms |
47816 KB |
Output is correct |
38 |
Correct |
352 ms |
49852 KB |
Output is correct |
39 |
Correct |
277 ms |
42832 KB |
Output is correct |
40 |
Correct |
289 ms |
42568 KB |
Output is correct |
41 |
Correct |
266 ms |
41772 KB |
Output is correct |
42 |
Correct |
273 ms |
40484 KB |
Output is correct |
43 |
Correct |
360 ms |
53960 KB |
Output is correct |
44 |
Correct |
371 ms |
51396 KB |
Output is correct |
45 |
Correct |
341 ms |
47812 KB |
Output is correct |
46 |
Correct |
279 ms |
41044 KB |
Output is correct |
47 |
Correct |
247 ms |
39760 KB |
Output is correct |
48 |
Correct |
242 ms |
39628 KB |
Output is correct |
49 |
Correct |
277 ms |
41556 KB |
Output is correct |
50 |
Correct |
275 ms |
50372 KB |
Output is correct |
51 |
Correct |
294 ms |
48764 KB |
Output is correct |
52 |
Correct |
269 ms |
46620 KB |
Output is correct |
53 |
Correct |
423 ms |
21940 KB |
Output is correct |
54 |
Correct |
438 ms |
22352 KB |
Output is correct |
55 |
Correct |
482 ms |
22868 KB |
Output is correct |
56 |
Correct |
472 ms |
23240 KB |
Output is correct |
57 |
Correct |
534 ms |
27340 KB |
Output is correct |
58 |
Correct |
418 ms |
22052 KB |
Output is correct |
59 |
Correct |
568 ms |
34504 KB |
Output is correct |
60 |
Correct |
453 ms |
21428 KB |
Output is correct |
61 |
Correct |
381 ms |
20048 KB |
Output is correct |
62 |
Correct |
450 ms |
26312 KB |
Output is correct |
63 |
Correct |
498 ms |
21328 KB |
Output is correct |
64 |
Correct |
501 ms |
31936 KB |
Output is correct |
65 |
Correct |
445 ms |
21856 KB |
Output is correct |
66 |
Correct |
497 ms |
26824 KB |
Output is correct |
67 |
Correct |
396 ms |
21068 KB |
Output is correct |
68 |
Correct |
583 ms |
33988 KB |
Output is correct |
69 |
Correct |
296 ms |
42404 KB |
Output is correct |
70 |
Correct |
238 ms |
40016 KB |
Output is correct |
71 |
Correct |
367 ms |
48436 KB |
Output is correct |
72 |
Correct |
139 ms |
22036 KB |
Output is correct |
73 |
Correct |
141 ms |
22196 KB |
Output is correct |
74 |
Correct |
185 ms |
27224 KB |
Output is correct |
75 |
Correct |
369 ms |
24400 KB |
Output is correct |
76 |
Correct |
400 ms |
23636 KB |
Output is correct |
77 |
Correct |
377 ms |
23756 KB |
Output is correct |
78 |
Correct |
396 ms |
25312 KB |
Output is correct |