#include "race.h"
#include <vector>
#include <map>
using namespace std;
int K;
const int INF = 1e9;
vector<vector<pair<int, int>>> g;
vector<int> sz, vis;
int get_centroid(int root) {
auto sub_size = [&](auto sub_size, int x, int p) -> void {
sz[x] = 1;
for(auto [z, w]: g[x]) {
if(z != p && vis[z] == 0) {
sub_size(sub_size, z, x);
sz[x] += sz[z];
}
}
};
sub_size(sub_size, root, -1);
int n = sz[root];
auto dfs = [&](auto dfs, int x, int p) -> int {
for(auto [z, w]: g[x]) {
if(z != p && vis[z] == 0) {
if(2 * sz[z] > n) return dfs(dfs, z, x);
}
}
return x;
};
return dfs(dfs, root, -1);
}
int get(int c) {
c = get_centroid(c);
int ans = INF;
map<int, int> mp;
mp[0] = 0;
vector<pair<int, int>> collect;
auto dfs = [&](auto dfs, int x, int p, int s, int dep) -> void {
if(s > K) return;
if(mp.count(K - s)) ans = min(ans, mp[K - s] + dep);
collect.emplace_back(s, dep);
for(auto [z, w]: g[x]) {
if(z != p && vis[z] == 0) {
dfs(dfs, z, x, s + w, dep + 1);
}
}
};
for(auto [z, w]: g[c]) {
if(vis[z] == 0) {
collect.clear();
dfs(dfs, z, c, w, 1);
for(auto [s, dep]: collect) {
if(!mp.count(s) || mp[s] > dep) {
mp[s] = dep;
}
}
}
}
vis[c] = 1;
for(auto [z, w]: g[c]) {
if(vis[z] == 0) {
ans = min(ans, get(z));
}
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[]) {
::K = K;
g.resize(N);
sz.resize(N);
vis.resize(N);
for(int i = 0; i < N - 1; ++i) {
int x = H[i][0], y = H[i][1], w = L[i];
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
}
int ans = get(0);
if(ans == INF) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2480 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 |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2480 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 |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2472 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2468 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
2 ms |
2892 KB |
Output is correct |
25 |
Correct |
2 ms |
2648 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
2 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2492 KB |
Output is correct |
31 |
Correct |
2 ms |
2648 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2392 KB |
Output is correct |
35 |
Correct |
1 ms |
2584 KB |
Output is correct |
36 |
Correct |
1 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2480 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 |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2472 KB |
Output is correct |
19 |
Correct |
135 ms |
11000 KB |
Output is correct |
20 |
Correct |
132 ms |
11164 KB |
Output is correct |
21 |
Correct |
137 ms |
11424 KB |
Output is correct |
22 |
Correct |
127 ms |
11728 KB |
Output is correct |
23 |
Correct |
58 ms |
11352 KB |
Output is correct |
24 |
Correct |
53 ms |
11088 KB |
Output is correct |
25 |
Correct |
95 ms |
12704 KB |
Output is correct |
26 |
Correct |
74 ms |
17628 KB |
Output is correct |
27 |
Correct |
150 ms |
22004 KB |
Output is correct |
28 |
Correct |
165 ms |
28756 KB |
Output is correct |
29 |
Correct |
154 ms |
28304 KB |
Output is correct |
30 |
Correct |
131 ms |
21840 KB |
Output is correct |
31 |
Correct |
130 ms |
21908 KB |
Output is correct |
32 |
Correct |
172 ms |
22084 KB |
Output is correct |
33 |
Correct |
179 ms |
20816 KB |
Output is correct |
34 |
Correct |
116 ms |
21680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2480 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 |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2472 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2468 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
2 ms |
2892 KB |
Output is correct |
25 |
Correct |
2 ms |
2648 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
2 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2492 KB |
Output is correct |
31 |
Correct |
2 ms |
2648 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2392 KB |
Output is correct |
35 |
Correct |
1 ms |
2584 KB |
Output is correct |
36 |
Correct |
1 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
135 ms |
11000 KB |
Output is correct |
40 |
Correct |
132 ms |
11164 KB |
Output is correct |
41 |
Correct |
137 ms |
11424 KB |
Output is correct |
42 |
Correct |
127 ms |
11728 KB |
Output is correct |
43 |
Correct |
58 ms |
11352 KB |
Output is correct |
44 |
Correct |
53 ms |
11088 KB |
Output is correct |
45 |
Correct |
95 ms |
12704 KB |
Output is correct |
46 |
Correct |
74 ms |
17628 KB |
Output is correct |
47 |
Correct |
150 ms |
22004 KB |
Output is correct |
48 |
Correct |
165 ms |
28756 KB |
Output is correct |
49 |
Correct |
154 ms |
28304 KB |
Output is correct |
50 |
Correct |
131 ms |
21840 KB |
Output is correct |
51 |
Correct |
130 ms |
21908 KB |
Output is correct |
52 |
Correct |
172 ms |
22084 KB |
Output is correct |
53 |
Correct |
179 ms |
20816 KB |
Output is correct |
54 |
Correct |
116 ms |
21680 KB |
Output is correct |
55 |
Correct |
14 ms |
3676 KB |
Output is correct |
56 |
Correct |
10 ms |
3420 KB |
Output is correct |
57 |
Correct |
110 ms |
11908 KB |
Output is correct |
58 |
Correct |
36 ms |
10952 KB |
Output is correct |
59 |
Correct |
237 ms |
24524 KB |
Output is correct |
60 |
Correct |
636 ms |
52120 KB |
Output is correct |
61 |
Correct |
198 ms |
22416 KB |
Output is correct |
62 |
Correct |
228 ms |
22208 KB |
Output is correct |
63 |
Correct |
225 ms |
22344 KB |
Output is correct |
64 |
Correct |
640 ms |
35056 KB |
Output is correct |
65 |
Correct |
152 ms |
22916 KB |
Output is correct |
66 |
Correct |
419 ms |
28604 KB |
Output is correct |
67 |
Correct |
83 ms |
22724 KB |
Output is correct |
68 |
Correct |
269 ms |
29824 KB |
Output is correct |
69 |
Correct |
267 ms |
29976 KB |
Output is correct |
70 |
Correct |
257 ms |
28680 KB |
Output is correct |