#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
const int MX = 2e5 + 10;
const int INF = 1e9;
int _K, off[MX], ans = INF; ll dep[MX];
map<ll, int>* dp[MX];
vector<pii> adj[MX];
void ae(int u, int v, int w){
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
void chmin(int& a, int b){
if(b < a) a = b;
}
void add(int u, int v, int cdep){ // add dp of v to dp of u
if(dp[u] -> size() < dp[v] -> size()) swap(dp[u], dp[v]), swap(off[u], off[v]);
for(pii x : (*dp[v])){
int len = _K + 2 * cdep - x.fi;
if(dp[u] -> count(len)) chmin(ans, x.se + off[v] + (*dp[u])[len] + off[u]);
}
for(pii x : (*dp[v])){
if(dp[u] -> count(x.fi)) chmin((*dp[u])[x.fi], x.se + off[v] - off[u]);
else (*dp[u])[x.fi] = x.se + off[v] - off[u];
}
}
void dfs(int u, int v){
dp[u] = new map<ll, int>();
(*dp[u])[dep[u]] = 0;
off[u] = 0;
for(pii nxt : adj[u]){
if(nxt.fi == v) continue;
dep[nxt.fi] = dep[u] + nxt.se;
dfs(nxt.fi, u);
off[nxt.fi]++;
add(u, nxt.fi, dep[u]);
}
}
int best_path(int N, int K, int H[][2], int L[]){
_K = K;
for(int i = 0; i < N - 1; i++) ae(H[i][0], H[i][1], L[i]);
dfs(0, -1);
if(ans != INF) return ans;
else return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5016 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5024 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5016 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5024 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
5016 KB |
Output is correct |
20 |
Correct |
3 ms |
5020 KB |
Output is correct |
21 |
Correct |
4 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
5 ms |
5332 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Correct |
4 ms |
5332 KB |
Output is correct |
26 |
Correct |
4 ms |
5332 KB |
Output is correct |
27 |
Correct |
4 ms |
5152 KB |
Output is correct |
28 |
Correct |
4 ms |
5416 KB |
Output is correct |
29 |
Correct |
5 ms |
5332 KB |
Output is correct |
30 |
Correct |
4 ms |
5416 KB |
Output is correct |
31 |
Correct |
4 ms |
5332 KB |
Output is correct |
32 |
Correct |
4 ms |
5332 KB |
Output is correct |
33 |
Correct |
5 ms |
5332 KB |
Output is correct |
34 |
Correct |
5 ms |
5292 KB |
Output is correct |
35 |
Correct |
4 ms |
5332 KB |
Output is correct |
36 |
Correct |
4 ms |
5204 KB |
Output is correct |
37 |
Correct |
4 ms |
5204 KB |
Output is correct |
38 |
Correct |
3 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5016 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5024 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5020 KB |
Output is correct |
19 |
Correct |
109 ms |
34944 KB |
Output is correct |
20 |
Correct |
112 ms |
35144 KB |
Output is correct |
21 |
Correct |
159 ms |
34696 KB |
Output is correct |
22 |
Correct |
106 ms |
34296 KB |
Output is correct |
23 |
Correct |
136 ms |
47124 KB |
Output is correct |
24 |
Correct |
135 ms |
37772 KB |
Output is correct |
25 |
Correct |
161 ms |
30672 KB |
Output is correct |
26 |
Correct |
58 ms |
34528 KB |
Output is correct |
27 |
Correct |
200 ms |
53148 KB |
Output is correct |
28 |
Correct |
328 ms |
76628 KB |
Output is correct |
29 |
Correct |
279 ms |
76072 KB |
Output is correct |
30 |
Correct |
182 ms |
53148 KB |
Output is correct |
31 |
Correct |
198 ms |
53220 KB |
Output is correct |
32 |
Correct |
265 ms |
53180 KB |
Output is correct |
33 |
Correct |
256 ms |
57956 KB |
Output is correct |
34 |
Correct |
335 ms |
89824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5016 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5024 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
5016 KB |
Output is correct |
20 |
Correct |
3 ms |
5020 KB |
Output is correct |
21 |
Correct |
4 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
5 ms |
5332 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Correct |
4 ms |
5332 KB |
Output is correct |
26 |
Correct |
4 ms |
5332 KB |
Output is correct |
27 |
Correct |
4 ms |
5152 KB |
Output is correct |
28 |
Correct |
4 ms |
5416 KB |
Output is correct |
29 |
Correct |
5 ms |
5332 KB |
Output is correct |
30 |
Correct |
4 ms |
5416 KB |
Output is correct |
31 |
Correct |
4 ms |
5332 KB |
Output is correct |
32 |
Correct |
4 ms |
5332 KB |
Output is correct |
33 |
Correct |
5 ms |
5332 KB |
Output is correct |
34 |
Correct |
5 ms |
5292 KB |
Output is correct |
35 |
Correct |
4 ms |
5332 KB |
Output is correct |
36 |
Correct |
4 ms |
5204 KB |
Output is correct |
37 |
Correct |
4 ms |
5204 KB |
Output is correct |
38 |
Correct |
3 ms |
5332 KB |
Output is correct |
39 |
Correct |
109 ms |
34944 KB |
Output is correct |
40 |
Correct |
112 ms |
35144 KB |
Output is correct |
41 |
Correct |
159 ms |
34696 KB |
Output is correct |
42 |
Correct |
106 ms |
34296 KB |
Output is correct |
43 |
Correct |
136 ms |
47124 KB |
Output is correct |
44 |
Correct |
135 ms |
37772 KB |
Output is correct |
45 |
Correct |
161 ms |
30672 KB |
Output is correct |
46 |
Correct |
58 ms |
34528 KB |
Output is correct |
47 |
Correct |
200 ms |
53148 KB |
Output is correct |
48 |
Correct |
328 ms |
76628 KB |
Output is correct |
49 |
Correct |
279 ms |
76072 KB |
Output is correct |
50 |
Correct |
182 ms |
53148 KB |
Output is correct |
51 |
Correct |
198 ms |
53220 KB |
Output is correct |
52 |
Correct |
265 ms |
53180 KB |
Output is correct |
53 |
Correct |
256 ms |
57956 KB |
Output is correct |
54 |
Correct |
335 ms |
89824 KB |
Output is correct |
55 |
Correct |
20 ms |
8908 KB |
Output is correct |
56 |
Correct |
10 ms |
7636 KB |
Output is correct |
57 |
Correct |
69 ms |
32388 KB |
Output is correct |
58 |
Correct |
49 ms |
25572 KB |
Output is correct |
59 |
Correct |
83 ms |
40784 KB |
Output is correct |
60 |
Correct |
263 ms |
76296 KB |
Output is correct |
61 |
Correct |
223 ms |
56540 KB |
Output is correct |
62 |
Correct |
180 ms |
53116 KB |
Output is correct |
63 |
Correct |
255 ms |
53304 KB |
Output is correct |
64 |
Correct |
402 ms |
108036 KB |
Output is correct |
65 |
Correct |
441 ms |
105724 KB |
Output is correct |
66 |
Correct |
296 ms |
75132 KB |
Output is correct |
67 |
Correct |
190 ms |
47248 KB |
Output is correct |
68 |
Correct |
367 ms |
69324 KB |
Output is correct |
69 |
Correct |
358 ms |
74524 KB |
Output is correct |
70 |
Correct |
318 ms |
66644 KB |
Output is correct |