#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 2e5+4;
vector <bool> vis;
vector <map <ll, ll>> f;
vector <ll> dep_points, dep_len;
ll sol = maxn;
void dfs_dep(ll c, vector <pair<ll, ll>> g[]){
vis[c] = 1;
for (auto x : g[c]){
if (vis[x.first])continue;
dep_points[x.first] = dep_points[c]+1;
dep_len[x.first] = dep_len[c]+x.second;
dfs_dep(x.first, g);
}
vis[c] = 0;
}
void compare_merge(map <ll, ll> &x, map <ll, ll> &y, ll add, ll c, ll k){
if (x.size() > y.size())swap(x, y);
for (auto it = x.begin(); it != x.end(); it++){
if (k+add-(it->first) < 0)break;
if (!y.count(k+add-(it->first)))continue;
ll v = (it->first);
sol = min(sol, x[v]+y[k+add-v]-2*dep_points[c]);
}
for (auto it = x.begin(); it != x.end(); it++){
if (!y.count(it->first))y[it->first] = (it->second);
else y[it->first] = min(y[it->first], it->second);
}
x.clear();
}
void dfs_solve(ll c, vector <pair<ll, ll>> g[], ll k){
vis[c] = 1;
for (auto x : g[c]){
if (vis[x.first])continue;
dfs_solve(x.first, g, k);
compare_merge(f[x.first], f[c], 2*dep_len[c], c, k);
}
f[c][dep_len[c]] = dep_points[c];
if (f[c].count(k+dep_len[c]))sol = min(sol, f[c][k+dep_len[c]]-dep_points[c]);
}
int best_path(int N, int K, int H[][2], int L[]){
ll n = N, k = K;
vector <pair<ll, ll>> g[n];
vis.resize(n, 0);
for (int i = 0; i < n-1; i++){
g[H[i][0]].push_back({H[i][1], L[i]});
g[H[i][1]].push_back({H[i][0], L[i]});
}
f.resize(n);
dep_len.resize(n, 0);
dep_points.resize(n, 0);
dfs_dep(0, g);
dfs_solve(0, g, k);
if (sol == maxn)return -1;
else return sol;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2488 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2488 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2500 KB |
Output is correct |
22 |
Correct |
1 ms |
2648 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2648 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2648 KB |
Output is correct |
36 |
Correct |
1 ms |
2648 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2488 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
79 ms |
18512 KB |
Output is correct |
20 |
Correct |
77 ms |
19800 KB |
Output is correct |
21 |
Correct |
87 ms |
19836 KB |
Output is correct |
22 |
Correct |
80 ms |
19792 KB |
Output is correct |
23 |
Correct |
106 ms |
20308 KB |
Output is correct |
24 |
Correct |
79 ms |
19512 KB |
Output is correct |
25 |
Correct |
61 ms |
25680 KB |
Output is correct |
26 |
Correct |
46 ms |
30408 KB |
Output is correct |
27 |
Correct |
129 ms |
35664 KB |
Output is correct |
28 |
Correct |
209 ms |
68804 KB |
Output is correct |
29 |
Correct |
198 ms |
67980 KB |
Output is correct |
30 |
Correct |
127 ms |
35756 KB |
Output is correct |
31 |
Correct |
126 ms |
35664 KB |
Output is correct |
32 |
Correct |
182 ms |
35664 KB |
Output is correct |
33 |
Correct |
180 ms |
35032 KB |
Output is correct |
34 |
Correct |
266 ms |
48212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2488 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2500 KB |
Output is correct |
22 |
Correct |
1 ms |
2648 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2648 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2648 KB |
Output is correct |
36 |
Correct |
1 ms |
2648 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
39 |
Correct |
79 ms |
18512 KB |
Output is correct |
40 |
Correct |
77 ms |
19800 KB |
Output is correct |
41 |
Correct |
87 ms |
19836 KB |
Output is correct |
42 |
Correct |
80 ms |
19792 KB |
Output is correct |
43 |
Correct |
106 ms |
20308 KB |
Output is correct |
44 |
Correct |
79 ms |
19512 KB |
Output is correct |
45 |
Correct |
61 ms |
25680 KB |
Output is correct |
46 |
Correct |
46 ms |
30408 KB |
Output is correct |
47 |
Correct |
129 ms |
35664 KB |
Output is correct |
48 |
Correct |
209 ms |
68804 KB |
Output is correct |
49 |
Correct |
198 ms |
67980 KB |
Output is correct |
50 |
Correct |
127 ms |
35756 KB |
Output is correct |
51 |
Correct |
126 ms |
35664 KB |
Output is correct |
52 |
Correct |
182 ms |
35664 KB |
Output is correct |
53 |
Correct |
180 ms |
35032 KB |
Output is correct |
54 |
Correct |
266 ms |
48212 KB |
Output is correct |
55 |
Correct |
11 ms |
4440 KB |
Output is correct |
56 |
Correct |
6 ms |
3932 KB |
Output is correct |
57 |
Correct |
44 ms |
19932 KB |
Output is correct |
58 |
Correct |
45 ms |
19012 KB |
Output is correct |
59 |
Correct |
65 ms |
36468 KB |
Output is correct |
60 |
Correct |
186 ms |
67924 KB |
Output is correct |
61 |
Correct |
165 ms |
37460 KB |
Output is correct |
62 |
Correct |
141 ms |
35712 KB |
Output is correct |
63 |
Correct |
166 ms |
35668 KB |
Output is correct |
64 |
Correct |
350 ms |
44116 KB |
Output is correct |
65 |
Correct |
348 ms |
48716 KB |
Output is correct |
66 |
Correct |
209 ms |
66640 KB |
Output is correct |
67 |
Correct |
119 ms |
34752 KB |
Output is correct |
68 |
Correct |
266 ms |
46164 KB |
Output is correct |
69 |
Correct |
258 ms |
50412 KB |
Output is correct |
70 |
Correct |
236 ms |
44096 KB |
Output is correct |