#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long INF = 1e18;
int n, k;
vector<array<int, 2>> adj[N];
bool choose[N];
array<long long, 2> Dfs(int u, int p, long long r) {
array<long long, 2> res = {-INF, -INF};
vector<long long> a = {0};
for (auto [v, c] : adj[u]) {
if (v == p) {
continue;
}
auto val = Dfs(v, u, r);
res[1] = max(res[1], val[1] - c);
if (val[0] + c > r) {
choose[v] = true;
res[1] = max(res[1], r - c);
} else {
a.push_back(val[0] + c);
}
}
for (auto d : a) {
if (d > res[1]) {
res[0] = max(res[0], d);
}
}
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});
}
long long l = 0, r = 1e15, ans = -1;
while (l <= r) {
long long mid = (l + r) >> 1;
fill(choose + 1, choose + 1 + n, false);
if (Dfs(1, -1, mid)[0] >= 0) {
choose[1] = true;
}
int cnt = accumulate(choose + 1, choose + 1 + n, 0);
if (cnt <= k) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << ans << '\n';
fill(choose + 1, choose + 1 + n, false);
if (Dfs(1, -1, ans)[0] >= 0) {
choose[1] = true;
}
int cnt = accumulate(choose + 1, choose + 1 + n, 0);
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 'std::array<long long int, 2> Dfs(int, int, long long 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]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 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 |
2 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 |
4960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
829 ms |
40408 KB |
Output is correct |
2 |
Correct |
961 ms |
41464 KB |
Output is correct |
3 |
Correct |
567 ms |
16620 KB |
Output is correct |
4 |
Correct |
1750 ms |
12208 KB |
Output is correct |
5 |
Correct |
1651 ms |
11956 KB |
Output is correct |
6 |
Correct |
1642 ms |
11972 KB |
Output is correct |
7 |
Correct |
1640 ms |
12020 KB |
Output is correct |
8 |
Correct |
1755 ms |
12400 KB |
Output is correct |
9 |
Correct |
1723 ms |
12224 KB |
Output is correct |
10 |
Correct |
1878 ms |
12376 KB |
Output is correct |
11 |
Correct |
1147 ms |
14708 KB |
Output is correct |
12 |
Correct |
1047 ms |
15400 KB |
Output is correct |
13 |
Correct |
1244 ms |
16396 KB |
Output is correct |
14 |
Correct |
1100 ms |
15236 KB |
Output is correct |
15 |
Correct |
1056 ms |
14388 KB |
Output is correct |
16 |
Correct |
1096 ms |
15396 KB |
Output is correct |
17 |
Correct |
1039 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
958 ms |
42024 KB |
Output is correct |
2 |
Correct |
958 ms |
40992 KB |
Output is correct |
3 |
Correct |
886 ms |
39244 KB |
Output is correct |
4 |
Correct |
807 ms |
39020 KB |
Output is correct |
5 |
Correct |
749 ms |
41564 KB |
Output is correct |
6 |
Correct |
907 ms |
41276 KB |
Output is correct |
7 |
Correct |
887 ms |
42584 KB |
Output is correct |
8 |
Correct |
982 ms |
42248 KB |
Output is correct |
9 |
Correct |
1059 ms |
41936 KB |
Output is correct |
10 |
Correct |
1008 ms |
41184 KB |
Output is correct |
11 |
Correct |
1033 ms |
39852 KB |
Output is correct |
12 |
Correct |
1050 ms |
42760 KB |
Output is correct |
13 |
Correct |
1143 ms |
43144 KB |
Output is correct |
14 |
Correct |
962 ms |
42272 KB |
Output is correct |
15 |
Correct |
970 ms |
41160 KB |
Output is correct |
16 |
Correct |
922 ms |
39652 KB |
Output is correct |
17 |
Correct |
1002 ms |
39520 KB |
Output is correct |
18 |
Correct |
1027 ms |
41440 KB |
Output is correct |
19 |
Correct |
854 ms |
40604 KB |
Output is correct |
20 |
Correct |
931 ms |
41412 KB |
Output is correct |
21 |
Correct |
955 ms |
40772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 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 |
2 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 |
4960 KB |
Output is correct |
15 |
Correct |
829 ms |
40408 KB |
Output is correct |
16 |
Correct |
961 ms |
41464 KB |
Output is correct |
17 |
Correct |
567 ms |
16620 KB |
Output is correct |
18 |
Correct |
1750 ms |
12208 KB |
Output is correct |
19 |
Correct |
1651 ms |
11956 KB |
Output is correct |
20 |
Correct |
1642 ms |
11972 KB |
Output is correct |
21 |
Correct |
1640 ms |
12020 KB |
Output is correct |
22 |
Correct |
1755 ms |
12400 KB |
Output is correct |
23 |
Correct |
1723 ms |
12224 KB |
Output is correct |
24 |
Correct |
1878 ms |
12376 KB |
Output is correct |
25 |
Correct |
1147 ms |
14708 KB |
Output is correct |
26 |
Correct |
1047 ms |
15400 KB |
Output is correct |
27 |
Correct |
1244 ms |
16396 KB |
Output is correct |
28 |
Correct |
1100 ms |
15236 KB |
Output is correct |
29 |
Correct |
1056 ms |
14388 KB |
Output is correct |
30 |
Correct |
1096 ms |
15396 KB |
Output is correct |
31 |
Correct |
1039 ms |
14412 KB |
Output is correct |
32 |
Correct |
958 ms |
42024 KB |
Output is correct |
33 |
Correct |
958 ms |
40992 KB |
Output is correct |
34 |
Correct |
886 ms |
39244 KB |
Output is correct |
35 |
Correct |
807 ms |
39020 KB |
Output is correct |
36 |
Correct |
749 ms |
41564 KB |
Output is correct |
37 |
Correct |
907 ms |
41276 KB |
Output is correct |
38 |
Correct |
887 ms |
42584 KB |
Output is correct |
39 |
Correct |
982 ms |
42248 KB |
Output is correct |
40 |
Correct |
1059 ms |
41936 KB |
Output is correct |
41 |
Correct |
1008 ms |
41184 KB |
Output is correct |
42 |
Correct |
1033 ms |
39852 KB |
Output is correct |
43 |
Correct |
1050 ms |
42760 KB |
Output is correct |
44 |
Correct |
1143 ms |
43144 KB |
Output is correct |
45 |
Correct |
962 ms |
42272 KB |
Output is correct |
46 |
Correct |
970 ms |
41160 KB |
Output is correct |
47 |
Correct |
922 ms |
39652 KB |
Output is correct |
48 |
Correct |
1002 ms |
39520 KB |
Output is correct |
49 |
Correct |
1027 ms |
41440 KB |
Output is correct |
50 |
Correct |
854 ms |
40604 KB |
Output is correct |
51 |
Correct |
931 ms |
41412 KB |
Output is correct |
52 |
Correct |
955 ms |
40772 KB |
Output is correct |
53 |
Correct |
1688 ms |
12132 KB |
Output is correct |
54 |
Correct |
1744 ms |
16588 KB |
Output is correct |
55 |
Correct |
1889 ms |
17100 KB |
Output is correct |
56 |
Correct |
1808 ms |
16828 KB |
Output is correct |
57 |
Correct |
1769 ms |
17080 KB |
Output is correct |
58 |
Correct |
1748 ms |
16352 KB |
Output is correct |
59 |
Correct |
1747 ms |
18052 KB |
Output is correct |
60 |
Correct |
1839 ms |
15640 KB |
Output is correct |
61 |
Correct |
1633 ms |
14684 KB |
Output is correct |
62 |
Correct |
1598 ms |
15140 KB |
Output is correct |
63 |
Correct |
1863 ms |
15680 KB |
Output is correct |
64 |
Correct |
1661 ms |
16148 KB |
Output is correct |
65 |
Correct |
1804 ms |
16084 KB |
Output is correct |
66 |
Correct |
1609 ms |
16168 KB |
Output is correct |
67 |
Correct |
1670 ms |
15308 KB |
Output is correct |
68 |
Correct |
1805 ms |
17356 KB |
Output is correct |
69 |
Correct |
940 ms |
45544 KB |
Output is correct |
70 |
Correct |
810 ms |
42828 KB |
Output is correct |
71 |
Correct |
852 ms |
46580 KB |
Output is correct |
72 |
Correct |
502 ms |
20304 KB |
Output is correct |
73 |
Correct |
517 ms |
19620 KB |
Output is correct |
74 |
Correct |
527 ms |
19640 KB |
Output is correct |
75 |
Correct |
1100 ms |
19892 KB |
Output is correct |
76 |
Correct |
1239 ms |
18892 KB |
Output is correct |
77 |
Correct |
1135 ms |
19004 KB |
Output is correct |
78 |
Correct |
1082 ms |
18496 KB |
Output is correct |