#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
template <class F>
struct y_combinator_result {
F f;
template <class T>
explicit y_combinator_result(T &&f_) : f(std::forward<T>(f_)) {}
template <class... Args>
decltype(auto) operator()(Args &&...args) {
return f(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class F>
decltype(auto) y_combinator(F &&f) {
return y_combinator_result<std::decay_t<F>>(std::forward<F>(f));
}
int best_path(int n, int k, int h[][2], int l[]) {
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
g[h[i][0]].emplace_back(i);
g[h[i][1]].emplace_back(i);
}
int ans = n;
vector<int> depth(n);
vector<long long> dist(n);
vector<map<long long, int>> a(n);
y_combinator([&](auto dfs, int v, int p) -> void {
vector<int> c;
for (int id : g[v]) {
int to = v ^ h[id][0] ^ h[id][1];
if (to == p) {
continue;
}
c.emplace_back(to);
depth[to] = depth[v] + 1;
dist[to] = dist[v] + l[id];
dfs(to, v);
long long t = dist[v] + k;
if (a[to].count(t)) {
ans = min(ans, a[to][t] - depth[v]);
}
}
if (c.empty()) {
a[v][dist[v]] = depth[v];
return;
}
sort(c.begin(), c.end(), [&](int i, int j) {
return a[i].size() > a[j].size();
});
swap(a[v], a[c[0]]);
a[v][dist[v]] = depth[v];
for (int i = 1; i < (int) c.size(); i++) {
for (auto q : a[c[i]]) {
long long t = 2 * dist[v] - q.first + k;
if (a[v].count(t)) {
ans = min(ans, a[v][t] + q.second - 2 * depth[v]);
}
}
for (auto q : a[c[i]]) {
if (!a[v].count(q.first)) {
a[v][q.first] = n;
}
a[v][q.first] = min(a[v][q.first], q.second);
}
}
})(0, -1);
if (ans == n) {
ans = -1;
}
return ans;
}
#ifdef tabr
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
2 ms |
588 KB |
Output is correct |
30 |
Correct |
2 ms |
588 KB |
Output is correct |
31 |
Correct |
2 ms |
588 KB |
Output is correct |
32 |
Correct |
3 ms |
588 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
588 KB |
Output is correct |
37 |
Correct |
2 ms |
528 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
126 ms |
23880 KB |
Output is correct |
20 |
Correct |
130 ms |
23888 KB |
Output is correct |
21 |
Correct |
128 ms |
23868 KB |
Output is correct |
22 |
Correct |
130 ms |
24024 KB |
Output is correct |
23 |
Correct |
183 ms |
36812 KB |
Output is correct |
24 |
Correct |
157 ms |
30304 KB |
Output is correct |
25 |
Correct |
81 ms |
26372 KB |
Output is correct |
26 |
Correct |
57 ms |
37872 KB |
Output is correct |
27 |
Correct |
210 ms |
38792 KB |
Output is correct |
28 |
Correct |
223 ms |
81612 KB |
Output is correct |
29 |
Correct |
226 ms |
79152 KB |
Output is correct |
30 |
Correct |
202 ms |
38820 KB |
Output is correct |
31 |
Correct |
206 ms |
38876 KB |
Output is correct |
32 |
Correct |
254 ms |
38784 KB |
Output is correct |
33 |
Correct |
214 ms |
38300 KB |
Output is correct |
34 |
Correct |
390 ms |
70316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
2 ms |
588 KB |
Output is correct |
30 |
Correct |
2 ms |
588 KB |
Output is correct |
31 |
Correct |
2 ms |
588 KB |
Output is correct |
32 |
Correct |
3 ms |
588 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
588 KB |
Output is correct |
37 |
Correct |
2 ms |
528 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
39 |
Correct |
126 ms |
23880 KB |
Output is correct |
40 |
Correct |
130 ms |
23888 KB |
Output is correct |
41 |
Correct |
128 ms |
23868 KB |
Output is correct |
42 |
Correct |
130 ms |
24024 KB |
Output is correct |
43 |
Correct |
183 ms |
36812 KB |
Output is correct |
44 |
Correct |
157 ms |
30304 KB |
Output is correct |
45 |
Correct |
81 ms |
26372 KB |
Output is correct |
46 |
Correct |
57 ms |
37872 KB |
Output is correct |
47 |
Correct |
210 ms |
38792 KB |
Output is correct |
48 |
Correct |
223 ms |
81612 KB |
Output is correct |
49 |
Correct |
226 ms |
79152 KB |
Output is correct |
50 |
Correct |
202 ms |
38820 KB |
Output is correct |
51 |
Correct |
206 ms |
38876 KB |
Output is correct |
52 |
Correct |
254 ms |
38784 KB |
Output is correct |
53 |
Correct |
214 ms |
38300 KB |
Output is correct |
54 |
Correct |
390 ms |
70316 KB |
Output is correct |
55 |
Correct |
16 ms |
3660 KB |
Output is correct |
56 |
Correct |
7 ms |
2324 KB |
Output is correct |
57 |
Correct |
88 ms |
21972 KB |
Output is correct |
58 |
Correct |
59 ms |
19812 KB |
Output is correct |
59 |
Correct |
74 ms |
40972 KB |
Output is correct |
60 |
Correct |
220 ms |
80172 KB |
Output is correct |
61 |
Correct |
253 ms |
42096 KB |
Output is correct |
62 |
Correct |
192 ms |
38704 KB |
Output is correct |
63 |
Correct |
247 ms |
38892 KB |
Output is correct |
64 |
Correct |
551 ms |
88396 KB |
Output is correct |
65 |
Correct |
568 ms |
88824 KB |
Output is correct |
66 |
Correct |
231 ms |
73924 KB |
Output is correct |
67 |
Correct |
222 ms |
39608 KB |
Output is correct |
68 |
Correct |
363 ms |
56552 KB |
Output is correct |
69 |
Correct |
368 ms |
57268 KB |
Output is correct |
70 |
Correct |
354 ms |
54084 KB |
Output is correct |