#include "race.h"
#include <bits/stdc++.h>
using namespace std;
namespace std {
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template <class Fun>
class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T&& fun) : fun_(std::forward<T>(fun)) {}
template <class... Args>
decltype(auto) operator()(Args&&... args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun&& fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
}; // namespace std
template <class T>
bool chmin(T& _old, T _new) { return _old > _new && (_old = _new, true); }
template <class T>
bool chmax(T& _old, T _new) { return _old < _new && (_old = _new, true); }
int best_path(int n, int k, int e[][2], int w[]) {
constexpr int INF = 0x3f3f3f3f;
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n - 1; ++i) {
adj[e[i][0]].emplace_back(w[i], e[i][1]);
adj[e[i][1]].emplace_back(w[i], e[i][0]);
}
vector<int> sz(n), dist(n), best(k + 1, INF), checked;
vector<bool> erased(n);
vector<pair<int, int>> dists;
int ans = INF, s;
auto dfs_sz = y_combinator([&](auto self, int u, int p) -> int {
sz[u] = 1;
for (const auto& [w, v] : adj[u]) {
if (v != p && !erased[v]) {
sz[u] += self(v, u);
}
}
return sz[u];
});
auto dfs_ct = y_combinator([&](auto self, int u, int p) -> int {
for (const auto& [w, v] : adj[u]) {
if (v != p && !erased[v] && sz[v] > s / 2) {
return self(v, u);
}
}
return u;
});
auto dfs = y_combinator([&](auto self, int u, int p, int edges) -> void {
dists.emplace_back(dist[u], edges);
checked.emplace_back(dist[u]);
chmin(ans, edges + best[k - dist[u]]);
for (const auto& [w, v] : adj[u]) {
if (v != p && !erased[v] && dist[u] + w <= k) {
dist[v] = dist[u] + w;
self(v, u, edges + 1);
}
}
});
y_combinator([&](auto self, int u) -> void {
s = dfs_sz(u, -1);
u = dfs_ct(u, -1);
erased[u] = true;
for (const auto [w, v] : adj[u]) {
if (erased[v] || w > k) continue;
dist[v] = w;
dfs(v, u, 1);
for (const auto& [d, e] : dists) {
chmin(best[d], e);
}
dists.clear();
}
chmin(ans, best[k]);
for (const auto& x : checked) {
best[x] = INF;
}
checked.clear();
for (const auto& [w, v] : adj[u]) {
if (!erased[v]) {
self(v);
}
}
})(0);
return ans == INF ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
3916 KB |
Output is correct |
23 |
Correct |
3 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3788 KB |
Output is correct |
25 |
Correct |
3 ms |
3636 KB |
Output is correct |
26 |
Correct |
2 ms |
1612 KB |
Output is correct |
27 |
Correct |
3 ms |
3532 KB |
Output is correct |
28 |
Correct |
2 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1612 KB |
Output is correct |
30 |
Correct |
2 ms |
1740 KB |
Output is correct |
31 |
Correct |
3 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
3148 KB |
Output is correct |
33 |
Correct |
3 ms |
3404 KB |
Output is correct |
34 |
Correct |
3 ms |
2636 KB |
Output is correct |
35 |
Correct |
3 ms |
3532 KB |
Output is correct |
36 |
Correct |
3 ms |
4044 KB |
Output is correct |
37 |
Correct |
3 ms |
3404 KB |
Output is correct |
38 |
Correct |
2 ms |
2356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
134 ms |
8572 KB |
Output is correct |
20 |
Correct |
138 ms |
8516 KB |
Output is correct |
21 |
Correct |
161 ms |
8920 KB |
Output is correct |
22 |
Correct |
170 ms |
9488 KB |
Output is correct |
23 |
Correct |
87 ms |
8696 KB |
Output is correct |
24 |
Correct |
70 ms |
8496 KB |
Output is correct |
25 |
Correct |
150 ms |
10244 KB |
Output is correct |
26 |
Correct |
133 ms |
13376 KB |
Output is correct |
27 |
Correct |
203 ms |
16576 KB |
Output is correct |
28 |
Correct |
324 ms |
24084 KB |
Output is correct |
29 |
Correct |
316 ms |
23456 KB |
Output is correct |
30 |
Correct |
197 ms |
16580 KB |
Output is correct |
31 |
Correct |
191 ms |
16676 KB |
Output is correct |
32 |
Correct |
246 ms |
16692 KB |
Output is correct |
33 |
Correct |
274 ms |
15516 KB |
Output is correct |
34 |
Correct |
230 ms |
15440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
3916 KB |
Output is correct |
23 |
Correct |
3 ms |
3276 KB |
Output is correct |
24 |
Correct |
3 ms |
3788 KB |
Output is correct |
25 |
Correct |
3 ms |
3636 KB |
Output is correct |
26 |
Correct |
2 ms |
1612 KB |
Output is correct |
27 |
Correct |
3 ms |
3532 KB |
Output is correct |
28 |
Correct |
2 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1612 KB |
Output is correct |
30 |
Correct |
2 ms |
1740 KB |
Output is correct |
31 |
Correct |
3 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
3148 KB |
Output is correct |
33 |
Correct |
3 ms |
3404 KB |
Output is correct |
34 |
Correct |
3 ms |
2636 KB |
Output is correct |
35 |
Correct |
3 ms |
3532 KB |
Output is correct |
36 |
Correct |
3 ms |
4044 KB |
Output is correct |
37 |
Correct |
3 ms |
3404 KB |
Output is correct |
38 |
Correct |
2 ms |
2356 KB |
Output is correct |
39 |
Correct |
134 ms |
8572 KB |
Output is correct |
40 |
Correct |
138 ms |
8516 KB |
Output is correct |
41 |
Correct |
161 ms |
8920 KB |
Output is correct |
42 |
Correct |
170 ms |
9488 KB |
Output is correct |
43 |
Correct |
87 ms |
8696 KB |
Output is correct |
44 |
Correct |
70 ms |
8496 KB |
Output is correct |
45 |
Correct |
150 ms |
10244 KB |
Output is correct |
46 |
Correct |
133 ms |
13376 KB |
Output is correct |
47 |
Correct |
203 ms |
16576 KB |
Output is correct |
48 |
Correct |
324 ms |
24084 KB |
Output is correct |
49 |
Correct |
316 ms |
23456 KB |
Output is correct |
50 |
Correct |
197 ms |
16580 KB |
Output is correct |
51 |
Correct |
191 ms |
16676 KB |
Output is correct |
52 |
Correct |
246 ms |
16692 KB |
Output is correct |
53 |
Correct |
274 ms |
15516 KB |
Output is correct |
54 |
Correct |
230 ms |
15440 KB |
Output is correct |
55 |
Correct |
14 ms |
1356 KB |
Output is correct |
56 |
Correct |
11 ms |
1356 KB |
Output is correct |
57 |
Correct |
106 ms |
9900 KB |
Output is correct |
58 |
Correct |
43 ms |
9780 KB |
Output is correct |
59 |
Correct |
126 ms |
14320 KB |
Output is correct |
60 |
Correct |
515 ms |
30136 KB |
Output is correct |
61 |
Correct |
237 ms |
17220 KB |
Output is correct |
62 |
Correct |
235 ms |
21872 KB |
Output is correct |
63 |
Correct |
311 ms |
21820 KB |
Output is correct |
64 |
Correct |
526 ms |
20800 KB |
Output is correct |
65 |
Correct |
260 ms |
16708 KB |
Output is correct |
66 |
Correct |
444 ms |
26160 KB |
Output is correct |
67 |
Correct |
151 ms |
22832 KB |
Output is correct |
68 |
Correct |
294 ms |
21344 KB |
Output is correct |
69 |
Correct |
298 ms |
21512 KB |
Output is correct |
70 |
Correct |
288 ms |
20680 KB |
Output is correct |