#include "race.h"
#include <cassert>
#include <cstdint>
#include <functional>
#include <unordered_map>
#include <vector>
using namespace std;
using i6 = int64_t;
struct Edge {
int len;
int dst;
};
unordered_map<int, vector<Edge>> g;
int res = INT32_MAX;
int n;
int k;
vector<bool> used;
int find_centroid(int root, int total_size) {
vector<int> sizes(n);
function<int(int, int)> find_sizes = [&](int p, int u) -> int {
for (auto e : g[u]) {
if (e.dst == p || used[e.dst])
continue;
sizes[u] += find_sizes(u, e.dst);
}
return sizes[u];
};
find_sizes(-1, root);
function<int(int, int)> find_centroid_impl = [&](int p, int u) {
for (auto e : g[u]) {
if (e.dst == p || used[e.dst])
continue;
if (sizes[e.dst] > total_size / 2) {
return find_centroid_impl(u, e.dst);
}
}
return u;
};
return find_centroid_impl(-1, root);
}
void compute_solution(int root, int total_size) {
if (used[root])
return;
int centroid = find_centroid(root, total_size);
used[centroid] = 1;
unordered_map<i6, int> min_depth_by_dist;
unordered_map<i6, int> new_min_depth_by_dist;
int comp_size = 0;
function<void(int, int, int, i6)> walk = [&](int parent, int u, int depth, i6 dist) {
comp_size++;
auto it = new_min_depth_by_dist.find(dist);
if (it == new_min_depth_by_dist.end()) {
new_min_depth_by_dist[dist] = depth;
} else {
new_min_depth_by_dist[dist] = min(it->second, depth);
}
for (auto e : g[u]) {
if (e.dst == parent || used[e.dst])
continue;
walk(u, e.dst, depth + 1, dist + e.len);
}
};
for (auto e : g[centroid]) {
if (used[e.dst])
continue;
comp_size = 0;
walk(centroid, e.dst, 1, e.len);
for (auto& [dist, depth] : new_min_depth_by_dist) {
i6 rem_dist = k - dist;
if (rem_dist == 0) {
assert(depth > 0);
res = min(res, depth);
} else {
auto it = min_depth_by_dist.find(rem_dist);
if (it != min_depth_by_dist.end()) {
assert(depth + it->second > 0);
res = min(res, depth + it->second);
}
}
}
for (auto& [dist, depth] : new_min_depth_by_dist) {
auto it = min_depth_by_dist.find(dist);
if (it == min_depth_by_dist.end()) {
min_depth_by_dist[dist] = depth;
} else {
min_depth_by_dist[dist] = min(it->second, depth);
}
}
new_min_depth_by_dist.clear();
compute_solution(e.dst, comp_size);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
used.resize(N);
for (int i = 0; i < N; i++) {
g[H[i][0]].push_back({L[i], H[i][1]});
g[H[i][1]].push_back({L[i], H[i][0]});
}
compute_solution(0, n);
return res == INT32_MAX ? -1 : res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2808 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2808 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2808 KB |
Output is correct |
16 |
Correct |
2 ms |
2832 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
1 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2808 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2808 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2808 KB |
Output is correct |
16 |
Correct |
2 ms |
2832 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
1 ms |
2640 KB |
Output is correct |
19 |
Correct |
1 ms |
2556 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
3 ms |
2640 KB |
Output is correct |
22 |
Correct |
5 ms |
2896 KB |
Output is correct |
23 |
Correct |
8 ms |
3664 KB |
Output is correct |
24 |
Correct |
5 ms |
2896 KB |
Output is correct |
25 |
Correct |
3 ms |
2812 KB |
Output is correct |
26 |
Correct |
3 ms |
2640 KB |
Output is correct |
27 |
Correct |
3 ms |
2384 KB |
Output is correct |
28 |
Correct |
4 ms |
2896 KB |
Output is correct |
29 |
Correct |
3 ms |
2808 KB |
Output is correct |
30 |
Correct |
3 ms |
2760 KB |
Output is correct |
31 |
Correct |
3 ms |
2640 KB |
Output is correct |
32 |
Correct |
3 ms |
2900 KB |
Output is correct |
33 |
Correct |
5 ms |
3064 KB |
Output is correct |
34 |
Correct |
29 ms |
8084 KB |
Output is correct |
35 |
Correct |
32 ms |
9372 KB |
Output is correct |
36 |
Correct |
38 ms |
11604 KB |
Output is correct |
37 |
Correct |
18 ms |
5244 KB |
Output is correct |
38 |
Correct |
13 ms |
4176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2808 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2808 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2808 KB |
Output is correct |
16 |
Correct |
2 ms |
2832 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
1 ms |
2640 KB |
Output is correct |
19 |
Correct |
1064 ms |
14428 KB |
Output is correct |
20 |
Correct |
1101 ms |
14524 KB |
Output is correct |
21 |
Correct |
988 ms |
14516 KB |
Output is correct |
22 |
Correct |
908 ms |
14524 KB |
Output is correct |
23 |
Correct |
916 ms |
15776 KB |
Output is correct |
24 |
Correct |
687 ms |
14780 KB |
Output is correct |
25 |
Execution timed out |
3060 ms |
146044 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2640 KB |
Output is correct |
6 |
Correct |
2 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2904 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2808 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2808 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2808 KB |
Output is correct |
16 |
Correct |
2 ms |
2832 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
1 ms |
2640 KB |
Output is correct |
19 |
Correct |
1 ms |
2556 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
3 ms |
2640 KB |
Output is correct |
22 |
Correct |
5 ms |
2896 KB |
Output is correct |
23 |
Correct |
8 ms |
3664 KB |
Output is correct |
24 |
Correct |
5 ms |
2896 KB |
Output is correct |
25 |
Correct |
3 ms |
2812 KB |
Output is correct |
26 |
Correct |
3 ms |
2640 KB |
Output is correct |
27 |
Correct |
3 ms |
2384 KB |
Output is correct |
28 |
Correct |
4 ms |
2896 KB |
Output is correct |
29 |
Correct |
3 ms |
2808 KB |
Output is correct |
30 |
Correct |
3 ms |
2760 KB |
Output is correct |
31 |
Correct |
3 ms |
2640 KB |
Output is correct |
32 |
Correct |
3 ms |
2900 KB |
Output is correct |
33 |
Correct |
5 ms |
3064 KB |
Output is correct |
34 |
Correct |
29 ms |
8084 KB |
Output is correct |
35 |
Correct |
32 ms |
9372 KB |
Output is correct |
36 |
Correct |
38 ms |
11604 KB |
Output is correct |
37 |
Correct |
18 ms |
5244 KB |
Output is correct |
38 |
Correct |
13 ms |
4176 KB |
Output is correct |
39 |
Correct |
1064 ms |
14428 KB |
Output is correct |
40 |
Correct |
1101 ms |
14524 KB |
Output is correct |
41 |
Correct |
988 ms |
14516 KB |
Output is correct |
42 |
Correct |
908 ms |
14524 KB |
Output is correct |
43 |
Correct |
916 ms |
15776 KB |
Output is correct |
44 |
Correct |
687 ms |
14780 KB |
Output is correct |
45 |
Execution timed out |
3060 ms |
146044 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |