#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
const int N = 200005;
int n, k;
vector<pii> adj[N];
bool vis[N], park[N];
ll dfs(int v, int p, ll mid){
if(v!=0 && (int)adj[v].size()==1)
return 0;
ll mn = 1e16, mx = -1e16;
for(auto [u, w] : adj[v]){
if(u==p) continue;
ll val = dfs(u, v, mid);
if(val==0ll && vis[u]){
mn = min(mn, 0ll);
mx = max(mx, 0ll);
continue;
}
if(val+w<=0ll) vis[v] = 1, val += w;
else if(val<0ll) val = 0;
else val += w;
if(val>mid){
park[u] = 1;
val = min(-mid+w, 0ll);
if(-mid+w<=0ll) vis[v] = 1;
vis[u] = 1;
}
mn = min(mn, val);
mx = max(mx, val);
}
if(-mn>=mx) return mn;
return mx;
}
int check(ll mid){
memset(vis, 0, sizeof vis);
memset(park, 0, sizeof park);
if(dfs(0, 0, mid)>0) park[0] = 1;
else if(!vis[0]) park[0] = 1;
int cnt = 0;
for(int i = 0; i<n; ++i)
cnt += park[i];
return cnt;
}
int main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for(int i = 0; i<n-1; ++i){
int a, b, w;
cin >> a >> b >> w;
--a; --b;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
ll lo = 0, hi = 1e16;
while(lo<hi){
ll mid = lo+(hi-lo)/2;
if(check(mid)<=k) hi = mid;
else lo = mid+1;
}
cout << lo << '\n';
check(lo);
vector<int> res;
for(int i = 0; i<n; ++i)
if(park[i]) res.push_back(i);
for(int i = 0; i<n; ++i)
if(!park[i] && (int)res.size()<k)
res.push_back(i);
for(auto x : res)
cout << x+1 << ' ';
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
4 ms |
5308 KB |
Output is correct |
3 |
Correct |
3 ms |
5316 KB |
Output is correct |
4 |
Correct |
3 ms |
5324 KB |
Output is correct |
5 |
Correct |
3 ms |
5324 KB |
Output is correct |
6 |
Correct |
3 ms |
5396 KB |
Output is correct |
7 |
Correct |
3 ms |
5324 KB |
Output is correct |
8 |
Correct |
3 ms |
5400 KB |
Output is correct |
9 |
Correct |
3 ms |
5324 KB |
Output is correct |
10 |
Correct |
3 ms |
5324 KB |
Output is correct |
11 |
Correct |
3 ms |
5324 KB |
Output is correct |
12 |
Correct |
3 ms |
5400 KB |
Output is correct |
13 |
Correct |
4 ms |
5400 KB |
Output is correct |
14 |
Correct |
4 ms |
5308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
353 ms |
31476 KB |
Output is correct |
2 |
Correct |
366 ms |
31604 KB |
Output is correct |
3 |
Correct |
200 ms |
17332 KB |
Output is correct |
4 |
Correct |
829 ms |
16324 KB |
Output is correct |
5 |
Correct |
794 ms |
15960 KB |
Output is correct |
6 |
Correct |
811 ms |
15980 KB |
Output is correct |
7 |
Correct |
748 ms |
14792 KB |
Output is correct |
8 |
Correct |
853 ms |
13912 KB |
Output is correct |
9 |
Correct |
823 ms |
12396 KB |
Output is correct |
10 |
Correct |
868 ms |
15932 KB |
Output is correct |
11 |
Correct |
656 ms |
17112 KB |
Output is correct |
12 |
Correct |
642 ms |
17580 KB |
Output is correct |
13 |
Correct |
706 ms |
18872 KB |
Output is correct |
14 |
Correct |
670 ms |
16484 KB |
Output is correct |
15 |
Correct |
611 ms |
15940 KB |
Output is correct |
16 |
Correct |
639 ms |
17348 KB |
Output is correct |
17 |
Correct |
674 ms |
16628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
378 ms |
33564 KB |
Output is correct |
2 |
Correct |
420 ms |
33260 KB |
Output is correct |
3 |
Correct |
340 ms |
31708 KB |
Output is correct |
4 |
Correct |
370 ms |
31620 KB |
Output is correct |
5 |
Correct |
389 ms |
34832 KB |
Output is correct |
6 |
Correct |
440 ms |
34096 KB |
Output is correct |
7 |
Correct |
432 ms |
34980 KB |
Output is correct |
8 |
Correct |
416 ms |
33476 KB |
Output is correct |
9 |
Correct |
474 ms |
33316 KB |
Output is correct |
10 |
Correct |
363 ms |
30720 KB |
Output is correct |
11 |
Correct |
372 ms |
28496 KB |
Output is correct |
12 |
Correct |
455 ms |
31704 KB |
Output is correct |
13 |
Correct |
426 ms |
31616 KB |
Output is correct |
14 |
Correct |
450 ms |
30800 KB |
Output is correct |
15 |
Correct |
388 ms |
29324 KB |
Output is correct |
16 |
Correct |
395 ms |
28336 KB |
Output is correct |
17 |
Correct |
327 ms |
28284 KB |
Output is correct |
18 |
Correct |
419 ms |
29636 KB |
Output is correct |
19 |
Correct |
368 ms |
30068 KB |
Output is correct |
20 |
Correct |
369 ms |
30516 KB |
Output is correct |
21 |
Correct |
383 ms |
29800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
4 ms |
5308 KB |
Output is correct |
3 |
Correct |
3 ms |
5316 KB |
Output is correct |
4 |
Correct |
3 ms |
5324 KB |
Output is correct |
5 |
Correct |
3 ms |
5324 KB |
Output is correct |
6 |
Correct |
3 ms |
5396 KB |
Output is correct |
7 |
Correct |
3 ms |
5324 KB |
Output is correct |
8 |
Correct |
3 ms |
5400 KB |
Output is correct |
9 |
Correct |
3 ms |
5324 KB |
Output is correct |
10 |
Correct |
3 ms |
5324 KB |
Output is correct |
11 |
Correct |
3 ms |
5324 KB |
Output is correct |
12 |
Correct |
3 ms |
5400 KB |
Output is correct |
13 |
Correct |
4 ms |
5400 KB |
Output is correct |
14 |
Correct |
4 ms |
5308 KB |
Output is correct |
15 |
Correct |
353 ms |
31476 KB |
Output is correct |
16 |
Correct |
366 ms |
31604 KB |
Output is correct |
17 |
Correct |
200 ms |
17332 KB |
Output is correct |
18 |
Correct |
829 ms |
16324 KB |
Output is correct |
19 |
Correct |
794 ms |
15960 KB |
Output is correct |
20 |
Correct |
811 ms |
15980 KB |
Output is correct |
21 |
Correct |
748 ms |
14792 KB |
Output is correct |
22 |
Correct |
853 ms |
13912 KB |
Output is correct |
23 |
Correct |
823 ms |
12396 KB |
Output is correct |
24 |
Correct |
868 ms |
15932 KB |
Output is correct |
25 |
Correct |
656 ms |
17112 KB |
Output is correct |
26 |
Correct |
642 ms |
17580 KB |
Output is correct |
27 |
Correct |
706 ms |
18872 KB |
Output is correct |
28 |
Correct |
670 ms |
16484 KB |
Output is correct |
29 |
Correct |
611 ms |
15940 KB |
Output is correct |
30 |
Correct |
639 ms |
17348 KB |
Output is correct |
31 |
Correct |
674 ms |
16628 KB |
Output is correct |
32 |
Correct |
378 ms |
33564 KB |
Output is correct |
33 |
Correct |
420 ms |
33260 KB |
Output is correct |
34 |
Correct |
340 ms |
31708 KB |
Output is correct |
35 |
Correct |
370 ms |
31620 KB |
Output is correct |
36 |
Correct |
389 ms |
34832 KB |
Output is correct |
37 |
Correct |
440 ms |
34096 KB |
Output is correct |
38 |
Correct |
432 ms |
34980 KB |
Output is correct |
39 |
Correct |
416 ms |
33476 KB |
Output is correct |
40 |
Correct |
474 ms |
33316 KB |
Output is correct |
41 |
Correct |
363 ms |
30720 KB |
Output is correct |
42 |
Correct |
372 ms |
28496 KB |
Output is correct |
43 |
Correct |
455 ms |
31704 KB |
Output is correct |
44 |
Correct |
426 ms |
31616 KB |
Output is correct |
45 |
Correct |
450 ms |
30800 KB |
Output is correct |
46 |
Correct |
388 ms |
29324 KB |
Output is correct |
47 |
Correct |
395 ms |
28336 KB |
Output is correct |
48 |
Correct |
327 ms |
28284 KB |
Output is correct |
49 |
Correct |
419 ms |
29636 KB |
Output is correct |
50 |
Correct |
368 ms |
30068 KB |
Output is correct |
51 |
Correct |
369 ms |
30516 KB |
Output is correct |
52 |
Correct |
383 ms |
29800 KB |
Output is correct |
53 |
Correct |
858 ms |
12408 KB |
Output is correct |
54 |
Correct |
848 ms |
12468 KB |
Output is correct |
55 |
Correct |
936 ms |
12800 KB |
Output is correct |
56 |
Correct |
860 ms |
12768 KB |
Output is correct |
57 |
Correct |
948 ms |
13592 KB |
Output is correct |
58 |
Correct |
798 ms |
12388 KB |
Output is correct |
59 |
Correct |
971 ms |
14748 KB |
Output is correct |
60 |
Correct |
1014 ms |
12820 KB |
Output is correct |
61 |
Correct |
788 ms |
12160 KB |
Output is correct |
62 |
Correct |
829 ms |
13168 KB |
Output is correct |
63 |
Correct |
945 ms |
12852 KB |
Output is correct |
64 |
Correct |
801 ms |
14416 KB |
Output is correct |
65 |
Correct |
895 ms |
12744 KB |
Output is correct |
66 |
Correct |
931 ms |
13348 KB |
Output is correct |
67 |
Correct |
741 ms |
12200 KB |
Output is correct |
68 |
Correct |
939 ms |
14888 KB |
Output is correct |
69 |
Correct |
379 ms |
29440 KB |
Output is correct |
70 |
Correct |
376 ms |
28200 KB |
Output is correct |
71 |
Correct |
436 ms |
31128 KB |
Output is correct |
72 |
Correct |
176 ms |
12628 KB |
Output is correct |
73 |
Correct |
222 ms |
13004 KB |
Output is correct |
74 |
Correct |
194 ms |
14564 KB |
Output is correct |
75 |
Correct |
598 ms |
14292 KB |
Output is correct |
76 |
Correct |
638 ms |
14616 KB |
Output is correct |
77 |
Correct |
633 ms |
14276 KB |
Output is correct |
78 |
Correct |
675 ms |
14716 KB |
Output is correct |