#include <bits/stdc++.h>
using namespace std;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
#define int long long
const int inf = 1e17;
int n, k, lim;
vector<vector<pair<int, int>>> adj;
vector<int> lst;
pair<int, int> dfs(int x, int p = -1, int wpar = 0){
int maxw = 0, minb = inf;
for (auto [y, w]: adj[x]){
if (y == p) continue;
auto sbt = dfs(y, x, w);
maxw = max(maxw, sbt.first);
minb = min(minb, sbt.second);
}
if (maxw + minb <= lim) return {0, minb + wpar};
if (p != -1){
if (maxw + wpar > lim){
lst.push_back(x);
return {0, wpar};
}
}
else{
lst.push_back(x);
return {0, wpar};
}
return {maxw + wpar, minb + wpar};
}
bool check(int threshold){
lst.clear();
lim = threshold;
dfs(0);
return (int)lst.size() <= k;
}
void main_program(){
cin >> n >> k;
adj.resize(n);
for (int i = 0; i < n-1; i++){
int a, b, w; cin >> a >> b >> w;
a--; b--;
adj[a].emplace_back(b, w);
adj[b].emplace_back(a, w);
}
int l = 0, r = 1e15;
while (l < r){
int mid = (l + r) >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
check(l);
cout << l << "\n";
vector<int> mark(n);
for (auto i: lst){
mark[i] = 1; k--;
}
for (int i = 0; i < n; i++){
if (!mark[i] && k){
mark[i] = 1; k--;
}
}
for (int i = 0; i < n; i++){
if (mark[i]) cout << i+1 << " ";
}
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
232 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
30260 KB |
Output is correct |
2 |
Correct |
296 ms |
31148 KB |
Output is correct |
3 |
Correct |
238 ms |
16820 KB |
Output is correct |
4 |
Correct |
1026 ms |
16516 KB |
Output is correct |
5 |
Correct |
889 ms |
16200 KB |
Output is correct |
6 |
Correct |
963 ms |
16068 KB |
Output is correct |
7 |
Correct |
996 ms |
16212 KB |
Output is correct |
8 |
Correct |
950 ms |
16836 KB |
Output is correct |
9 |
Correct |
1056 ms |
16616 KB |
Output is correct |
10 |
Correct |
1112 ms |
16924 KB |
Output is correct |
11 |
Correct |
690 ms |
17868 KB |
Output is correct |
12 |
Correct |
631 ms |
17796 KB |
Output is correct |
13 |
Correct |
686 ms |
19368 KB |
Output is correct |
14 |
Correct |
623 ms |
17560 KB |
Output is correct |
15 |
Correct |
602 ms |
17160 KB |
Output is correct |
16 |
Correct |
625 ms |
17912 KB |
Output is correct |
17 |
Correct |
664 ms |
17200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
315 ms |
31352 KB |
Output is correct |
2 |
Correct |
300 ms |
30584 KB |
Output is correct |
3 |
Correct |
294 ms |
29124 KB |
Output is correct |
4 |
Correct |
271 ms |
29060 KB |
Output is correct |
5 |
Correct |
330 ms |
32812 KB |
Output is correct |
6 |
Correct |
329 ms |
32008 KB |
Output is correct |
7 |
Correct |
347 ms |
33420 KB |
Output is correct |
8 |
Correct |
303 ms |
31788 KB |
Output is correct |
9 |
Correct |
287 ms |
31376 KB |
Output is correct |
10 |
Correct |
276 ms |
30772 KB |
Output is correct |
11 |
Correct |
270 ms |
29728 KB |
Output is correct |
12 |
Correct |
331 ms |
33984 KB |
Output is correct |
13 |
Correct |
346 ms |
33736 KB |
Output is correct |
14 |
Correct |
331 ms |
32848 KB |
Output is correct |
15 |
Correct |
286 ms |
30636 KB |
Output is correct |
16 |
Correct |
271 ms |
29460 KB |
Output is correct |
17 |
Correct |
294 ms |
29436 KB |
Output is correct |
18 |
Correct |
295 ms |
30860 KB |
Output is correct |
19 |
Correct |
289 ms |
32060 KB |
Output is correct |
20 |
Correct |
320 ms |
32772 KB |
Output is correct |
21 |
Correct |
297 ms |
31944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
232 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
278 ms |
30260 KB |
Output is correct |
16 |
Correct |
296 ms |
31148 KB |
Output is correct |
17 |
Correct |
238 ms |
16820 KB |
Output is correct |
18 |
Correct |
1026 ms |
16516 KB |
Output is correct |
19 |
Correct |
889 ms |
16200 KB |
Output is correct |
20 |
Correct |
963 ms |
16068 KB |
Output is correct |
21 |
Correct |
996 ms |
16212 KB |
Output is correct |
22 |
Correct |
950 ms |
16836 KB |
Output is correct |
23 |
Correct |
1056 ms |
16616 KB |
Output is correct |
24 |
Correct |
1112 ms |
16924 KB |
Output is correct |
25 |
Correct |
690 ms |
17868 KB |
Output is correct |
26 |
Correct |
631 ms |
17796 KB |
Output is correct |
27 |
Correct |
686 ms |
19368 KB |
Output is correct |
28 |
Correct |
623 ms |
17560 KB |
Output is correct |
29 |
Correct |
602 ms |
17160 KB |
Output is correct |
30 |
Correct |
625 ms |
17912 KB |
Output is correct |
31 |
Correct |
664 ms |
17200 KB |
Output is correct |
32 |
Correct |
315 ms |
31352 KB |
Output is correct |
33 |
Correct |
300 ms |
30584 KB |
Output is correct |
34 |
Correct |
294 ms |
29124 KB |
Output is correct |
35 |
Correct |
271 ms |
29060 KB |
Output is correct |
36 |
Correct |
330 ms |
32812 KB |
Output is correct |
37 |
Correct |
329 ms |
32008 KB |
Output is correct |
38 |
Correct |
347 ms |
33420 KB |
Output is correct |
39 |
Correct |
303 ms |
31788 KB |
Output is correct |
40 |
Correct |
287 ms |
31376 KB |
Output is correct |
41 |
Correct |
276 ms |
30772 KB |
Output is correct |
42 |
Correct |
270 ms |
29728 KB |
Output is correct |
43 |
Correct |
331 ms |
33984 KB |
Output is correct |
44 |
Correct |
346 ms |
33736 KB |
Output is correct |
45 |
Correct |
331 ms |
32848 KB |
Output is correct |
46 |
Correct |
286 ms |
30636 KB |
Output is correct |
47 |
Correct |
271 ms |
29460 KB |
Output is correct |
48 |
Correct |
294 ms |
29436 KB |
Output is correct |
49 |
Correct |
295 ms |
30860 KB |
Output is correct |
50 |
Correct |
289 ms |
32060 KB |
Output is correct |
51 |
Correct |
320 ms |
32772 KB |
Output is correct |
52 |
Correct |
297 ms |
31944 KB |
Output is correct |
53 |
Correct |
1167 ms |
19736 KB |
Output is correct |
54 |
Correct |
1133 ms |
20284 KB |
Output is correct |
55 |
Correct |
1194 ms |
21216 KB |
Output is correct |
56 |
Correct |
1148 ms |
20960 KB |
Output is correct |
57 |
Correct |
1032 ms |
21972 KB |
Output is correct |
58 |
Correct |
1045 ms |
19908 KB |
Output is correct |
59 |
Correct |
1358 ms |
23560 KB |
Output is correct |
60 |
Correct |
1106 ms |
19660 KB |
Output is correct |
61 |
Correct |
867 ms |
18012 KB |
Output is correct |
62 |
Correct |
1002 ms |
19524 KB |
Output is correct |
63 |
Correct |
1032 ms |
19804 KB |
Output is correct |
64 |
Correct |
1035 ms |
21332 KB |
Output is correct |
65 |
Correct |
1025 ms |
19884 KB |
Output is correct |
66 |
Correct |
1006 ms |
20568 KB |
Output is correct |
67 |
Correct |
893 ms |
18700 KB |
Output is correct |
68 |
Correct |
1073 ms |
23144 KB |
Output is correct |
69 |
Correct |
296 ms |
34564 KB |
Output is correct |
70 |
Correct |
268 ms |
32248 KB |
Output is correct |
71 |
Correct |
347 ms |
36376 KB |
Output is correct |
72 |
Correct |
214 ms |
19344 KB |
Output is correct |
73 |
Correct |
216 ms |
19636 KB |
Output is correct |
74 |
Correct |
280 ms |
21428 KB |
Output is correct |
75 |
Correct |
645 ms |
21556 KB |
Output is correct |
76 |
Correct |
688 ms |
21320 KB |
Output is correct |
77 |
Correct |
638 ms |
20952 KB |
Output is correct |
78 |
Correct |
658 ms |
21296 KB |
Output is correct |