#include <bits/stdc++.h>
#define Alora "cownav"
#define fi(i,a,b) for(int i = a; i <= b; i++)
#define fid(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define f first
#define se second
#define pii pair<int, int>
#define getbit(i, j) ((i >> j) & 1)
#define all(v) v.begin(), v.end()
#define pb push_back
#define maxn 200005
const int M = 1e9 + 7;
using namespace std;
int n, k, place[maxn], ans;
ll reach[maxn], dist[maxn], sum, oo = 1e18;
vector <pii> g[maxn];
void dfs(int u, int fa, ll lim){
for(auto [v, w]: g[u]) if(v != fa){
dfs(v, u, lim);
if(reach[v] + min(1ll*w, dist[v]) <= lim){
if(reach[v] + dist[v] > lim)
reach[u] = max(reach[u], reach[v] + w);
dist[u] = min(dist[u], dist[v] + w);
}
else{
place[v] = 1; ans++;
dist[u] = min(dist[u], 1ll*w);
}
}
}
int cnt(ll lim){
fi(i, 1, n) reach[i] = 0, dist[i] = oo, place[i] = 0;
ans = 0;
dfs(1, 1, lim);
if(reach[1] + dist[1] > lim) ans++, place[1] = 1;
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
if(fopen(Alora".inp", "r")){
freopen(Alora".inp", "r", stdin);
freopen(Alora".out", "w", stdout);}
cin >> n >> k;
fi(i, 1, n - 1){
int u, v, w; cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
sum += w;
}
ll d = 0, c = 1e18;
while(d <= c){
ll g = (d + c)/2;
if(cnt(g) <= k) c = g - 1;
else d = g + 1;
}
cout << d << '\n';
int x = cnt(d);
fi(i, 1, n){
if(place[i]) cout << i << " ";
else if(x < k) x++, cout << i << " ";
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen(Alora".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:44:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen(Alora".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8848 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
336 ms |
32484 KB |
Output is correct |
2 |
Correct |
407 ms |
33048 KB |
Output is correct |
3 |
Correct |
245 ms |
21180 KB |
Output is correct |
4 |
Correct |
574 ms |
20048 KB |
Output is correct |
5 |
Correct |
624 ms |
19788 KB |
Output is correct |
6 |
Correct |
582 ms |
19764 KB |
Output is correct |
7 |
Correct |
629 ms |
18768 KB |
Output is correct |
8 |
Correct |
742 ms |
19280 KB |
Output is correct |
9 |
Correct |
562 ms |
19556 KB |
Output is correct |
10 |
Correct |
725 ms |
20080 KB |
Output is correct |
11 |
Correct |
504 ms |
21844 KB |
Output is correct |
12 |
Correct |
493 ms |
21744 KB |
Output is correct |
13 |
Correct |
588 ms |
22804 KB |
Output is correct |
14 |
Correct |
528 ms |
20696 KB |
Output is correct |
15 |
Correct |
478 ms |
19792 KB |
Output is correct |
16 |
Correct |
500 ms |
21268 KB |
Output is correct |
17 |
Correct |
472 ms |
20372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
429 ms |
33364 KB |
Output is correct |
2 |
Correct |
370 ms |
37124 KB |
Output is correct |
3 |
Correct |
318 ms |
35412 KB |
Output is correct |
4 |
Correct |
330 ms |
35576 KB |
Output is correct |
5 |
Correct |
354 ms |
37616 KB |
Output is correct |
6 |
Correct |
401 ms |
37336 KB |
Output is correct |
7 |
Correct |
371 ms |
38364 KB |
Output is correct |
8 |
Correct |
354 ms |
37464 KB |
Output is correct |
9 |
Correct |
391 ms |
37204 KB |
Output is correct |
10 |
Correct |
347 ms |
36692 KB |
Output is correct |
11 |
Correct |
348 ms |
35408 KB |
Output is correct |
12 |
Correct |
388 ms |
38184 KB |
Output is correct |
13 |
Correct |
383 ms |
38400 KB |
Output is correct |
14 |
Correct |
375 ms |
37600 KB |
Output is correct |
15 |
Correct |
363 ms |
35668 KB |
Output is correct |
16 |
Correct |
325 ms |
34612 KB |
Output is correct |
17 |
Correct |
338 ms |
34428 KB |
Output is correct |
18 |
Correct |
349 ms |
36096 KB |
Output is correct |
19 |
Correct |
418 ms |
35516 KB |
Output is correct |
20 |
Correct |
351 ms |
36268 KB |
Output is correct |
21 |
Correct |
356 ms |
35668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8848 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
336 ms |
32484 KB |
Output is correct |
16 |
Correct |
407 ms |
33048 KB |
Output is correct |
17 |
Correct |
245 ms |
21180 KB |
Output is correct |
18 |
Correct |
574 ms |
20048 KB |
Output is correct |
19 |
Correct |
624 ms |
19788 KB |
Output is correct |
20 |
Correct |
582 ms |
19764 KB |
Output is correct |
21 |
Correct |
629 ms |
18768 KB |
Output is correct |
22 |
Correct |
742 ms |
19280 KB |
Output is correct |
23 |
Correct |
562 ms |
19556 KB |
Output is correct |
24 |
Correct |
725 ms |
20080 KB |
Output is correct |
25 |
Correct |
504 ms |
21844 KB |
Output is correct |
26 |
Correct |
493 ms |
21744 KB |
Output is correct |
27 |
Correct |
588 ms |
22804 KB |
Output is correct |
28 |
Correct |
528 ms |
20696 KB |
Output is correct |
29 |
Correct |
478 ms |
19792 KB |
Output is correct |
30 |
Correct |
500 ms |
21268 KB |
Output is correct |
31 |
Correct |
472 ms |
20372 KB |
Output is correct |
32 |
Correct |
429 ms |
33364 KB |
Output is correct |
33 |
Correct |
370 ms |
37124 KB |
Output is correct |
34 |
Correct |
318 ms |
35412 KB |
Output is correct |
35 |
Correct |
330 ms |
35576 KB |
Output is correct |
36 |
Correct |
354 ms |
37616 KB |
Output is correct |
37 |
Correct |
401 ms |
37336 KB |
Output is correct |
38 |
Correct |
371 ms |
38364 KB |
Output is correct |
39 |
Correct |
354 ms |
37464 KB |
Output is correct |
40 |
Correct |
391 ms |
37204 KB |
Output is correct |
41 |
Correct |
347 ms |
36692 KB |
Output is correct |
42 |
Correct |
348 ms |
35408 KB |
Output is correct |
43 |
Correct |
388 ms |
38184 KB |
Output is correct |
44 |
Correct |
383 ms |
38400 KB |
Output is correct |
45 |
Correct |
375 ms |
37600 KB |
Output is correct |
46 |
Correct |
363 ms |
35668 KB |
Output is correct |
47 |
Correct |
325 ms |
34612 KB |
Output is correct |
48 |
Correct |
338 ms |
34428 KB |
Output is correct |
49 |
Correct |
349 ms |
36096 KB |
Output is correct |
50 |
Correct |
418 ms |
35516 KB |
Output is correct |
51 |
Correct |
351 ms |
36268 KB |
Output is correct |
52 |
Correct |
356 ms |
35668 KB |
Output is correct |
53 |
Correct |
733 ms |
20252 KB |
Output is correct |
54 |
Correct |
1229 ms |
20248 KB |
Output is correct |
55 |
Correct |
1569 ms |
20948 KB |
Output is correct |
56 |
Correct |
1370 ms |
20564 KB |
Output is correct |
57 |
Correct |
971 ms |
20796 KB |
Output is correct |
58 |
Correct |
866 ms |
20184 KB |
Output is correct |
59 |
Correct |
943 ms |
21676 KB |
Output is correct |
60 |
Correct |
986 ms |
19612 KB |
Output is correct |
61 |
Correct |
780 ms |
18516 KB |
Output is correct |
62 |
Correct |
759 ms |
18940 KB |
Output is correct |
63 |
Correct |
1060 ms |
19536 KB |
Output is correct |
64 |
Correct |
866 ms |
20056 KB |
Output is correct |
65 |
Correct |
922 ms |
19932 KB |
Output is correct |
66 |
Correct |
869 ms |
19992 KB |
Output is correct |
67 |
Correct |
712 ms |
19024 KB |
Output is correct |
68 |
Correct |
960 ms |
21168 KB |
Output is correct |
69 |
Correct |
349 ms |
37312 KB |
Output is correct |
70 |
Correct |
346 ms |
35232 KB |
Output is correct |
71 |
Correct |
458 ms |
38312 KB |
Output is correct |
72 |
Correct |
226 ms |
20384 KB |
Output is correct |
73 |
Correct |
241 ms |
19648 KB |
Output is correct |
74 |
Correct |
301 ms |
20416 KB |
Output is correct |
75 |
Correct |
647 ms |
22332 KB |
Output is correct |
76 |
Correct |
665 ms |
21452 KB |
Output is correct |
77 |
Correct |
608 ms |
21584 KB |
Output is correct |
78 |
Correct |
633 ms |
20888 KB |
Output is correct |