#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, k;
long long dist[N];
array<int, 2> par[N];
vector<array<int, 2>> adj[N];
vector<int> ord;
bool mark[N], choose[N];
void Dfs1(int u, int p) {
for (auto [v, c] : adj[u]) {
if (v == p) {
continue;
}
dist[v] = dist[u] + c;
par[v] = {u, c};
Dfs1(v, u);
}
}
void Dfs2(int u, int p, long long rem) {
if (rem < 0) {
return;
}
mark[u] = true;
for (auto [v, c] : adj[u]) {
if (v == p) {
continue;
}
Dfs2(v, u, rem - c);
}
}
int calc(long long r) {
fill(mark + 1, mark + 1 + n, false);
fill(choose + 1, choose + 1 + n, false);
int res = 0;
for (auto u : ord) {
if (mark[u]) {
continue;
}
long long rem = r;
while (par[u][0] && rem >= par[u][1]) {
rem -= par[u][1];
u = par[u][0];
}
++res;
choose[u] = true;
Dfs2(u, -1, r);
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i < n; ++i) {
int u, v, c;
cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
Dfs1(1, -1);
ord.resize(n);
iota(ord.begin(), ord.end(), 1);
sort(ord.begin(), ord.end(), [&](int u, int v){
return dist[u] > dist[v];
});
long long l = 0, r = 1e15, ans = -1;
while (l <= r) {
long long mid = (l + r) >> 1;
int cnt = calc(mid);
if (cnt <= k) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << ans << '\n';
int cnt = calc(ans);
for (int i = 1; i <= n; ++i) {
if (!choose[i] && cnt < k) {
++cnt;
choose[i] = true;
}
}
for (int i = 1; i <= n; ++i) {
if (choose[i]) {
cout << i << ' ';
}
}
cout << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'void Dfs1(int, int)':
Main.cpp:14:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
14 | for (auto [v, c] : adj[u]) {
| ^
Main.cpp: In function 'void Dfs2(int, int, long long int)':
Main.cpp:29:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | for (auto [v, c] : adj[u]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
29504 KB |
Output is correct |
2 |
Correct |
239 ms |
30188 KB |
Output is correct |
3 |
Correct |
275 ms |
17028 KB |
Output is correct |
4 |
Correct |
2436 ms |
15948 KB |
Output is correct |
5 |
Correct |
2473 ms |
15692 KB |
Output is correct |
6 |
Correct |
1079 ms |
15680 KB |
Output is correct |
7 |
Correct |
1064 ms |
15796 KB |
Output is correct |
8 |
Correct |
1169 ms |
16384 KB |
Output is correct |
9 |
Correct |
1248 ms |
16092 KB |
Output is correct |
10 |
Correct |
1290 ms |
16216 KB |
Output is correct |
11 |
Correct |
906 ms |
17100 KB |
Output is correct |
12 |
Correct |
781 ms |
17272 KB |
Output is correct |
13 |
Correct |
847 ms |
18380 KB |
Output is correct |
14 |
Correct |
701 ms |
17140 KB |
Output is correct |
15 |
Correct |
669 ms |
16704 KB |
Output is correct |
16 |
Correct |
783 ms |
17296 KB |
Output is correct |
17 |
Correct |
867 ms |
16716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
30688 KB |
Output is correct |
2 |
Correct |
194 ms |
29940 KB |
Output is correct |
3 |
Correct |
196 ms |
28624 KB |
Output is correct |
4 |
Correct |
219 ms |
28564 KB |
Output is correct |
5 |
Correct |
209 ms |
30684 KB |
Output is correct |
6 |
Correct |
253 ms |
30380 KB |
Output is correct |
7 |
Correct |
232 ms |
31312 KB |
Output is correct |
8 |
Correct |
206 ms |
30852 KB |
Output is correct |
9 |
Correct |
198 ms |
30556 KB |
Output is correct |
10 |
Correct |
212 ms |
30032 KB |
Output is correct |
11 |
Correct |
214 ms |
29132 KB |
Output is correct |
12 |
Correct |
238 ms |
31584 KB |
Output is correct |
13 |
Correct |
240 ms |
31780 KB |
Output is correct |
14 |
Correct |
236 ms |
31088 KB |
Output is correct |
15 |
Correct |
213 ms |
30012 KB |
Output is correct |
16 |
Correct |
206 ms |
29072 KB |
Output is correct |
17 |
Correct |
215 ms |
28964 KB |
Output is correct |
18 |
Correct |
245 ms |
30228 KB |
Output is correct |
19 |
Correct |
226 ms |
29900 KB |
Output is correct |
20 |
Correct |
239 ms |
30668 KB |
Output is correct |
21 |
Correct |
233 ms |
30028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
221 ms |
29504 KB |
Output is correct |
16 |
Correct |
239 ms |
30188 KB |
Output is correct |
17 |
Correct |
275 ms |
17028 KB |
Output is correct |
18 |
Correct |
2436 ms |
15948 KB |
Output is correct |
19 |
Correct |
2473 ms |
15692 KB |
Output is correct |
20 |
Correct |
1079 ms |
15680 KB |
Output is correct |
21 |
Correct |
1064 ms |
15796 KB |
Output is correct |
22 |
Correct |
1169 ms |
16384 KB |
Output is correct |
23 |
Correct |
1248 ms |
16092 KB |
Output is correct |
24 |
Correct |
1290 ms |
16216 KB |
Output is correct |
25 |
Correct |
906 ms |
17100 KB |
Output is correct |
26 |
Correct |
781 ms |
17272 KB |
Output is correct |
27 |
Correct |
847 ms |
18380 KB |
Output is correct |
28 |
Correct |
701 ms |
17140 KB |
Output is correct |
29 |
Correct |
669 ms |
16704 KB |
Output is correct |
30 |
Correct |
783 ms |
17296 KB |
Output is correct |
31 |
Correct |
867 ms |
16716 KB |
Output is correct |
32 |
Correct |
205 ms |
30688 KB |
Output is correct |
33 |
Correct |
194 ms |
29940 KB |
Output is correct |
34 |
Correct |
196 ms |
28624 KB |
Output is correct |
35 |
Correct |
219 ms |
28564 KB |
Output is correct |
36 |
Correct |
209 ms |
30684 KB |
Output is correct |
37 |
Correct |
253 ms |
30380 KB |
Output is correct |
38 |
Correct |
232 ms |
31312 KB |
Output is correct |
39 |
Correct |
206 ms |
30852 KB |
Output is correct |
40 |
Correct |
198 ms |
30556 KB |
Output is correct |
41 |
Correct |
212 ms |
30032 KB |
Output is correct |
42 |
Correct |
214 ms |
29132 KB |
Output is correct |
43 |
Correct |
238 ms |
31584 KB |
Output is correct |
44 |
Correct |
240 ms |
31780 KB |
Output is correct |
45 |
Correct |
236 ms |
31088 KB |
Output is correct |
46 |
Correct |
213 ms |
30012 KB |
Output is correct |
47 |
Correct |
206 ms |
29072 KB |
Output is correct |
48 |
Correct |
215 ms |
28964 KB |
Output is correct |
49 |
Correct |
245 ms |
30228 KB |
Output is correct |
50 |
Correct |
226 ms |
29900 KB |
Output is correct |
51 |
Correct |
239 ms |
30668 KB |
Output is correct |
52 |
Correct |
233 ms |
30028 KB |
Output is correct |
53 |
Execution timed out |
3076 ms |
15964 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |