#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];
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);
}
}
vector<int> calc(long long r) {
fill(mark + 1, mark + 1 + n, false);
vector<int> res;
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.push_back(u);
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).size();
if (cnt <= k) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << ans << '\n';
vector<int> tmp = calc(ans);
fill(mark + 1, mark + 1 + n, false);
for (auto u : tmp) {
mark[u] = true;
}
for (int i = 1; i <= n; ++i) {
if (!mark[i] && (int)tmp.size() < k) {
tmp.push_back(i);
}
}
sort(tmp.begin(), tmp.end());
for (int x : tmp) {
cout << x << ' ';
}
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 |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 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 |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
29304 KB |
Output is correct |
2 |
Correct |
252 ms |
30028 KB |
Output is correct |
3 |
Correct |
267 ms |
16936 KB |
Output is correct |
4 |
Correct |
2664 ms |
15836 KB |
Output is correct |
5 |
Correct |
2670 ms |
15488 KB |
Output is correct |
6 |
Correct |
1188 ms |
15432 KB |
Output is correct |
7 |
Correct |
1249 ms |
15692 KB |
Output is correct |
8 |
Correct |
1243 ms |
16128 KB |
Output is correct |
9 |
Correct |
1243 ms |
15820 KB |
Output is correct |
10 |
Correct |
1326 ms |
16144 KB |
Output is correct |
11 |
Correct |
902 ms |
16972 KB |
Output is correct |
12 |
Correct |
782 ms |
17080 KB |
Output is correct |
13 |
Correct |
859 ms |
18228 KB |
Output is correct |
14 |
Correct |
703 ms |
16844 KB |
Output is correct |
15 |
Correct |
678 ms |
19276 KB |
Output is correct |
16 |
Correct |
776 ms |
20636 KB |
Output is correct |
17 |
Correct |
875 ms |
20028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
30532 KB |
Output is correct |
2 |
Correct |
194 ms |
29788 KB |
Output is correct |
3 |
Correct |
204 ms |
28488 KB |
Output is correct |
4 |
Correct |
212 ms |
28412 KB |
Output is correct |
5 |
Correct |
234 ms |
32240 KB |
Output is correct |
6 |
Correct |
248 ms |
31156 KB |
Output is correct |
7 |
Correct |
241 ms |
32124 KB |
Output is correct |
8 |
Correct |
197 ms |
30668 KB |
Output is correct |
9 |
Correct |
200 ms |
30496 KB |
Output is correct |
10 |
Correct |
205 ms |
29884 KB |
Output is correct |
11 |
Correct |
218 ms |
28944 KB |
Output is correct |
12 |
Correct |
258 ms |
33236 KB |
Output is correct |
13 |
Correct |
259 ms |
33056 KB |
Output is correct |
14 |
Correct |
248 ms |
31684 KB |
Output is correct |
15 |
Correct |
217 ms |
29832 KB |
Output is correct |
16 |
Correct |
204 ms |
28820 KB |
Output is correct |
17 |
Correct |
214 ms |
28816 KB |
Output is correct |
18 |
Correct |
231 ms |
30192 KB |
Output is correct |
19 |
Correct |
240 ms |
31568 KB |
Output is correct |
20 |
Correct |
231 ms |
31980 KB |
Output is correct |
21 |
Correct |
232 ms |
30792 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 |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 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 |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
228 ms |
29304 KB |
Output is correct |
16 |
Correct |
252 ms |
30028 KB |
Output is correct |
17 |
Correct |
267 ms |
16936 KB |
Output is correct |
18 |
Correct |
2664 ms |
15836 KB |
Output is correct |
19 |
Correct |
2670 ms |
15488 KB |
Output is correct |
20 |
Correct |
1188 ms |
15432 KB |
Output is correct |
21 |
Correct |
1249 ms |
15692 KB |
Output is correct |
22 |
Correct |
1243 ms |
16128 KB |
Output is correct |
23 |
Correct |
1243 ms |
15820 KB |
Output is correct |
24 |
Correct |
1326 ms |
16144 KB |
Output is correct |
25 |
Correct |
902 ms |
16972 KB |
Output is correct |
26 |
Correct |
782 ms |
17080 KB |
Output is correct |
27 |
Correct |
859 ms |
18228 KB |
Output is correct |
28 |
Correct |
703 ms |
16844 KB |
Output is correct |
29 |
Correct |
678 ms |
19276 KB |
Output is correct |
30 |
Correct |
776 ms |
20636 KB |
Output is correct |
31 |
Correct |
875 ms |
20028 KB |
Output is correct |
32 |
Correct |
205 ms |
30532 KB |
Output is correct |
33 |
Correct |
194 ms |
29788 KB |
Output is correct |
34 |
Correct |
204 ms |
28488 KB |
Output is correct |
35 |
Correct |
212 ms |
28412 KB |
Output is correct |
36 |
Correct |
234 ms |
32240 KB |
Output is correct |
37 |
Correct |
248 ms |
31156 KB |
Output is correct |
38 |
Correct |
241 ms |
32124 KB |
Output is correct |
39 |
Correct |
197 ms |
30668 KB |
Output is correct |
40 |
Correct |
200 ms |
30496 KB |
Output is correct |
41 |
Correct |
205 ms |
29884 KB |
Output is correct |
42 |
Correct |
218 ms |
28944 KB |
Output is correct |
43 |
Correct |
258 ms |
33236 KB |
Output is correct |
44 |
Correct |
259 ms |
33056 KB |
Output is correct |
45 |
Correct |
248 ms |
31684 KB |
Output is correct |
46 |
Correct |
217 ms |
29832 KB |
Output is correct |
47 |
Correct |
204 ms |
28820 KB |
Output is correct |
48 |
Correct |
214 ms |
28816 KB |
Output is correct |
49 |
Correct |
231 ms |
30192 KB |
Output is correct |
50 |
Correct |
240 ms |
31568 KB |
Output is correct |
51 |
Correct |
231 ms |
31980 KB |
Output is correct |
52 |
Correct |
232 ms |
30792 KB |
Output is correct |
53 |
Execution timed out |
3065 ms |
19888 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |