#include <bits/stdc++.h>
#include "race.h"
#define int64 long long
using namespace std;
const int maxn = 2e5 + 2;
vector <pair <int, int> > ad[maxn];
map <int, int> mp [maxn];
int rs = 1e9;
void dfs (int u, int p, int k, int d, int h) {
// cerr << u << " " << p << " " << k << " " << d << " " << h << "\n";
mp[u][d] = h;
for (auto [v, c]: ad[u]) {
if (v == p) continue;
dfs(v, u, k, d + c, h + 1);
if (mp[v].size() > mp[u].size()) swap(mp[u], mp[v]);
for (auto [cost, node]: mp[v]) {
int rem = k + 2 * d - cost;
if (mp[u].count(rem)) {
// cout << u << " " << cost << " " << rem << "\n";
rs = min(rs, mp[u][rem] + node - 2 * h);
}
}
for (auto [cost, node]: mp[v]) {
if (!mp[u].count(cost)) mp[u][cost] = node;
mp[u][cost] = min(mp[u][cost], node);
}
}
// if (mp[u].count(k + d)) rs = min(rs, mp[u][k + d] - h);
}
int best_path(int n, int k, int h[][2], int l[]){
for (int i = 0; i < n - 1; i++){
ad[h[i][0]].push_back({h[i][1], l[i]});
ad[h[i][1]].push_back({h[i][0], l[i]});
}
dfs(0, -1, k, 0, 0);
return rs == (int) 1e9 ? -1 : rs;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18012 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18012 KB |
Output is correct |
5 |
Correct |
4 ms |
18012 KB |
Output is correct |
6 |
Correct |
4 ms |
18012 KB |
Output is correct |
7 |
Correct |
4 ms |
18012 KB |
Output is correct |
8 |
Correct |
4 ms |
18012 KB |
Output is correct |
9 |
Correct |
5 ms |
18012 KB |
Output is correct |
10 |
Correct |
4 ms |
18008 KB |
Output is correct |
11 |
Correct |
5 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18008 KB |
Output is correct |
13 |
Correct |
4 ms |
18104 KB |
Output is correct |
14 |
Correct |
4 ms |
18012 KB |
Output is correct |
15 |
Correct |
4 ms |
18012 KB |
Output is correct |
16 |
Correct |
4 ms |
18012 KB |
Output is correct |
17 |
Correct |
4 ms |
18012 KB |
Output is correct |
18 |
Correct |
4 ms |
18012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18012 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18012 KB |
Output is correct |
5 |
Correct |
4 ms |
18012 KB |
Output is correct |
6 |
Correct |
4 ms |
18012 KB |
Output is correct |
7 |
Correct |
4 ms |
18012 KB |
Output is correct |
8 |
Correct |
4 ms |
18012 KB |
Output is correct |
9 |
Correct |
5 ms |
18012 KB |
Output is correct |
10 |
Correct |
4 ms |
18008 KB |
Output is correct |
11 |
Correct |
5 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18008 KB |
Output is correct |
13 |
Correct |
4 ms |
18104 KB |
Output is correct |
14 |
Correct |
4 ms |
18012 KB |
Output is correct |
15 |
Correct |
4 ms |
18012 KB |
Output is correct |
16 |
Correct |
4 ms |
18012 KB |
Output is correct |
17 |
Correct |
4 ms |
18012 KB |
Output is correct |
18 |
Correct |
4 ms |
18012 KB |
Output is correct |
19 |
Correct |
4 ms |
18012 KB |
Output is correct |
20 |
Correct |
5 ms |
18012 KB |
Output is correct |
21 |
Correct |
5 ms |
18268 KB |
Output is correct |
22 |
Correct |
5 ms |
18352 KB |
Output is correct |
23 |
Correct |
5 ms |
18264 KB |
Output is correct |
24 |
Correct |
5 ms |
18264 KB |
Output is correct |
25 |
Correct |
6 ms |
18392 KB |
Output is correct |
26 |
Correct |
5 ms |
18264 KB |
Output is correct |
27 |
Correct |
5 ms |
18264 KB |
Output is correct |
28 |
Correct |
5 ms |
18268 KB |
Output is correct |
29 |
Correct |
5 ms |
18268 KB |
Output is correct |
30 |
Correct |
5 ms |
18268 KB |
Output is correct |
31 |
Correct |
5 ms |
18180 KB |
Output is correct |
32 |
Correct |
5 ms |
18364 KB |
Output is correct |
33 |
Correct |
6 ms |
18268 KB |
Output is correct |
34 |
Correct |
5 ms |
18268 KB |
Output is correct |
35 |
Correct |
5 ms |
18464 KB |
Output is correct |
36 |
Correct |
4 ms |
18156 KB |
Output is correct |
37 |
Correct |
4 ms |
18264 KB |
Output is correct |
38 |
Correct |
5 ms |
18268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18012 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18012 KB |
Output is correct |
5 |
Correct |
4 ms |
18012 KB |
Output is correct |
6 |
Correct |
4 ms |
18012 KB |
Output is correct |
7 |
Correct |
4 ms |
18012 KB |
Output is correct |
8 |
Correct |
4 ms |
18012 KB |
Output is correct |
9 |
Correct |
5 ms |
18012 KB |
Output is correct |
10 |
Correct |
4 ms |
18008 KB |
Output is correct |
11 |
Correct |
5 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18008 KB |
Output is correct |
13 |
Correct |
4 ms |
18104 KB |
Output is correct |
14 |
Correct |
4 ms |
18012 KB |
Output is correct |
15 |
Correct |
4 ms |
18012 KB |
Output is correct |
16 |
Correct |
4 ms |
18012 KB |
Output is correct |
17 |
Correct |
4 ms |
18012 KB |
Output is correct |
18 |
Correct |
4 ms |
18012 KB |
Output is correct |
19 |
Correct |
90 ms |
35668 KB |
Output is correct |
20 |
Correct |
92 ms |
36692 KB |
Output is correct |
21 |
Correct |
103 ms |
36144 KB |
Output is correct |
22 |
Correct |
96 ms |
36532 KB |
Output is correct |
23 |
Correct |
137 ms |
46172 KB |
Output is correct |
24 |
Correct |
106 ms |
38996 KB |
Output is correct |
25 |
Correct |
70 ms |
37968 KB |
Output is correct |
26 |
Correct |
47 ms |
44436 KB |
Output is correct |
27 |
Correct |
141 ms |
43860 KB |
Output is correct |
28 |
Correct |
248 ms |
78164 KB |
Output is correct |
29 |
Correct |
231 ms |
76884 KB |
Output is correct |
30 |
Correct |
187 ms |
44432 KB |
Output is correct |
31 |
Correct |
140 ms |
44372 KB |
Output is correct |
32 |
Correct |
211 ms |
44372 KB |
Output is correct |
33 |
Correct |
179 ms |
47952 KB |
Output is correct |
34 |
Correct |
320 ms |
72496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18012 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18012 KB |
Output is correct |
5 |
Correct |
4 ms |
18012 KB |
Output is correct |
6 |
Correct |
4 ms |
18012 KB |
Output is correct |
7 |
Correct |
4 ms |
18012 KB |
Output is correct |
8 |
Correct |
4 ms |
18012 KB |
Output is correct |
9 |
Correct |
5 ms |
18012 KB |
Output is correct |
10 |
Correct |
4 ms |
18008 KB |
Output is correct |
11 |
Correct |
5 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18008 KB |
Output is correct |
13 |
Correct |
4 ms |
18104 KB |
Output is correct |
14 |
Correct |
4 ms |
18012 KB |
Output is correct |
15 |
Correct |
4 ms |
18012 KB |
Output is correct |
16 |
Correct |
4 ms |
18012 KB |
Output is correct |
17 |
Correct |
4 ms |
18012 KB |
Output is correct |
18 |
Correct |
4 ms |
18012 KB |
Output is correct |
19 |
Correct |
4 ms |
18012 KB |
Output is correct |
20 |
Correct |
5 ms |
18012 KB |
Output is correct |
21 |
Correct |
5 ms |
18268 KB |
Output is correct |
22 |
Correct |
5 ms |
18352 KB |
Output is correct |
23 |
Correct |
5 ms |
18264 KB |
Output is correct |
24 |
Correct |
5 ms |
18264 KB |
Output is correct |
25 |
Correct |
6 ms |
18392 KB |
Output is correct |
26 |
Correct |
5 ms |
18264 KB |
Output is correct |
27 |
Correct |
5 ms |
18264 KB |
Output is correct |
28 |
Correct |
5 ms |
18268 KB |
Output is correct |
29 |
Correct |
5 ms |
18268 KB |
Output is correct |
30 |
Correct |
5 ms |
18268 KB |
Output is correct |
31 |
Correct |
5 ms |
18180 KB |
Output is correct |
32 |
Correct |
5 ms |
18364 KB |
Output is correct |
33 |
Correct |
6 ms |
18268 KB |
Output is correct |
34 |
Correct |
5 ms |
18268 KB |
Output is correct |
35 |
Correct |
5 ms |
18464 KB |
Output is correct |
36 |
Correct |
4 ms |
18156 KB |
Output is correct |
37 |
Correct |
4 ms |
18264 KB |
Output is correct |
38 |
Correct |
5 ms |
18268 KB |
Output is correct |
39 |
Correct |
90 ms |
35668 KB |
Output is correct |
40 |
Correct |
92 ms |
36692 KB |
Output is correct |
41 |
Correct |
103 ms |
36144 KB |
Output is correct |
42 |
Correct |
96 ms |
36532 KB |
Output is correct |
43 |
Correct |
137 ms |
46172 KB |
Output is correct |
44 |
Correct |
106 ms |
38996 KB |
Output is correct |
45 |
Correct |
70 ms |
37968 KB |
Output is correct |
46 |
Correct |
47 ms |
44436 KB |
Output is correct |
47 |
Correct |
141 ms |
43860 KB |
Output is correct |
48 |
Correct |
248 ms |
78164 KB |
Output is correct |
49 |
Correct |
231 ms |
76884 KB |
Output is correct |
50 |
Correct |
187 ms |
44432 KB |
Output is correct |
51 |
Correct |
140 ms |
44372 KB |
Output is correct |
52 |
Correct |
211 ms |
44372 KB |
Output is correct |
53 |
Correct |
179 ms |
47952 KB |
Output is correct |
54 |
Correct |
320 ms |
72496 KB |
Output is correct |
55 |
Correct |
16 ms |
20576 KB |
Output is correct |
56 |
Correct |
9 ms |
19288 KB |
Output is correct |
57 |
Correct |
54 ms |
33876 KB |
Output is correct |
58 |
Correct |
39 ms |
29892 KB |
Output is correct |
59 |
Correct |
82 ms |
49324 KB |
Output is correct |
60 |
Correct |
239 ms |
77392 KB |
Output is correct |
61 |
Correct |
187 ms |
46164 KB |
Output is correct |
62 |
Correct |
138 ms |
44372 KB |
Output is correct |
63 |
Correct |
198 ms |
44372 KB |
Output is correct |
64 |
Correct |
427 ms |
84048 KB |
Output is correct |
65 |
Correct |
416 ms |
84704 KB |
Output is correct |
66 |
Correct |
250 ms |
71464 KB |
Output is correct |
67 |
Correct |
134 ms |
41448 KB |
Output is correct |
68 |
Correct |
299 ms |
56960 KB |
Output is correct |
69 |
Correct |
334 ms |
58440 KB |
Output is correct |
70 |
Correct |
285 ms |
55808 KB |
Output is correct |