#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
constexpr int N = 2e5 + 5;
constexpr int INF = 1e18;
int n,k;
vector<array<int,2>> v[N];
bool ok=1;
int vis[N];
int dp[N];
int depth[N];
int maxi[N];
int ck;
void dfs(int c,int p,int d){
if(!ok) return;
depth[c]=d;
maxi[c]=d;
for(array<int,2> x:v[c]){
int u=x[0],co=x[1];
if(u==p) continue;
dfs(u,c,d+co);
dp[c]=min(dp[c],dp[u]);
maxi[c]=max(maxi[c],maxi[u]);
}
if(maxi[c]<=ck+2*d-dp[c]) {
maxi[c]=-INF;
return;
}
int u = maxi[c];
if(u>ck+d){
ok=0;
return;
}
if(c==1){
vis[c]=1;
dp[c]=d;
maxi[c]=-INF;
return;
}
if(u-depth[p]>ck){
vis[c]=1;
dp[c]=d;
maxi[c]=-INF;
return;
}
}
bool check(int x){
ok=1;
fill(dp,dp+N,INF);
fill(vis,vis+N,0);
fill(maxi,maxi+N,-INF);
ck=x;
dfs(1,0,0);
if(ok && count(vis,vis+N,1)<=k) return 1;
return 0;
}
void solve(){
cin >> n >> k;
for(int i=1;i<n;i++){
int a,b,c;
cin >> a >> b >> c;
v[a].pb({b,c});
v[b].pb({a,c});
}
int l=0,r=(int)1e15;
while(l<r){
int m=(l+r)/2;
if(check(m)) r=m;
else l=m+1;
}
cout << l << endl;
check(l);
vector<int> ans;
for(int i=1;i<=n;i++) if(vis[i]) ans.pb(i);
for(int i=1;i<=n;i++) if(!vis[i] && sz(ans)<k) ans.pb(i);
for(int x:ans) cout << x << " ";
cout << endl;
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
11352 KB |
Output is correct |
2 |
Correct |
16 ms |
11356 KB |
Output is correct |
3 |
Correct |
21 ms |
11408 KB |
Output is correct |
4 |
Correct |
16 ms |
11356 KB |
Output is correct |
5 |
Correct |
16 ms |
11356 KB |
Output is correct |
6 |
Correct |
16 ms |
11352 KB |
Output is correct |
7 |
Correct |
16 ms |
11356 KB |
Output is correct |
8 |
Correct |
16 ms |
11356 KB |
Output is correct |
9 |
Correct |
16 ms |
11356 KB |
Output is correct |
10 |
Correct |
16 ms |
11356 KB |
Output is correct |
11 |
Correct |
16 ms |
11356 KB |
Output is correct |
12 |
Correct |
19 ms |
11608 KB |
Output is correct |
13 |
Correct |
17 ms |
11420 KB |
Output is correct |
14 |
Correct |
15 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
300 ms |
34876 KB |
Output is correct |
2 |
Correct |
350 ms |
38188 KB |
Output is correct |
3 |
Correct |
278 ms |
23484 KB |
Output is correct |
4 |
Correct |
900 ms |
25296 KB |
Output is correct |
5 |
Correct |
995 ms |
24724 KB |
Output is correct |
6 |
Correct |
926 ms |
24736 KB |
Output is correct |
7 |
Correct |
952 ms |
23536 KB |
Output is correct |
8 |
Correct |
975 ms |
24232 KB |
Output is correct |
9 |
Correct |
918 ms |
24600 KB |
Output is correct |
10 |
Correct |
1152 ms |
24916 KB |
Output is correct |
11 |
Correct |
733 ms |
26508 KB |
Output is correct |
12 |
Correct |
664 ms |
26452 KB |
Output is correct |
13 |
Correct |
838 ms |
27736 KB |
Output is correct |
14 |
Correct |
652 ms |
24996 KB |
Output is correct |
15 |
Correct |
703 ms |
24400 KB |
Output is correct |
16 |
Correct |
684 ms |
25940 KB |
Output is correct |
17 |
Correct |
724 ms |
25492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
351 ms |
35944 KB |
Output is correct |
2 |
Correct |
313 ms |
37716 KB |
Output is correct |
3 |
Correct |
287 ms |
36432 KB |
Output is correct |
4 |
Correct |
318 ms |
36288 KB |
Output is correct |
5 |
Correct |
333 ms |
40060 KB |
Output is correct |
6 |
Correct |
368 ms |
39120 KB |
Output is correct |
7 |
Correct |
338 ms |
39940 KB |
Output is correct |
8 |
Correct |
340 ms |
38400 KB |
Output is correct |
9 |
Correct |
345 ms |
38192 KB |
Output is correct |
10 |
Correct |
356 ms |
38012 KB |
Output is correct |
11 |
Correct |
316 ms |
36804 KB |
Output is correct |
12 |
Correct |
344 ms |
40652 KB |
Output is correct |
13 |
Correct |
360 ms |
40652 KB |
Output is correct |
14 |
Correct |
373 ms |
39228 KB |
Output is correct |
15 |
Correct |
337 ms |
36924 KB |
Output is correct |
16 |
Correct |
294 ms |
36320 KB |
Output is correct |
17 |
Correct |
294 ms |
35920 KB |
Output is correct |
18 |
Correct |
346 ms |
37568 KB |
Output is correct |
19 |
Correct |
313 ms |
38584 KB |
Output is correct |
20 |
Correct |
320 ms |
39200 KB |
Output is correct |
21 |
Correct |
382 ms |
38092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
11352 KB |
Output is correct |
2 |
Correct |
16 ms |
11356 KB |
Output is correct |
3 |
Correct |
21 ms |
11408 KB |
Output is correct |
4 |
Correct |
16 ms |
11356 KB |
Output is correct |
5 |
Correct |
16 ms |
11356 KB |
Output is correct |
6 |
Correct |
16 ms |
11352 KB |
Output is correct |
7 |
Correct |
16 ms |
11356 KB |
Output is correct |
8 |
Correct |
16 ms |
11356 KB |
Output is correct |
9 |
Correct |
16 ms |
11356 KB |
Output is correct |
10 |
Correct |
16 ms |
11356 KB |
Output is correct |
11 |
Correct |
16 ms |
11356 KB |
Output is correct |
12 |
Correct |
19 ms |
11608 KB |
Output is correct |
13 |
Correct |
17 ms |
11420 KB |
Output is correct |
14 |
Correct |
15 ms |
11356 KB |
Output is correct |
15 |
Correct |
300 ms |
34876 KB |
Output is correct |
16 |
Correct |
350 ms |
38188 KB |
Output is correct |
17 |
Correct |
278 ms |
23484 KB |
Output is correct |
18 |
Correct |
900 ms |
25296 KB |
Output is correct |
19 |
Correct |
995 ms |
24724 KB |
Output is correct |
20 |
Correct |
926 ms |
24736 KB |
Output is correct |
21 |
Correct |
952 ms |
23536 KB |
Output is correct |
22 |
Correct |
975 ms |
24232 KB |
Output is correct |
23 |
Correct |
918 ms |
24600 KB |
Output is correct |
24 |
Correct |
1152 ms |
24916 KB |
Output is correct |
25 |
Correct |
733 ms |
26508 KB |
Output is correct |
26 |
Correct |
664 ms |
26452 KB |
Output is correct |
27 |
Correct |
838 ms |
27736 KB |
Output is correct |
28 |
Correct |
652 ms |
24996 KB |
Output is correct |
29 |
Correct |
703 ms |
24400 KB |
Output is correct |
30 |
Correct |
684 ms |
25940 KB |
Output is correct |
31 |
Correct |
724 ms |
25492 KB |
Output is correct |
32 |
Correct |
351 ms |
35944 KB |
Output is correct |
33 |
Correct |
313 ms |
37716 KB |
Output is correct |
34 |
Correct |
287 ms |
36432 KB |
Output is correct |
35 |
Correct |
318 ms |
36288 KB |
Output is correct |
36 |
Correct |
333 ms |
40060 KB |
Output is correct |
37 |
Correct |
368 ms |
39120 KB |
Output is correct |
38 |
Correct |
338 ms |
39940 KB |
Output is correct |
39 |
Correct |
340 ms |
38400 KB |
Output is correct |
40 |
Correct |
345 ms |
38192 KB |
Output is correct |
41 |
Correct |
356 ms |
38012 KB |
Output is correct |
42 |
Correct |
316 ms |
36804 KB |
Output is correct |
43 |
Correct |
344 ms |
40652 KB |
Output is correct |
44 |
Correct |
360 ms |
40652 KB |
Output is correct |
45 |
Correct |
373 ms |
39228 KB |
Output is correct |
46 |
Correct |
337 ms |
36924 KB |
Output is correct |
47 |
Correct |
294 ms |
36320 KB |
Output is correct |
48 |
Correct |
294 ms |
35920 KB |
Output is correct |
49 |
Correct |
346 ms |
37568 KB |
Output is correct |
50 |
Correct |
313 ms |
38584 KB |
Output is correct |
51 |
Correct |
320 ms |
39200 KB |
Output is correct |
52 |
Correct |
382 ms |
38092 KB |
Output is correct |
53 |
Correct |
969 ms |
25060 KB |
Output is correct |
54 |
Correct |
1022 ms |
25464 KB |
Output is correct |
55 |
Correct |
1036 ms |
26128 KB |
Output is correct |
56 |
Correct |
990 ms |
25940 KB |
Output is correct |
57 |
Correct |
1134 ms |
26752 KB |
Output is correct |
58 |
Correct |
977 ms |
25180 KB |
Output is correct |
59 |
Correct |
1112 ms |
28400 KB |
Output is correct |
60 |
Correct |
1030 ms |
24808 KB |
Output is correct |
61 |
Correct |
861 ms |
23448 KB |
Output is correct |
62 |
Correct |
876 ms |
24764 KB |
Output is correct |
63 |
Correct |
1350 ms |
24668 KB |
Output is correct |
64 |
Correct |
1231 ms |
26488 KB |
Output is correct |
65 |
Correct |
1264 ms |
24988 KB |
Output is correct |
66 |
Correct |
1276 ms |
25744 KB |
Output is correct |
67 |
Correct |
1118 ms |
24196 KB |
Output is correct |
68 |
Correct |
1350 ms |
28076 KB |
Output is correct |
69 |
Correct |
375 ms |
39508 KB |
Output is correct |
70 |
Correct |
311 ms |
37616 KB |
Output is correct |
71 |
Correct |
416 ms |
41292 KB |
Output is correct |
72 |
Correct |
327 ms |
25016 KB |
Output is correct |
73 |
Correct |
347 ms |
23488 KB |
Output is correct |
74 |
Correct |
395 ms |
26548 KB |
Output is correct |
75 |
Correct |
808 ms |
26860 KB |
Output is correct |
76 |
Correct |
926 ms |
26204 KB |
Output is correct |
77 |
Correct |
791 ms |
25936 KB |
Output is correct |
78 |
Correct |
868 ms |
26176 KB |
Output is correct |