#include <bits/stdc++.h>
typedef long long ll;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, k;
std::cin >> n >> k;
std::vector<std::vector<std::pair<ll, ll>>> adj(n + 1);
for (ll i = 0, u, v, w; i < n - 1; ++i) {
std::cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
std::vector<ll> ans;
std::function<std::pair<bool, ll>(ll, ll, ll)> dfs;
dfs = [&](ll x, ll par, ll parent_edge) {
ll furthest_unprotected = 0;
ll station_reachability = -1e15;
for (auto &[to, w] : adj[x]) {
if (to == par) {
continue;
}
auto res = dfs(to, x, w);
if (res.first) {
station_reachability = std::max(station_reachability, res.second);
} else {
furthest_unprotected = std::max(furthest_unprotected, res.second);
}
}
std::pair<bool, ll> ret;
if (station_reachability >= furthest_unprotected) {
// We cover everything in the subtree
ret = {true, station_reachability - parent_edge};
} else {
// Okay, let's update the furthest node
ret = {false, furthest_unprotected + parent_edge};
}
if (!ret.first and ret.second > k) {
// If x => par[x] makes a node unreachable from anything outside x's
// subtree, then x is a forced pick.
ans.push_back(x);
ret = {true, k - parent_edge};
}
// If this happens, we aren't protecting anything anymore. At the same time,
// we have still protected everything in our subtree.
if (ret.first and ret.second < 0) {
ret = {false, 0};
}
return ret;
};
// The reason we're making the parent edge very large is because by default
// our algorithm tries to get the parent to place down a fire station if the
// distance is small enough, since this is optimal. However, in the case of 1,
// we have no parent, so this would behave as if there was a large barrier
// between 1 and its parent, forcing a placemenet (if necessary) at 1 itself.
dfs(1, 0, 1e15);
std::cout << ans.size() << '\n';
for (auto &i : ans) {
std::cout << i << ' ';
}
std::cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
34824 KB |
Output is correct |
2 |
Correct |
159 ms |
34752 KB |
Output is correct |
3 |
Correct |
51 ms |
12996 KB |
Output is correct |
4 |
Correct |
152 ms |
33700 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
189 ms |
34616 KB |
Output is correct |
2 |
Correct |
66 ms |
17360 KB |
Output is correct |
3 |
Correct |
72 ms |
17872 KB |
Output is correct |
4 |
Correct |
57 ms |
15564 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
452 KB |
Output is correct |
7 |
Correct |
116 ms |
30988 KB |
Output is correct |
8 |
Correct |
120 ms |
30904 KB |
Output is correct |
9 |
Correct |
136 ms |
31168 KB |
Output is correct |
10 |
Correct |
121 ms |
31168 KB |
Output is correct |
11 |
Correct |
184 ms |
34648 KB |
Output is correct |
12 |
Correct |
110 ms |
21428 KB |
Output is correct |
13 |
Correct |
59 ms |
13388 KB |
Output is correct |
14 |
Correct |
113 ms |
20072 KB |
Output is correct |
15 |
Correct |
132 ms |
24260 KB |
Output is correct |
16 |
Correct |
105 ms |
26064 KB |
Output is correct |
17 |
Correct |
93 ms |
21836 KB |
Output is correct |
18 |
Correct |
89 ms |
21372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
476 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
2 ms |
936 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
684 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
856 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
126 ms |
29456 KB |
Output is correct |
2 |
Correct |
117 ms |
27728 KB |
Output is correct |
3 |
Correct |
138 ms |
29896 KB |
Output is correct |
4 |
Correct |
59 ms |
14152 KB |
Output is correct |
5 |
Correct |
183 ms |
45240 KB |
Output is correct |
6 |
Correct |
212 ms |
43180 KB |
Output is correct |
7 |
Correct |
164 ms |
49284 KB |
Output is correct |
8 |
Correct |
159 ms |
47816 KB |
Output is correct |
9 |
Correct |
141 ms |
40652 KB |
Output is correct |
10 |
Correct |
164 ms |
39368 KB |
Output is correct |
11 |
Correct |
158 ms |
51652 KB |
Output is correct |
12 |
Correct |
82 ms |
20304 KB |
Output is correct |
13 |
Correct |
151 ms |
42440 KB |
Output is correct |
14 |
Correct |
157 ms |
37964 KB |
Output is correct |
15 |
Correct |
124 ms |
30292 KB |
Output is correct |
16 |
Correct |
121 ms |
28500 KB |
Output is correct |
17 |
Correct |
117 ms |
28144 KB |
Output is correct |
18 |
Correct |
138 ms |
30144 KB |
Output is correct |
19 |
Correct |
82 ms |
21072 KB |
Output is correct |
20 |
Correct |
49 ms |
12116 KB |
Output is correct |
21 |
Correct |
139 ms |
30176 KB |
Output is correct |