#include <bits/stdc++.h>
#include "race.h"
using ll = long long;
using pii = std::pair<int, int>;
std::vector < std::vector < pii > > g;
std::vector < ll > dist;
std::vector < int > depth;
void DFS1(int v, int p, ll sum, int d) {
dist[v] = sum;
depth[v] = d;
for (auto [u, w] : g[v])
if (u != p)
DFS1(u, v, sum + w, d + 1);
}
int len, ans = INT_MAX;
std::map < ll, int > DFS2(int v, int p) {
std::map < ll, int > dp;
dp[dist[v]] = depth[v];
for (auto [u, w] : g[v])
if (u != p) {
auto sub = DFS2(u, v);
if (sub.size() > dp.size())
std::swap(sub, dp);
for (auto [dist_, depth_] : sub)
if (dp.count(len + 2 * dist[v] - dist_)) {
ans = std::min(ans, dp[len + 2 * dist[v] - dist_] + depth_ - 2 * depth[v]);
}
for (auto [dist, depth] : sub)
if (!dp.count(dist))
dp[dist] = depth;
else
dp[dist] = std::min(dp[dist], depth);
}
return dp;
}
int best_path(int N, int K, int H[][2], int L[]) {
dist.resize(N);
depth.resize(N);
g.resize(N);
for (int i = 0; i < N - 1; i++) {
g[H[i][0]].push_back({ H[i][1], L[i] });
g[H[i][1]].push_back({ H[i][0], L[i] });
}
DFS1(0, 0, 0, 0);
len = K;
DFS2(0, 0);
return ans == INT_MAX ? -1 : ans;
}
// int main() {
// int n, k; std::cin >> n >> k;
// int h[n][2], l[n];
// for (int i = 0; i < n - 1; i++)
// std::cin >> h[i][0] >> h[i][1] >> l[i];
// std::cout << best_path(n, k, h, l) << "\n";
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2676 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2676 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2508 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2504 KB |
Output is correct |
27 |
Correct |
1 ms |
2400 KB |
Output is correct |
28 |
Correct |
2 ms |
2400 KB |
Output is correct |
29 |
Correct |
1 ms |
2404 KB |
Output is correct |
30 |
Correct |
2 ms |
2400 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2400 KB |
Output is correct |
33 |
Correct |
2 ms |
2648 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2504 KB |
Output is correct |
38 |
Correct |
1 ms |
2848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2676 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
65 ms |
11880 KB |
Output is correct |
20 |
Correct |
66 ms |
12880 KB |
Output is correct |
21 |
Correct |
64 ms |
12628 KB |
Output is correct |
22 |
Correct |
62 ms |
13300 KB |
Output is correct |
23 |
Correct |
88 ms |
13768 KB |
Output is correct |
24 |
Correct |
69 ms |
13376 KB |
Output is correct |
25 |
Correct |
63 ms |
29252 KB |
Output is correct |
26 |
Correct |
56 ms |
43604 KB |
Output is correct |
27 |
Correct |
141 ms |
21236 KB |
Output is correct |
28 |
Correct |
201 ms |
81968 KB |
Output is correct |
29 |
Correct |
197 ms |
79580 KB |
Output is correct |
30 |
Correct |
115 ms |
21648 KB |
Output is correct |
31 |
Correct |
123 ms |
21816 KB |
Output is correct |
32 |
Correct |
146 ms |
21596 KB |
Output is correct |
33 |
Correct |
172 ms |
21584 KB |
Output is correct |
34 |
Correct |
229 ms |
34404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2676 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2508 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2504 KB |
Output is correct |
27 |
Correct |
1 ms |
2400 KB |
Output is correct |
28 |
Correct |
2 ms |
2400 KB |
Output is correct |
29 |
Correct |
1 ms |
2404 KB |
Output is correct |
30 |
Correct |
2 ms |
2400 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2400 KB |
Output is correct |
33 |
Correct |
2 ms |
2648 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2504 KB |
Output is correct |
38 |
Correct |
1 ms |
2848 KB |
Output is correct |
39 |
Correct |
65 ms |
11880 KB |
Output is correct |
40 |
Correct |
66 ms |
12880 KB |
Output is correct |
41 |
Correct |
64 ms |
12628 KB |
Output is correct |
42 |
Correct |
62 ms |
13300 KB |
Output is correct |
43 |
Correct |
88 ms |
13768 KB |
Output is correct |
44 |
Correct |
69 ms |
13376 KB |
Output is correct |
45 |
Correct |
63 ms |
29252 KB |
Output is correct |
46 |
Correct |
56 ms |
43604 KB |
Output is correct |
47 |
Correct |
141 ms |
21236 KB |
Output is correct |
48 |
Correct |
201 ms |
81968 KB |
Output is correct |
49 |
Correct |
197 ms |
79580 KB |
Output is correct |
50 |
Correct |
115 ms |
21648 KB |
Output is correct |
51 |
Correct |
123 ms |
21816 KB |
Output is correct |
52 |
Correct |
146 ms |
21596 KB |
Output is correct |
53 |
Correct |
172 ms |
21584 KB |
Output is correct |
54 |
Correct |
229 ms |
34404 KB |
Output is correct |
55 |
Correct |
10 ms |
3740 KB |
Output is correct |
56 |
Correct |
5 ms |
3160 KB |
Output is correct |
57 |
Correct |
41 ms |
12120 KB |
Output is correct |
58 |
Correct |
38 ms |
13040 KB |
Output is correct |
59 |
Correct |
74 ms |
43516 KB |
Output is correct |
60 |
Correct |
193 ms |
80384 KB |
Output is correct |
61 |
Correct |
144 ms |
22616 KB |
Output is correct |
62 |
Correct |
111 ms |
21700 KB |
Output is correct |
63 |
Correct |
144 ms |
21584 KB |
Output is correct |
64 |
Correct |
335 ms |
28732 KB |
Output is correct |
65 |
Correct |
314 ms |
35084 KB |
Output is correct |
66 |
Correct |
208 ms |
71508 KB |
Output is correct |
67 |
Correct |
89 ms |
22976 KB |
Output is correct |
68 |
Correct |
231 ms |
32416 KB |
Output is correct |
69 |
Correct |
240 ms |
34724 KB |
Output is correct |
70 |
Correct |
224 ms |
32012 KB |
Output is correct |