#include <bits/stdc++.h>
#include <race.h>
using i64 = long long;
constexpr int maxN = 2E5 + 5;
int n, k;
std::map<i64, int> s[maxN];
std::vector<std::pair<int, int>> adj[maxN];
int h[maxN], d[maxN];
int ans = maxN;
void dfs(int node, int par) {
for(auto [child, w] : adj[node]) {
if(child == par) {
continue;
}
h[child] = h[node] + w;
d[child] = d[node] + 1;
dfs(child, node);
}
return;
}
void s2l(int node, int par) {
i64 target = k + h[node] * 2;
s[node][h[node]] = d[node];
for(auto [child, w] : adj[node]) {
if(child == par) {
continue;
}
s2l(child, node);
if(s[child].size() > s[node].size()) {
std::swap(s[child], s[node]);
}
for(auto [a, b] : s[child]) {
if(s[node].count(target - a)) {
ans = std::min(ans, s[node][target - a] + b - 2 * d[node]);
}
}
for(auto [a, b] : s[child]) {
if(!s[node].count(a)) {
s[node][a] = n;
}
s[node][a] = std::min(s[node][a], b);
}
}
/*
std::cerr << "node: " << node << "\n";
for(auto [a, b] : s[node]) {
std::cerr << a << " " << b << "\n";
}
*/
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
for(int i = 0; i < n - 1; i++) {
//std::cerr << H[i][0] << " " << H[i][1] << " :: " << L[i] << "\n";
adj[H[i][0]].emplace_back(H[i][1], L[i]);
adj[H[i][1]].emplace_back(H[i][0], L[i]);
}
dfs(0, -1);
s2l(0, -1);
if(ans == maxN) {
ans = -1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19544 KB |
Output is correct |
2 |
Correct |
4 ms |
19548 KB |
Output is correct |
3 |
Correct |
5 ms |
19544 KB |
Output is correct |
4 |
Correct |
4 ms |
19548 KB |
Output is correct |
5 |
Correct |
5 ms |
19548 KB |
Output is correct |
6 |
Correct |
5 ms |
19548 KB |
Output is correct |
7 |
Correct |
4 ms |
19548 KB |
Output is correct |
8 |
Correct |
4 ms |
19548 KB |
Output is correct |
9 |
Correct |
4 ms |
19548 KB |
Output is correct |
10 |
Correct |
4 ms |
19548 KB |
Output is correct |
11 |
Correct |
5 ms |
19544 KB |
Output is correct |
12 |
Correct |
4 ms |
19548 KB |
Output is correct |
13 |
Correct |
4 ms |
19724 KB |
Output is correct |
14 |
Correct |
4 ms |
19548 KB |
Output is correct |
15 |
Correct |
4 ms |
19548 KB |
Output is correct |
16 |
Correct |
5 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19548 KB |
Output is correct |
18 |
Correct |
4 ms |
19736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19544 KB |
Output is correct |
2 |
Correct |
4 ms |
19548 KB |
Output is correct |
3 |
Correct |
5 ms |
19544 KB |
Output is correct |
4 |
Correct |
4 ms |
19548 KB |
Output is correct |
5 |
Correct |
5 ms |
19548 KB |
Output is correct |
6 |
Correct |
5 ms |
19548 KB |
Output is correct |
7 |
Correct |
4 ms |
19548 KB |
Output is correct |
8 |
Correct |
4 ms |
19548 KB |
Output is correct |
9 |
Correct |
4 ms |
19548 KB |
Output is correct |
10 |
Correct |
4 ms |
19548 KB |
Output is correct |
11 |
Correct |
5 ms |
19544 KB |
Output is correct |
12 |
Correct |
4 ms |
19548 KB |
Output is correct |
13 |
Correct |
4 ms |
19724 KB |
Output is correct |
14 |
Correct |
4 ms |
19548 KB |
Output is correct |
15 |
Correct |
4 ms |
19548 KB |
Output is correct |
16 |
Correct |
5 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19548 KB |
Output is correct |
18 |
Correct |
4 ms |
19736 KB |
Output is correct |
19 |
Correct |
5 ms |
19548 KB |
Output is correct |
20 |
Correct |
4 ms |
19640 KB |
Output is correct |
21 |
Correct |
5 ms |
19804 KB |
Output is correct |
22 |
Correct |
5 ms |
19804 KB |
Output is correct |
23 |
Correct |
5 ms |
19800 KB |
Output is correct |
24 |
Correct |
6 ms |
19804 KB |
Output is correct |
25 |
Correct |
5 ms |
19804 KB |
Output is correct |
26 |
Correct |
6 ms |
19804 KB |
Output is correct |
27 |
Correct |
6 ms |
19800 KB |
Output is correct |
28 |
Correct |
6 ms |
19800 KB |
Output is correct |
29 |
Correct |
6 ms |
19804 KB |
Output is correct |
30 |
Correct |
6 ms |
19900 KB |
Output is correct |
31 |
Correct |
6 ms |
19804 KB |
Output is correct |
32 |
Correct |
6 ms |
20060 KB |
Output is correct |
33 |
Correct |
5 ms |
20060 KB |
Output is correct |
34 |
Correct |
5 ms |
19804 KB |
Output is correct |
35 |
Correct |
5 ms |
19804 KB |
Output is correct |
36 |
Correct |
5 ms |
19804 KB |
Output is correct |
37 |
Correct |
5 ms |
19804 KB |
Output is correct |
38 |
Correct |
5 ms |
19804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19544 KB |
Output is correct |
2 |
Correct |
4 ms |
19548 KB |
Output is correct |
3 |
Correct |
5 ms |
19544 KB |
Output is correct |
4 |
Correct |
4 ms |
19548 KB |
Output is correct |
5 |
Correct |
5 ms |
19548 KB |
Output is correct |
6 |
Correct |
5 ms |
19548 KB |
Output is correct |
7 |
Correct |
4 ms |
19548 KB |
Output is correct |
8 |
Correct |
4 ms |
19548 KB |
Output is correct |
9 |
Correct |
4 ms |
19548 KB |
Output is correct |
10 |
Correct |
4 ms |
19548 KB |
Output is correct |
11 |
Correct |
5 ms |
19544 KB |
Output is correct |
12 |
Correct |
4 ms |
19548 KB |
Output is correct |
13 |
Correct |
4 ms |
19724 KB |
Output is correct |
14 |
Correct |
4 ms |
19548 KB |
Output is correct |
15 |
Correct |
4 ms |
19548 KB |
Output is correct |
16 |
Correct |
5 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19548 KB |
Output is correct |
18 |
Correct |
4 ms |
19736 KB |
Output is correct |
19 |
Correct |
115 ms |
41160 KB |
Output is correct |
20 |
Correct |
100 ms |
41808 KB |
Output is correct |
21 |
Correct |
112 ms |
41424 KB |
Output is correct |
22 |
Correct |
102 ms |
41524 KB |
Output is correct |
23 |
Correct |
154 ms |
54156 KB |
Output is correct |
24 |
Correct |
110 ms |
44692 KB |
Output is correct |
25 |
Correct |
89 ms |
41796 KB |
Output is correct |
26 |
Correct |
53 ms |
48980 KB |
Output is correct |
27 |
Correct |
146 ms |
49492 KB |
Output is correct |
28 |
Correct |
284 ms |
88568 KB |
Output is correct |
29 |
Correct |
283 ms |
87076 KB |
Output is correct |
30 |
Correct |
150 ms |
49864 KB |
Output is correct |
31 |
Correct |
149 ms |
49912 KB |
Output is correct |
32 |
Correct |
225 ms |
49748 KB |
Output is correct |
33 |
Correct |
201 ms |
54856 KB |
Output is correct |
34 |
Correct |
350 ms |
87248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
19544 KB |
Output is correct |
2 |
Correct |
4 ms |
19548 KB |
Output is correct |
3 |
Correct |
5 ms |
19544 KB |
Output is correct |
4 |
Correct |
4 ms |
19548 KB |
Output is correct |
5 |
Correct |
5 ms |
19548 KB |
Output is correct |
6 |
Correct |
5 ms |
19548 KB |
Output is correct |
7 |
Correct |
4 ms |
19548 KB |
Output is correct |
8 |
Correct |
4 ms |
19548 KB |
Output is correct |
9 |
Correct |
4 ms |
19548 KB |
Output is correct |
10 |
Correct |
4 ms |
19548 KB |
Output is correct |
11 |
Correct |
5 ms |
19544 KB |
Output is correct |
12 |
Correct |
4 ms |
19548 KB |
Output is correct |
13 |
Correct |
4 ms |
19724 KB |
Output is correct |
14 |
Correct |
4 ms |
19548 KB |
Output is correct |
15 |
Correct |
4 ms |
19548 KB |
Output is correct |
16 |
Correct |
5 ms |
19548 KB |
Output is correct |
17 |
Correct |
5 ms |
19548 KB |
Output is correct |
18 |
Correct |
4 ms |
19736 KB |
Output is correct |
19 |
Correct |
5 ms |
19548 KB |
Output is correct |
20 |
Correct |
4 ms |
19640 KB |
Output is correct |
21 |
Correct |
5 ms |
19804 KB |
Output is correct |
22 |
Correct |
5 ms |
19804 KB |
Output is correct |
23 |
Correct |
5 ms |
19800 KB |
Output is correct |
24 |
Correct |
6 ms |
19804 KB |
Output is correct |
25 |
Correct |
5 ms |
19804 KB |
Output is correct |
26 |
Correct |
6 ms |
19804 KB |
Output is correct |
27 |
Correct |
6 ms |
19800 KB |
Output is correct |
28 |
Correct |
6 ms |
19800 KB |
Output is correct |
29 |
Correct |
6 ms |
19804 KB |
Output is correct |
30 |
Correct |
6 ms |
19900 KB |
Output is correct |
31 |
Correct |
6 ms |
19804 KB |
Output is correct |
32 |
Correct |
6 ms |
20060 KB |
Output is correct |
33 |
Correct |
5 ms |
20060 KB |
Output is correct |
34 |
Correct |
5 ms |
19804 KB |
Output is correct |
35 |
Correct |
5 ms |
19804 KB |
Output is correct |
36 |
Correct |
5 ms |
19804 KB |
Output is correct |
37 |
Correct |
5 ms |
19804 KB |
Output is correct |
38 |
Correct |
5 ms |
19804 KB |
Output is correct |
39 |
Correct |
115 ms |
41160 KB |
Output is correct |
40 |
Correct |
100 ms |
41808 KB |
Output is correct |
41 |
Correct |
112 ms |
41424 KB |
Output is correct |
42 |
Correct |
102 ms |
41524 KB |
Output is correct |
43 |
Correct |
154 ms |
54156 KB |
Output is correct |
44 |
Correct |
110 ms |
44692 KB |
Output is correct |
45 |
Correct |
89 ms |
41796 KB |
Output is correct |
46 |
Correct |
53 ms |
48980 KB |
Output is correct |
47 |
Correct |
146 ms |
49492 KB |
Output is correct |
48 |
Correct |
284 ms |
88568 KB |
Output is correct |
49 |
Correct |
283 ms |
87076 KB |
Output is correct |
50 |
Correct |
150 ms |
49864 KB |
Output is correct |
51 |
Correct |
149 ms |
49912 KB |
Output is correct |
52 |
Correct |
225 ms |
49748 KB |
Output is correct |
53 |
Correct |
201 ms |
54856 KB |
Output is correct |
54 |
Correct |
350 ms |
87248 KB |
Output is correct |
55 |
Correct |
18 ms |
22620 KB |
Output is correct |
56 |
Correct |
11 ms |
21340 KB |
Output is correct |
57 |
Correct |
61 ms |
38744 KB |
Output is correct |
58 |
Correct |
43 ms |
32704 KB |
Output is correct |
59 |
Correct |
90 ms |
55460 KB |
Output is correct |
60 |
Correct |
294 ms |
87676 KB |
Output is correct |
61 |
Correct |
220 ms |
52852 KB |
Output is correct |
62 |
Correct |
149 ms |
49700 KB |
Output is correct |
63 |
Correct |
205 ms |
49748 KB |
Output is correct |
64 |
Correct |
489 ms |
103940 KB |
Output is correct |
65 |
Correct |
455 ms |
102740 KB |
Output is correct |
66 |
Correct |
314 ms |
82568 KB |
Output is correct |
67 |
Correct |
144 ms |
44556 KB |
Output is correct |
68 |
Correct |
323 ms |
66484 KB |
Output is correct |
69 |
Correct |
346 ms |
70116 KB |
Output is correct |
70 |
Correct |
313 ms |
64836 KB |
Output is correct |