#include <iostream>
#include <vector>
#include "race.h"
using namespace std;
const int N = 1e6 + 5, M = 1e6 + 5;
int t, n, m, k, a[N], sz[N], tmp[N], mx, l[N], r[N];
vector<pair<int, int>> v[N];
bool vis[N];
int ans = 1e9;
int get_subtree_sizes(int node, int p = -1) {
sz[node] = 1;
for (auto x:v[node]) if (x.first != p && !vis[x.first]) sz[node] += get_subtree_sizes(x.first, node);
return sz[node];
}
int get_centroid(int node, int size, int p = -1) {
for (auto x:v[node]) {
if (x.first == p || vis[x.first]) continue;
if (sz[x.first] >= size) return get_centroid(x.first, size, node);
}
return node;
}
void get_cnt(int node, int p, int yes, int sum, int level) {
if (sum > k) return;
if (sum == k) ans = min(ans, level);
if (yes == 1) tmp[sum] = min(tmp[sum], level);
else if (yes) tmp[sum] = 1e9;
else ans = min(ans, level + tmp[k - sum]);
for (auto x:v[node]) if (x.first != p && !vis[x.first]) get_cnt(x.first, node, yes, sum + x.second, level + 1);
}
void centroid_decomp(int node = 0) {
get_subtree_sizes(node);
int c = get_centroid(node, sz[node] / 2);
vis[c] = 1;
mx = 0;
for (auto x:v[c]) if (!vis[x.first]) get_cnt(x.first, c, 0, x.second, 1), get_cnt(x.first, c, 1, x.second, 1);
for (auto x:v[c]) if (!vis[x.first]) get_cnt(x.first, c, 2, x.second, 1);
for (auto x:v[c]) if (!vis[x.first]) centroid_decomp(x.first);
}
int best_path(int N, int K, int H[][2], int L[]) {
fill(tmp, tmp + 1000000 + 5, 1e9);
n = N, k = K;
for (int i = 0; i < n-1; i++) {
v[H[i][1]].push_back({H[i][0], L[i]});
v[H[i][0]].push_back({H[i][1], L[i]});
}
centroid_decomp();
return (ans == 1e9 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
27732 KB |
Output is correct |
2 |
Correct |
17 ms |
27684 KB |
Output is correct |
3 |
Correct |
15 ms |
27640 KB |
Output is correct |
4 |
Correct |
15 ms |
27708 KB |
Output is correct |
5 |
Correct |
14 ms |
27732 KB |
Output is correct |
6 |
Correct |
17 ms |
27668 KB |
Output is correct |
7 |
Correct |
15 ms |
27732 KB |
Output is correct |
8 |
Correct |
15 ms |
27648 KB |
Output is correct |
9 |
Correct |
14 ms |
27636 KB |
Output is correct |
10 |
Correct |
15 ms |
27632 KB |
Output is correct |
11 |
Correct |
14 ms |
27640 KB |
Output is correct |
12 |
Correct |
14 ms |
27728 KB |
Output is correct |
13 |
Correct |
14 ms |
27652 KB |
Output is correct |
14 |
Correct |
14 ms |
27636 KB |
Output is correct |
15 |
Correct |
15 ms |
27732 KB |
Output is correct |
16 |
Correct |
15 ms |
27632 KB |
Output is correct |
17 |
Correct |
14 ms |
27732 KB |
Output is correct |
18 |
Correct |
15 ms |
27636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
27732 KB |
Output is correct |
2 |
Correct |
17 ms |
27684 KB |
Output is correct |
3 |
Correct |
15 ms |
27640 KB |
Output is correct |
4 |
Correct |
15 ms |
27708 KB |
Output is correct |
5 |
Correct |
14 ms |
27732 KB |
Output is correct |
6 |
Correct |
17 ms |
27668 KB |
Output is correct |
7 |
Correct |
15 ms |
27732 KB |
Output is correct |
8 |
Correct |
15 ms |
27648 KB |
Output is correct |
9 |
Correct |
14 ms |
27636 KB |
Output is correct |
10 |
Correct |
15 ms |
27632 KB |
Output is correct |
11 |
Correct |
14 ms |
27640 KB |
Output is correct |
12 |
Correct |
14 ms |
27728 KB |
Output is correct |
13 |
Correct |
14 ms |
27652 KB |
Output is correct |
14 |
Correct |
14 ms |
27636 KB |
Output is correct |
15 |
Correct |
15 ms |
27732 KB |
Output is correct |
16 |
Correct |
15 ms |
27632 KB |
Output is correct |
17 |
Correct |
14 ms |
27732 KB |
Output is correct |
18 |
Correct |
15 ms |
27636 KB |
Output is correct |
19 |
Correct |
13 ms |
27640 KB |
Output is correct |
20 |
Correct |
13 ms |
27732 KB |
Output is correct |
21 |
Correct |
14 ms |
27772 KB |
Output is correct |
22 |
Correct |
14 ms |
27732 KB |
Output is correct |
23 |
Correct |
15 ms |
27732 KB |
Output is correct |
24 |
Correct |
14 ms |
27732 KB |
Output is correct |
25 |
Correct |
15 ms |
27732 KB |
Output is correct |
26 |
Correct |
15 ms |
27776 KB |
Output is correct |
27 |
Correct |
15 ms |
27692 KB |
Output is correct |
28 |
Correct |
16 ms |
27732 KB |
Output is correct |
29 |
Correct |
15 ms |
27732 KB |
Output is correct |
30 |
Correct |
15 ms |
27772 KB |
Output is correct |
31 |
Correct |
15 ms |
27684 KB |
Output is correct |
32 |
Correct |
15 ms |
27732 KB |
Output is correct |
33 |
Correct |
16 ms |
27732 KB |
Output is correct |
34 |
Correct |
15 ms |
27760 KB |
Output is correct |
35 |
Correct |
15 ms |
27732 KB |
Output is correct |
36 |
Correct |
16 ms |
27732 KB |
Output is correct |
37 |
Correct |
15 ms |
27796 KB |
Output is correct |
38 |
Correct |
16 ms |
27732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
27732 KB |
Output is correct |
2 |
Correct |
17 ms |
27684 KB |
Output is correct |
3 |
Correct |
15 ms |
27640 KB |
Output is correct |
4 |
Correct |
15 ms |
27708 KB |
Output is correct |
5 |
Correct |
14 ms |
27732 KB |
Output is correct |
6 |
Correct |
17 ms |
27668 KB |
Output is correct |
7 |
Correct |
15 ms |
27732 KB |
Output is correct |
8 |
Correct |
15 ms |
27648 KB |
Output is correct |
9 |
Correct |
14 ms |
27636 KB |
Output is correct |
10 |
Correct |
15 ms |
27632 KB |
Output is correct |
11 |
Correct |
14 ms |
27640 KB |
Output is correct |
12 |
Correct |
14 ms |
27728 KB |
Output is correct |
13 |
Correct |
14 ms |
27652 KB |
Output is correct |
14 |
Correct |
14 ms |
27636 KB |
Output is correct |
15 |
Correct |
15 ms |
27732 KB |
Output is correct |
16 |
Correct |
15 ms |
27632 KB |
Output is correct |
17 |
Correct |
14 ms |
27732 KB |
Output is correct |
18 |
Correct |
15 ms |
27636 KB |
Output is correct |
19 |
Correct |
107 ms |
34384 KB |
Output is correct |
20 |
Correct |
109 ms |
34384 KB |
Output is correct |
21 |
Correct |
112 ms |
34412 KB |
Output is correct |
22 |
Correct |
102 ms |
34384 KB |
Output is correct |
23 |
Correct |
66 ms |
34724 KB |
Output is correct |
24 |
Correct |
54 ms |
34508 KB |
Output is correct |
25 |
Correct |
106 ms |
37036 KB |
Output is correct |
26 |
Correct |
90 ms |
40012 KB |
Output is correct |
27 |
Correct |
138 ms |
40820 KB |
Output is correct |
28 |
Correct |
175 ms |
52064 KB |
Output is correct |
29 |
Correct |
180 ms |
51164 KB |
Output is correct |
30 |
Correct |
123 ms |
40864 KB |
Output is correct |
31 |
Correct |
132 ms |
40852 KB |
Output is correct |
32 |
Correct |
143 ms |
40820 KB |
Output is correct |
33 |
Correct |
161 ms |
39668 KB |
Output is correct |
34 |
Correct |
132 ms |
39620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
27732 KB |
Output is correct |
2 |
Correct |
17 ms |
27684 KB |
Output is correct |
3 |
Correct |
15 ms |
27640 KB |
Output is correct |
4 |
Correct |
15 ms |
27708 KB |
Output is correct |
5 |
Correct |
14 ms |
27732 KB |
Output is correct |
6 |
Correct |
17 ms |
27668 KB |
Output is correct |
7 |
Correct |
15 ms |
27732 KB |
Output is correct |
8 |
Correct |
15 ms |
27648 KB |
Output is correct |
9 |
Correct |
14 ms |
27636 KB |
Output is correct |
10 |
Correct |
15 ms |
27632 KB |
Output is correct |
11 |
Correct |
14 ms |
27640 KB |
Output is correct |
12 |
Correct |
14 ms |
27728 KB |
Output is correct |
13 |
Correct |
14 ms |
27652 KB |
Output is correct |
14 |
Correct |
14 ms |
27636 KB |
Output is correct |
15 |
Correct |
15 ms |
27732 KB |
Output is correct |
16 |
Correct |
15 ms |
27632 KB |
Output is correct |
17 |
Correct |
14 ms |
27732 KB |
Output is correct |
18 |
Correct |
15 ms |
27636 KB |
Output is correct |
19 |
Correct |
13 ms |
27640 KB |
Output is correct |
20 |
Correct |
13 ms |
27732 KB |
Output is correct |
21 |
Correct |
14 ms |
27772 KB |
Output is correct |
22 |
Correct |
14 ms |
27732 KB |
Output is correct |
23 |
Correct |
15 ms |
27732 KB |
Output is correct |
24 |
Correct |
14 ms |
27732 KB |
Output is correct |
25 |
Correct |
15 ms |
27732 KB |
Output is correct |
26 |
Correct |
15 ms |
27776 KB |
Output is correct |
27 |
Correct |
15 ms |
27692 KB |
Output is correct |
28 |
Correct |
16 ms |
27732 KB |
Output is correct |
29 |
Correct |
15 ms |
27732 KB |
Output is correct |
30 |
Correct |
15 ms |
27772 KB |
Output is correct |
31 |
Correct |
15 ms |
27684 KB |
Output is correct |
32 |
Correct |
15 ms |
27732 KB |
Output is correct |
33 |
Correct |
16 ms |
27732 KB |
Output is correct |
34 |
Correct |
15 ms |
27760 KB |
Output is correct |
35 |
Correct |
15 ms |
27732 KB |
Output is correct |
36 |
Correct |
16 ms |
27732 KB |
Output is correct |
37 |
Correct |
15 ms |
27796 KB |
Output is correct |
38 |
Correct |
16 ms |
27732 KB |
Output is correct |
39 |
Correct |
107 ms |
34384 KB |
Output is correct |
40 |
Correct |
109 ms |
34384 KB |
Output is correct |
41 |
Correct |
112 ms |
34412 KB |
Output is correct |
42 |
Correct |
102 ms |
34384 KB |
Output is correct |
43 |
Correct |
66 ms |
34724 KB |
Output is correct |
44 |
Correct |
54 ms |
34508 KB |
Output is correct |
45 |
Correct |
106 ms |
37036 KB |
Output is correct |
46 |
Correct |
90 ms |
40012 KB |
Output is correct |
47 |
Correct |
138 ms |
40820 KB |
Output is correct |
48 |
Correct |
175 ms |
52064 KB |
Output is correct |
49 |
Correct |
180 ms |
51164 KB |
Output is correct |
50 |
Correct |
123 ms |
40864 KB |
Output is correct |
51 |
Correct |
132 ms |
40852 KB |
Output is correct |
52 |
Correct |
143 ms |
40820 KB |
Output is correct |
53 |
Correct |
161 ms |
39668 KB |
Output is correct |
54 |
Correct |
132 ms |
39620 KB |
Output is correct |
55 |
Correct |
20 ms |
28416 KB |
Output is correct |
56 |
Correct |
21 ms |
28260 KB |
Output is correct |
57 |
Correct |
68 ms |
34524 KB |
Output is correct |
58 |
Correct |
40 ms |
34368 KB |
Output is correct |
59 |
Correct |
90 ms |
40052 KB |
Output is correct |
60 |
Correct |
322 ms |
51400 KB |
Output is correct |
61 |
Correct |
136 ms |
40880 KB |
Output is correct |
62 |
Correct |
177 ms |
40784 KB |
Output is correct |
63 |
Correct |
186 ms |
40784 KB |
Output is correct |
64 |
Correct |
347 ms |
40712 KB |
Output is correct |
65 |
Correct |
146 ms |
40792 KB |
Output is correct |
66 |
Correct |
269 ms |
48972 KB |
Output is correct |
67 |
Correct |
85 ms |
41108 KB |
Output is correct |
68 |
Correct |
190 ms |
41048 KB |
Output is correct |
69 |
Correct |
186 ms |
41036 KB |
Output is correct |
70 |
Correct |
168 ms |
40624 KB |
Output is correct |