#include "race.h"
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 200200;
int ans = -1, d[N], K;
ll len[N];
vector<pair<int, int>> g[N];
void calc(int u,int p,ll x = 0) {
len[u] = x;
for(auto to : g[u]) if(to.first != p) {
d[to.first] = d[u] + 1;
calc(to.first, u, x + to.second);
}
}
map<ll, int> dfs(int u,int p) {
map<ll, int> own;
own[len[u]] = d[u];
for(auto to : g[u]) if(to.first != p) {
auto x = dfs(to.first, u);
if(x.size() > own.size()) {
own.swap(x);
}
for(auto it : x) {
if(own.count(K - it.first + 2 * len[u])) {
if(ans == -1) {
ans = own[K - it.first + 2 * len[u]] + it.second - 2 * d[u];
} else {
ans = min(ans, own[K - it.first + 2 * len[u]] + it.second - 2 * d[u]);
}
}
if(own.count(it.first)) {
own[it.first] = min(own[it.first], it.second);
} else {
own[it.first] = it.second;
}
}
}
return own;
}
int best_path(int n, int k, int h[][2], int l[]) {
K = k;
for(int i = 0; i < n - 1; i++) {
int u = h[i][0],v = h[i][1], w = l[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
calc(0, -1);
dfs(0, -1);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5048 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5048 KB |
Output is correct |
12 |
Correct |
5 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
5052 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5048 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5048 KB |
Output is correct |
12 |
Correct |
5 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
5052 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
5 ms |
5156 KB |
Output is correct |
26 |
Correct |
5 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5048 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5068 KB |
Output is correct |
30 |
Correct |
5 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5144 KB |
Output is correct |
33 |
Correct |
4 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5196 KB |
Output is correct |
35 |
Correct |
4 ms |
5196 KB |
Output is correct |
36 |
Correct |
4 ms |
5196 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5048 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5048 KB |
Output is correct |
12 |
Correct |
5 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
5052 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
140 ms |
11128 KB |
Output is correct |
20 |
Correct |
131 ms |
11080 KB |
Output is correct |
21 |
Correct |
141 ms |
11000 KB |
Output is correct |
22 |
Correct |
143 ms |
11148 KB |
Output is correct |
23 |
Correct |
155 ms |
11448 KB |
Output is correct |
24 |
Correct |
122 ms |
11284 KB |
Output is correct |
25 |
Correct |
121 ms |
26440 KB |
Output is correct |
26 |
Correct |
77 ms |
40212 KB |
Output is correct |
27 |
Correct |
246 ms |
17584 KB |
Output is correct |
28 |
Correct |
349 ms |
75156 KB |
Output is correct |
29 |
Correct |
352 ms |
72676 KB |
Output is correct |
30 |
Correct |
269 ms |
17612 KB |
Output is correct |
31 |
Correct |
248 ms |
17552 KB |
Output is correct |
32 |
Correct |
346 ms |
17604 KB |
Output is correct |
33 |
Correct |
308 ms |
17208 KB |
Output is correct |
34 |
Correct |
420 ms |
29736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
5048 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5048 KB |
Output is correct |
12 |
Correct |
5 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
5052 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
5 ms |
5156 KB |
Output is correct |
26 |
Correct |
5 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5048 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5068 KB |
Output is correct |
30 |
Correct |
5 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5144 KB |
Output is correct |
33 |
Correct |
4 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5196 KB |
Output is correct |
35 |
Correct |
4 ms |
5196 KB |
Output is correct |
36 |
Correct |
4 ms |
5196 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
140 ms |
11128 KB |
Output is correct |
40 |
Correct |
131 ms |
11080 KB |
Output is correct |
41 |
Correct |
141 ms |
11000 KB |
Output is correct |
42 |
Correct |
143 ms |
11148 KB |
Output is correct |
43 |
Correct |
155 ms |
11448 KB |
Output is correct |
44 |
Correct |
122 ms |
11284 KB |
Output is correct |
45 |
Correct |
121 ms |
26440 KB |
Output is correct |
46 |
Correct |
77 ms |
40212 KB |
Output is correct |
47 |
Correct |
246 ms |
17584 KB |
Output is correct |
48 |
Correct |
349 ms |
75156 KB |
Output is correct |
49 |
Correct |
352 ms |
72676 KB |
Output is correct |
50 |
Correct |
269 ms |
17612 KB |
Output is correct |
51 |
Correct |
248 ms |
17552 KB |
Output is correct |
52 |
Correct |
346 ms |
17604 KB |
Output is correct |
53 |
Correct |
308 ms |
17208 KB |
Output is correct |
54 |
Correct |
420 ms |
29736 KB |
Output is correct |
55 |
Correct |
17 ms |
6076 KB |
Output is correct |
56 |
Correct |
10 ms |
5652 KB |
Output is correct |
57 |
Incorrect |
70 ms |
11236 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |