#include <iostream>
#include <vector>
#include <queue>
#include "race.h"
using namespace std;
int n, k, a, b, w, sz, c, ans = 1e9;
vector <vector <pair <int, int>>> g(2e5);
vector <bool> del(2e5, false);
vector <int> mn(1e6 + 1, 1e9);
queue <int> q;
int find_c(int x) {
if (del[x]) return 0;
del[x] = true;
int mx = 0, sum = 1;
for (auto& p : g[x]) {
a = find_c(p.first);
mx = max(mx, a);
sum += a;
}
if (mx <= sz / 2 && sz - sum <= sz / 2) c = x;
del[x] = false;
return sum;
}
void dfs1(int x, int d, int wd) {
if (del[x] || wd > k) return;
del[x] = true;
ans = min(ans, d + mn[k - wd]);
for (auto& p : g[x]) dfs1(p.first, d + 1, wd + p.second);
del[x] = false;
}
void dfs2(int x, int d, int wd) {
if (del[x] || wd > k) return;
del[x] = true;
mn[wd] = min(mn[wd], d);
q.push(wd);
for (auto& p : g[x]) dfs2(p.first, d + 1, wd + p.second);
del[x] = false;
}
void solve(int x) {
if (del[x]) return;
sz = find_c(x);
find_c(x);
del[c] = true;
for (auto& p : g[c]) {
dfs1(p.first, 1, p.second);
dfs2(p.first, 1, p.second);
}
while (q.size()) { mn[q.front()] = 1e9; q.pop(); }
for (auto& p : g[c]) solve(p.first);
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N; k = K;
for (int i = 0; i < n - 1; i++) {
a = H[i][0];
b = H[i][1];
w = L[i];
g[a].push_back({ b,w });
g[b].push_back({ a,w });
}
mn[0] = 0;
solve(0);
return (ans < 1e9 ? ans : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8956 KB |
Output is correct |
4 |
Correct |
4 ms |
8960 KB |
Output is correct |
5 |
Correct |
4 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
5 ms |
8904 KB |
Output is correct |
9 |
Correct |
4 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8960 KB |
Output is correct |
11 |
Correct |
4 ms |
8952 KB |
Output is correct |
12 |
Correct |
4 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
4 ms |
8956 KB |
Output is correct |
15 |
Correct |
5 ms |
8952 KB |
Output is correct |
16 |
Correct |
4 ms |
8852 KB |
Output is correct |
17 |
Correct |
4 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8956 KB |
Output is correct |
4 |
Correct |
4 ms |
8960 KB |
Output is correct |
5 |
Correct |
4 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
5 ms |
8904 KB |
Output is correct |
9 |
Correct |
4 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8960 KB |
Output is correct |
11 |
Correct |
4 ms |
8952 KB |
Output is correct |
12 |
Correct |
4 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
4 ms |
8956 KB |
Output is correct |
15 |
Correct |
5 ms |
8952 KB |
Output is correct |
16 |
Correct |
4 ms |
8852 KB |
Output is correct |
17 |
Correct |
4 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8916 KB |
Output is correct |
19 |
Correct |
4 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8952 KB |
Output is correct |
21 |
Correct |
5 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
9028 KB |
Output is correct |
26 |
Correct |
5 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8908 KB |
Output is correct |
28 |
Correct |
5 ms |
8964 KB |
Output is correct |
29 |
Correct |
5 ms |
8964 KB |
Output is correct |
30 |
Correct |
5 ms |
8916 KB |
Output is correct |
31 |
Correct |
5 ms |
8964 KB |
Output is correct |
32 |
Correct |
5 ms |
8916 KB |
Output is correct |
33 |
Correct |
5 ms |
8916 KB |
Output is correct |
34 |
Correct |
6 ms |
8972 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
5 ms |
8968 KB |
Output is correct |
37 |
Correct |
5 ms |
8964 KB |
Output is correct |
38 |
Correct |
5 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8956 KB |
Output is correct |
4 |
Correct |
4 ms |
8960 KB |
Output is correct |
5 |
Correct |
4 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
5 ms |
8904 KB |
Output is correct |
9 |
Correct |
4 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8960 KB |
Output is correct |
11 |
Correct |
4 ms |
8952 KB |
Output is correct |
12 |
Correct |
4 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
4 ms |
8956 KB |
Output is correct |
15 |
Correct |
5 ms |
8952 KB |
Output is correct |
16 |
Correct |
4 ms |
8852 KB |
Output is correct |
17 |
Correct |
4 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8916 KB |
Output is correct |
19 |
Correct |
115 ms |
15100 KB |
Output is correct |
20 |
Correct |
112 ms |
15156 KB |
Output is correct |
21 |
Correct |
127 ms |
15300 KB |
Output is correct |
22 |
Correct |
107 ms |
15492 KB |
Output is correct |
23 |
Correct |
96 ms |
15468 KB |
Output is correct |
24 |
Correct |
52 ms |
15356 KB |
Output is correct |
25 |
Correct |
112 ms |
18728 KB |
Output is correct |
26 |
Correct |
102 ms |
22784 KB |
Output is correct |
27 |
Correct |
126 ms |
21756 KB |
Output is correct |
28 |
Correct |
242 ms |
36212 KB |
Output is correct |
29 |
Correct |
244 ms |
35068 KB |
Output is correct |
30 |
Correct |
127 ms |
21704 KB |
Output is correct |
31 |
Correct |
161 ms |
21788 KB |
Output is correct |
32 |
Correct |
147 ms |
21888 KB |
Output is correct |
33 |
Correct |
206 ms |
20640 KB |
Output is correct |
34 |
Correct |
158 ms |
21476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8916 KB |
Output is correct |
2 |
Correct |
4 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8956 KB |
Output is correct |
4 |
Correct |
4 ms |
8960 KB |
Output is correct |
5 |
Correct |
4 ms |
8916 KB |
Output is correct |
6 |
Correct |
4 ms |
8916 KB |
Output is correct |
7 |
Correct |
4 ms |
8916 KB |
Output is correct |
8 |
Correct |
5 ms |
8904 KB |
Output is correct |
9 |
Correct |
4 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8960 KB |
Output is correct |
11 |
Correct |
4 ms |
8952 KB |
Output is correct |
12 |
Correct |
4 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
4 ms |
8956 KB |
Output is correct |
15 |
Correct |
5 ms |
8952 KB |
Output is correct |
16 |
Correct |
4 ms |
8852 KB |
Output is correct |
17 |
Correct |
4 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8916 KB |
Output is correct |
19 |
Correct |
4 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8952 KB |
Output is correct |
21 |
Correct |
5 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
9028 KB |
Output is correct |
26 |
Correct |
5 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8908 KB |
Output is correct |
28 |
Correct |
5 ms |
8964 KB |
Output is correct |
29 |
Correct |
5 ms |
8964 KB |
Output is correct |
30 |
Correct |
5 ms |
8916 KB |
Output is correct |
31 |
Correct |
5 ms |
8964 KB |
Output is correct |
32 |
Correct |
5 ms |
8916 KB |
Output is correct |
33 |
Correct |
5 ms |
8916 KB |
Output is correct |
34 |
Correct |
6 ms |
8972 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
5 ms |
8968 KB |
Output is correct |
37 |
Correct |
5 ms |
8964 KB |
Output is correct |
38 |
Correct |
5 ms |
8916 KB |
Output is correct |
39 |
Correct |
115 ms |
15100 KB |
Output is correct |
40 |
Correct |
112 ms |
15156 KB |
Output is correct |
41 |
Correct |
127 ms |
15300 KB |
Output is correct |
42 |
Correct |
107 ms |
15492 KB |
Output is correct |
43 |
Correct |
96 ms |
15468 KB |
Output is correct |
44 |
Correct |
52 ms |
15356 KB |
Output is correct |
45 |
Correct |
112 ms |
18728 KB |
Output is correct |
46 |
Correct |
102 ms |
22784 KB |
Output is correct |
47 |
Correct |
126 ms |
21756 KB |
Output is correct |
48 |
Correct |
242 ms |
36212 KB |
Output is correct |
49 |
Correct |
244 ms |
35068 KB |
Output is correct |
50 |
Correct |
127 ms |
21704 KB |
Output is correct |
51 |
Correct |
161 ms |
21788 KB |
Output is correct |
52 |
Correct |
147 ms |
21888 KB |
Output is correct |
53 |
Correct |
206 ms |
20640 KB |
Output is correct |
54 |
Correct |
158 ms |
21476 KB |
Output is correct |
55 |
Correct |
12 ms |
9608 KB |
Output is correct |
56 |
Correct |
14 ms |
9480 KB |
Output is correct |
57 |
Correct |
84 ms |
15700 KB |
Output is correct |
58 |
Correct |
34 ms |
15480 KB |
Output is correct |
59 |
Correct |
99 ms |
22820 KB |
Output is correct |
60 |
Correct |
359 ms |
36124 KB |
Output is correct |
61 |
Correct |
155 ms |
21972 KB |
Output is correct |
62 |
Correct |
163 ms |
22716 KB |
Output is correct |
63 |
Correct |
185 ms |
22676 KB |
Output is correct |
64 |
Correct |
359 ms |
22884 KB |
Output is correct |
65 |
Correct |
219 ms |
22548 KB |
Output is correct |
66 |
Correct |
298 ms |
32556 KB |
Output is correct |
67 |
Correct |
80 ms |
23300 KB |
Output is correct |
68 |
Correct |
197 ms |
22972 KB |
Output is correct |
69 |
Correct |
183 ms |
23004 KB |
Output is correct |
70 |
Correct |
165 ms |
22348 KB |
Output is correct |