#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define int int64_t
const int inf = 2e18;
#ifdef LOCAL
#include "algo/debug.h"
#endif
#define f first
#define s second
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-bg(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-bg(a); }
const int max_n = 2e5;
int n, k;
array<int, 2> inp_e[max_n];
int inp_w[max_n];
int ans = inf;
map<int, int> sub[max_n];
V<array<int, 2>> g[max_n];
int W[max_n], L[max_n];
void precomp(int u, int p, int cur_w, int cur_l) {
W[u] = cur_w;
L[u] = cur_l;
sub[u][cur_w] = cur_l;
for (auto [w, v] : g[u]) if (v != p) {
precomp(v, u, cur_w+w, cur_l+1);
}
}
void dfs(int u, int p) {
int target = k + 2*W[u];
for (auto [w, v] : g[u]) if (v != p) {
dfs(v, u);
if (sub[v].size() > sub[u].size()) swap(sub[u], sub[v]);
for (auto [ww, zz] : sub[v]) {
auto found = sub[u].find(target-ww);
if (found != end(sub[u]))
ans = min(ans, zz+found->s - 2*L[u]);
}
for (auto [ww, zz] : sub[v]) {
auto found = sub[u].find(ww);
if (found == end(sub[u])) sub[u].insert({ww, zz});
else sub[u][ww] = min(sub[u][ww], zz);
}
}
}
void read() {
cin >> n >> k;
for (int i = 0; i < n-1; i++) cin >> inp_e[i][0] >> inp_e[i][1];
for (int i = 0; i < n-1; i++) cin >> inp_w[i];
}
void solve() {
for (int i = 0; i < n-1; i++) {
g[inp_e[i][0]].pb({inp_w[i], inp_e[i][1]});
g[inp_e[i][1]].pb({inp_w[i], inp_e[i][0]});
}
precomp(0, -1, 0, 0);
dfs(0, -1);
if (ans == inf) ans = -1;
}
#undef int
int best_path(int N, int K, int INP_E[][2], int INP_W[]) {
// ::n = n;
// ::k = k;
n = N;
k = K;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < 2; j++)
inp_e[i][j] = INP_E[i][j];
inp_w[i] = INP_W[i];
}
read();
solve();
return ans;
}
// signed main() {
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
// read();
// solve();
// cout << ans << '\n';
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
4 ms |
22876 KB |
Output is correct |
5 |
Correct |
5 ms |
22872 KB |
Output is correct |
6 |
Correct |
4 ms |
23004 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22872 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22876 KB |
Output is correct |
12 |
Correct |
4 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
5 ms |
22876 KB |
Output is correct |
15 |
Correct |
5 ms |
22876 KB |
Output is correct |
16 |
Correct |
5 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22960 KB |
Output is correct |
18 |
Correct |
4 ms |
22876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
4 ms |
22876 KB |
Output is correct |
5 |
Correct |
5 ms |
22872 KB |
Output is correct |
6 |
Correct |
4 ms |
23004 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22872 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22876 KB |
Output is correct |
12 |
Correct |
4 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
5 ms |
22876 KB |
Output is correct |
15 |
Correct |
5 ms |
22876 KB |
Output is correct |
16 |
Correct |
5 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22960 KB |
Output is correct |
18 |
Correct |
4 ms |
22876 KB |
Output is correct |
19 |
Correct |
4 ms |
22872 KB |
Output is correct |
20 |
Correct |
4 ms |
22884 KB |
Output is correct |
21 |
Correct |
5 ms |
23132 KB |
Output is correct |
22 |
Correct |
5 ms |
23132 KB |
Output is correct |
23 |
Correct |
6 ms |
23200 KB |
Output is correct |
24 |
Correct |
5 ms |
23132 KB |
Output is correct |
25 |
Correct |
6 ms |
23132 KB |
Output is correct |
26 |
Correct |
5 ms |
23336 KB |
Output is correct |
27 |
Correct |
5 ms |
23132 KB |
Output is correct |
28 |
Correct |
6 ms |
23132 KB |
Output is correct |
29 |
Correct |
5 ms |
23132 KB |
Output is correct |
30 |
Correct |
6 ms |
23128 KB |
Output is correct |
31 |
Correct |
6 ms |
23132 KB |
Output is correct |
32 |
Correct |
5 ms |
23128 KB |
Output is correct |
33 |
Correct |
6 ms |
23216 KB |
Output is correct |
34 |
Correct |
5 ms |
23132 KB |
Output is correct |
35 |
Correct |
5 ms |
23228 KB |
Output is correct |
36 |
Correct |
5 ms |
23132 KB |
Output is correct |
37 |
Correct |
5 ms |
23132 KB |
Output is correct |
38 |
Correct |
5 ms |
23132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
4 ms |
22876 KB |
Output is correct |
5 |
Correct |
5 ms |
22872 KB |
Output is correct |
6 |
Correct |
4 ms |
23004 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22872 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22876 KB |
Output is correct |
12 |
Correct |
4 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
5 ms |
22876 KB |
Output is correct |
15 |
Correct |
5 ms |
22876 KB |
Output is correct |
16 |
Correct |
5 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22960 KB |
Output is correct |
18 |
Correct |
4 ms |
22876 KB |
Output is correct |
19 |
Correct |
121 ms |
47956 KB |
Output is correct |
20 |
Correct |
114 ms |
48980 KB |
Output is correct |
21 |
Correct |
116 ms |
48288 KB |
Output is correct |
22 |
Correct |
106 ms |
48464 KB |
Output is correct |
23 |
Correct |
145 ms |
61464 KB |
Output is correct |
24 |
Correct |
104 ms |
51456 KB |
Output is correct |
25 |
Correct |
102 ms |
49128 KB |
Output is correct |
26 |
Correct |
56 ms |
56092 KB |
Output is correct |
27 |
Correct |
167 ms |
59396 KB |
Output is correct |
28 |
Correct |
264 ms |
98916 KB |
Output is correct |
29 |
Correct |
261 ms |
97380 KB |
Output is correct |
30 |
Correct |
164 ms |
60036 KB |
Output is correct |
31 |
Correct |
185 ms |
60168 KB |
Output is correct |
32 |
Correct |
226 ms |
60048 KB |
Output is correct |
33 |
Correct |
225 ms |
64996 KB |
Output is correct |
34 |
Correct |
313 ms |
97336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
4 ms |
22876 KB |
Output is correct |
5 |
Correct |
5 ms |
22872 KB |
Output is correct |
6 |
Correct |
4 ms |
23004 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22872 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22876 KB |
Output is correct |
12 |
Correct |
4 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
5 ms |
22876 KB |
Output is correct |
15 |
Correct |
5 ms |
22876 KB |
Output is correct |
16 |
Correct |
5 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22960 KB |
Output is correct |
18 |
Correct |
4 ms |
22876 KB |
Output is correct |
19 |
Correct |
4 ms |
22872 KB |
Output is correct |
20 |
Correct |
4 ms |
22884 KB |
Output is correct |
21 |
Correct |
5 ms |
23132 KB |
Output is correct |
22 |
Correct |
5 ms |
23132 KB |
Output is correct |
23 |
Correct |
6 ms |
23200 KB |
Output is correct |
24 |
Correct |
5 ms |
23132 KB |
Output is correct |
25 |
Correct |
6 ms |
23132 KB |
Output is correct |
26 |
Correct |
5 ms |
23336 KB |
Output is correct |
27 |
Correct |
5 ms |
23132 KB |
Output is correct |
28 |
Correct |
6 ms |
23132 KB |
Output is correct |
29 |
Correct |
5 ms |
23132 KB |
Output is correct |
30 |
Correct |
6 ms |
23128 KB |
Output is correct |
31 |
Correct |
6 ms |
23132 KB |
Output is correct |
32 |
Correct |
5 ms |
23128 KB |
Output is correct |
33 |
Correct |
6 ms |
23216 KB |
Output is correct |
34 |
Correct |
5 ms |
23132 KB |
Output is correct |
35 |
Correct |
5 ms |
23228 KB |
Output is correct |
36 |
Correct |
5 ms |
23132 KB |
Output is correct |
37 |
Correct |
5 ms |
23132 KB |
Output is correct |
38 |
Correct |
5 ms |
23132 KB |
Output is correct |
39 |
Correct |
121 ms |
47956 KB |
Output is correct |
40 |
Correct |
114 ms |
48980 KB |
Output is correct |
41 |
Correct |
116 ms |
48288 KB |
Output is correct |
42 |
Correct |
106 ms |
48464 KB |
Output is correct |
43 |
Correct |
145 ms |
61464 KB |
Output is correct |
44 |
Correct |
104 ms |
51456 KB |
Output is correct |
45 |
Correct |
102 ms |
49128 KB |
Output is correct |
46 |
Correct |
56 ms |
56092 KB |
Output is correct |
47 |
Correct |
167 ms |
59396 KB |
Output is correct |
48 |
Correct |
264 ms |
98916 KB |
Output is correct |
49 |
Correct |
261 ms |
97380 KB |
Output is correct |
50 |
Correct |
164 ms |
60036 KB |
Output is correct |
51 |
Correct |
185 ms |
60168 KB |
Output is correct |
52 |
Correct |
226 ms |
60048 KB |
Output is correct |
53 |
Correct |
225 ms |
64996 KB |
Output is correct |
54 |
Correct |
313 ms |
97336 KB |
Output is correct |
55 |
Correct |
16 ms |
27992 KB |
Output is correct |
56 |
Correct |
11 ms |
26716 KB |
Output is correct |
57 |
Correct |
62 ms |
45652 KB |
Output is correct |
58 |
Correct |
45 ms |
39360 KB |
Output is correct |
59 |
Correct |
81 ms |
62352 KB |
Output is correct |
60 |
Correct |
248 ms |
97876 KB |
Output is correct |
61 |
Correct |
193 ms |
63028 KB |
Output is correct |
62 |
Correct |
175 ms |
59988 KB |
Output is correct |
63 |
Correct |
254 ms |
59960 KB |
Output is correct |
64 |
Correct |
446 ms |
113348 KB |
Output is correct |
65 |
Correct |
397 ms |
113264 KB |
Output is correct |
66 |
Correct |
279 ms |
91608 KB |
Output is correct |
67 |
Correct |
162 ms |
53820 KB |
Output is correct |
68 |
Correct |
309 ms |
76224 KB |
Output is correct |
69 |
Correct |
332 ms |
78676 KB |
Output is correct |
70 |
Correct |
287 ms |
74152 KB |
Output is correct |