/*
dsu: (ds[y] - ds[x]) + (ds[z] - ds[x]) = k
ds[z] = k + 2 ds[x] - ds[y];
# edges = (dep[y] - dep[x]) + (dep[z] - dep[x])
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
#define F first
#define S second
const int N = 200010, INF = 0x3f3f3f3f;
int res = INF;
int h[2 * N], e[2 * N], ne[2 * N], w[2 * N], idx;
map<int, int> dep[N];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u, int fa, int dist, int depth, int k) {
dep[u][dist] = depth;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (j == fa) {
continue;
}
dfs(j, u, dist + w[i], depth + 1, k);
if (dep[u].size() < dep[j].size()) {
swap(dep[u], dep[j]);
}
for (auto t : dep[j]) {
int ds = k + 2 * dist - t.F;
if (ds >= 0 && dep[u].count(ds) > 0) {
res = min(res, dep[u][ds] + dep[j][t.F] - 2 * depth);
}
}
for (auto t : dep[j]) {
if (dep[u][t.F]) dep[u][t.F] = min(dep[u][t.F], t.S);
else dep[u][t.F] = t.S;
}
dep[j].clear();
}
}
int best_path(int N, int K, int H[][2], int L[]) {
memset(h, -1, sizeof h);
for (int i = 0; i < N - 1; i++) {
add(H[i][0] + 1, H[i][1] + 1, L[i]);
add(H[i][1] + 1, H[i][0] + 1, L[i]);
}
dfs(1, -1, 0, 1, K);
return (res == INF ? -1 : res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19544 KB |
Output is correct |
2 |
Correct |
3 ms |
19548 KB |
Output is correct |
3 |
Correct |
3 ms |
19548 KB |
Output is correct |
4 |
Correct |
4 ms |
19796 KB |
Output is correct |
5 |
Correct |
5 ms |
19548 KB |
Output is correct |
6 |
Correct |
3 ms |
19676 KB |
Output is correct |
7 |
Correct |
4 ms |
19548 KB |
Output is correct |
8 |
Correct |
4 ms |
19548 KB |
Output is correct |
9 |
Correct |
3 ms |
19548 KB |
Output is correct |
10 |
Correct |
3 ms |
19548 KB |
Output is correct |
11 |
Correct |
4 ms |
19668 KB |
Output is correct |
12 |
Correct |
3 ms |
19544 KB |
Output is correct |
13 |
Correct |
3 ms |
19548 KB |
Output is correct |
14 |
Correct |
4 ms |
19548 KB |
Output is correct |
15 |
Correct |
3 ms |
19676 KB |
Output is correct |
16 |
Correct |
4 ms |
19548 KB |
Output is correct |
17 |
Correct |
3 ms |
19548 KB |
Output is correct |
18 |
Correct |
3 ms |
19668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19544 KB |
Output is correct |
2 |
Correct |
3 ms |
19548 KB |
Output is correct |
3 |
Correct |
3 ms |
19548 KB |
Output is correct |
4 |
Correct |
4 ms |
19796 KB |
Output is correct |
5 |
Correct |
5 ms |
19548 KB |
Output is correct |
6 |
Correct |
3 ms |
19676 KB |
Output is correct |
7 |
Correct |
4 ms |
19548 KB |
Output is correct |
8 |
Correct |
4 ms |
19548 KB |
Output is correct |
9 |
Correct |
3 ms |
19548 KB |
Output is correct |
10 |
Correct |
3 ms |
19548 KB |
Output is correct |
11 |
Correct |
4 ms |
19668 KB |
Output is correct |
12 |
Correct |
3 ms |
19544 KB |
Output is correct |
13 |
Correct |
3 ms |
19548 KB |
Output is correct |
14 |
Correct |
4 ms |
19548 KB |
Output is correct |
15 |
Correct |
3 ms |
19676 KB |
Output is correct |
16 |
Correct |
4 ms |
19548 KB |
Output is correct |
17 |
Correct |
3 ms |
19548 KB |
Output is correct |
18 |
Correct |
3 ms |
19668 KB |
Output is correct |
19 |
Correct |
3 ms |
19548 KB |
Output is correct |
20 |
Correct |
3 ms |
19548 KB |
Output is correct |
21 |
Correct |
4 ms |
19548 KB |
Output is correct |
22 |
Correct |
4 ms |
19804 KB |
Output is correct |
23 |
Correct |
5 ms |
19800 KB |
Output is correct |
24 |
Correct |
4 ms |
19804 KB |
Output is correct |
25 |
Correct |
4 ms |
19804 KB |
Output is correct |
26 |
Correct |
4 ms |
19804 KB |
Output is correct |
27 |
Correct |
4 ms |
19544 KB |
Output is correct |
28 |
Correct |
4 ms |
19804 KB |
Output is correct |
29 |
Correct |
4 ms |
19804 KB |
Output is correct |
30 |
Correct |
4 ms |
19804 KB |
Output is correct |
31 |
Correct |
5 ms |
19804 KB |
Output is correct |
32 |
Correct |
4 ms |
19804 KB |
Output is correct |
33 |
Correct |
4 ms |
19780 KB |
Output is correct |
34 |
Correct |
4 ms |
19804 KB |
Output is correct |
35 |
Correct |
4 ms |
19804 KB |
Output is correct |
36 |
Correct |
4 ms |
19804 KB |
Output is correct |
37 |
Correct |
4 ms |
19804 KB |
Output is correct |
38 |
Correct |
4 ms |
19804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19544 KB |
Output is correct |
2 |
Correct |
3 ms |
19548 KB |
Output is correct |
3 |
Correct |
3 ms |
19548 KB |
Output is correct |
4 |
Correct |
4 ms |
19796 KB |
Output is correct |
5 |
Correct |
5 ms |
19548 KB |
Output is correct |
6 |
Correct |
3 ms |
19676 KB |
Output is correct |
7 |
Correct |
4 ms |
19548 KB |
Output is correct |
8 |
Correct |
4 ms |
19548 KB |
Output is correct |
9 |
Correct |
3 ms |
19548 KB |
Output is correct |
10 |
Correct |
3 ms |
19548 KB |
Output is correct |
11 |
Correct |
4 ms |
19668 KB |
Output is correct |
12 |
Correct |
3 ms |
19544 KB |
Output is correct |
13 |
Correct |
3 ms |
19548 KB |
Output is correct |
14 |
Correct |
4 ms |
19548 KB |
Output is correct |
15 |
Correct |
3 ms |
19676 KB |
Output is correct |
16 |
Correct |
4 ms |
19548 KB |
Output is correct |
17 |
Correct |
3 ms |
19548 KB |
Output is correct |
18 |
Correct |
3 ms |
19668 KB |
Output is correct |
19 |
Correct |
64 ms |
23380 KB |
Output is correct |
20 |
Correct |
68 ms |
23892 KB |
Output is correct |
21 |
Correct |
58 ms |
23132 KB |
Output is correct |
22 |
Correct |
66 ms |
23388 KB |
Output is correct |
23 |
Correct |
89 ms |
23708 KB |
Output is correct |
24 |
Correct |
68 ms |
23472 KB |
Output is correct |
25 |
Correct |
51 ms |
33876 KB |
Output is correct |
26 |
Correct |
43 ms |
43600 KB |
Output is correct |
27 |
Correct |
87 ms |
24912 KB |
Output is correct |
28 |
Correct |
168 ms |
65580 KB |
Output is correct |
29 |
Correct |
166 ms |
63824 KB |
Output is correct |
30 |
Correct |
88 ms |
24924 KB |
Output is correct |
31 |
Correct |
88 ms |
24912 KB |
Output is correct |
32 |
Correct |
96 ms |
25324 KB |
Output is correct |
33 |
Correct |
120 ms |
26208 KB |
Output is correct |
34 |
Correct |
196 ms |
36180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19544 KB |
Output is correct |
2 |
Correct |
3 ms |
19548 KB |
Output is correct |
3 |
Correct |
3 ms |
19548 KB |
Output is correct |
4 |
Correct |
4 ms |
19796 KB |
Output is correct |
5 |
Correct |
5 ms |
19548 KB |
Output is correct |
6 |
Correct |
3 ms |
19676 KB |
Output is correct |
7 |
Correct |
4 ms |
19548 KB |
Output is correct |
8 |
Correct |
4 ms |
19548 KB |
Output is correct |
9 |
Correct |
3 ms |
19548 KB |
Output is correct |
10 |
Correct |
3 ms |
19548 KB |
Output is correct |
11 |
Correct |
4 ms |
19668 KB |
Output is correct |
12 |
Correct |
3 ms |
19544 KB |
Output is correct |
13 |
Correct |
3 ms |
19548 KB |
Output is correct |
14 |
Correct |
4 ms |
19548 KB |
Output is correct |
15 |
Correct |
3 ms |
19676 KB |
Output is correct |
16 |
Correct |
4 ms |
19548 KB |
Output is correct |
17 |
Correct |
3 ms |
19548 KB |
Output is correct |
18 |
Correct |
3 ms |
19668 KB |
Output is correct |
19 |
Correct |
3 ms |
19548 KB |
Output is correct |
20 |
Correct |
3 ms |
19548 KB |
Output is correct |
21 |
Correct |
4 ms |
19548 KB |
Output is correct |
22 |
Correct |
4 ms |
19804 KB |
Output is correct |
23 |
Correct |
5 ms |
19800 KB |
Output is correct |
24 |
Correct |
4 ms |
19804 KB |
Output is correct |
25 |
Correct |
4 ms |
19804 KB |
Output is correct |
26 |
Correct |
4 ms |
19804 KB |
Output is correct |
27 |
Correct |
4 ms |
19544 KB |
Output is correct |
28 |
Correct |
4 ms |
19804 KB |
Output is correct |
29 |
Correct |
4 ms |
19804 KB |
Output is correct |
30 |
Correct |
4 ms |
19804 KB |
Output is correct |
31 |
Correct |
5 ms |
19804 KB |
Output is correct |
32 |
Correct |
4 ms |
19804 KB |
Output is correct |
33 |
Correct |
4 ms |
19780 KB |
Output is correct |
34 |
Correct |
4 ms |
19804 KB |
Output is correct |
35 |
Correct |
4 ms |
19804 KB |
Output is correct |
36 |
Correct |
4 ms |
19804 KB |
Output is correct |
37 |
Correct |
4 ms |
19804 KB |
Output is correct |
38 |
Correct |
4 ms |
19804 KB |
Output is correct |
39 |
Correct |
64 ms |
23380 KB |
Output is correct |
40 |
Correct |
68 ms |
23892 KB |
Output is correct |
41 |
Correct |
58 ms |
23132 KB |
Output is correct |
42 |
Correct |
66 ms |
23388 KB |
Output is correct |
43 |
Correct |
89 ms |
23708 KB |
Output is correct |
44 |
Correct |
68 ms |
23472 KB |
Output is correct |
45 |
Correct |
51 ms |
33876 KB |
Output is correct |
46 |
Correct |
43 ms |
43600 KB |
Output is correct |
47 |
Correct |
87 ms |
24912 KB |
Output is correct |
48 |
Correct |
168 ms |
65580 KB |
Output is correct |
49 |
Correct |
166 ms |
63824 KB |
Output is correct |
50 |
Correct |
88 ms |
24924 KB |
Output is correct |
51 |
Correct |
88 ms |
24912 KB |
Output is correct |
52 |
Correct |
96 ms |
25324 KB |
Output is correct |
53 |
Correct |
120 ms |
26208 KB |
Output is correct |
54 |
Correct |
196 ms |
36180 KB |
Output is correct |
55 |
Correct |
14 ms |
20316 KB |
Output is correct |
56 |
Correct |
7 ms |
19808 KB |
Output is correct |
57 |
Correct |
37 ms |
23300 KB |
Output is correct |
58 |
Correct |
33 ms |
22872 KB |
Output is correct |
59 |
Correct |
58 ms |
43620 KB |
Output is correct |
60 |
Correct |
157 ms |
64340 KB |
Output is correct |
61 |
Correct |
120 ms |
26448 KB |
Output is correct |
62 |
Correct |
86 ms |
25056 KB |
Output is correct |
63 |
Correct |
98 ms |
25308 KB |
Output is correct |
64 |
Correct |
266 ms |
32196 KB |
Output is correct |
65 |
Correct |
294 ms |
36500 KB |
Output is correct |
66 |
Correct |
164 ms |
60316 KB |
Output is correct |
67 |
Correct |
78 ms |
25172 KB |
Output is correct |
68 |
Correct |
183 ms |
36948 KB |
Output is correct |
69 |
Correct |
180 ms |
33880 KB |
Output is correct |
70 |
Correct |
186 ms |
36432 KB |
Output is correct |