#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
const int MAXK = 1000010;
const int M = 2 * MAXN;
const int inf = 0x3f3f3f3f;
int hd[MAXN], nx[M], to[M], wt[M], ei = 0;
inline void link(int i, int j, int kk) {
nx[++ei] = hd[i], wt[ei] = kk, hd[i] = ei, to[ei] = j;
}
int n, k, ss[MAXN], dep, cc[MAXK], ans;
bool vv[MAXN];
int subtree(int x, int p = 0) {
ss[x] = 1;
for (int j = hd[x], y; j; j = nx[j])
if (!vv[y = to[j]] && y != p)
ss[x] += subtree(y, x);
return ss[x];
}
int centroid(int dd, int x, int p = 0) {
for (int j = hd[x], y; j; j = nx[j])
if (!vv[y = to[j]] && y != p && ss[y] > dd)
return centroid(dd, y, x);
return x;
}
void _fill(int x, int p, int w, int d) {
if (w > k) return;
cc[w] = min(cc[w], d);
dep = max(dep, w);
for (int j = hd[x], y; j; j = nx[j])
if (!vv[y = to[j]] && y != p)
_fill(y, x, w + wt[j], d + 1);
}
void _count(int x, int p, int w, int d) {
if (w > k) return;
ans = min(ans, d + cc[k - w]);
for (int j = hd[x], y; j; j = nx[j])
if (!vv[y = to[j]] && y != p)
_count(y, x, w + wt[j], d + 1);
}
void decomp(int x = 1) {
int c = centroid(subtree(x) / 2, x);
dep = 0, vv[c] = 1;
for (int j = hd[c], y; j; j = nx[j])
if (!vv[y = to[j]]) {
_count(y, c, wt[j], 1);
_fill(y, c, wt[j], 1);
}
for (int i = 1; i <= dep; ++i) cc[i] = inf;
for (int j = hd[c], y; j; j = nx[j])
if (!vv[y = to[j]])
decomp(y);
}
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K;
memset(vv, 0, sizeof(vv));
memset(hd, 0, sizeof(hd));
ans = inf, ei = 0;
for (int i = 0; i + 1 < n; ++i) {
++H[i][0], ++H[i][1];
link(H[i][0], H[i][1], L[i]);
link(H[i][1], H[i][0], L[i]);
}
memset(cc, 0x3f, sizeof(cc)); cc[0] = 0;
decomp();
return (ans == inf ? -1 : ans);
}
/**
int main() {
scanf("%d%d", &n, &k);
memset(vv, 0, sizeof(vv));
memset(hd, 0, sizeof(hd));
ans = inf, ei = 0;
for (int i = 1, x, y, z; i < n; ++i) {
scanf("%d%d%d", &x, &y, &z); ++x, ++y;
link(x, y, z), link(y, x, z);
}
memset(cc, 0x3f, sizeof(cc)); cc[0] = 0;
decomp();
printf("%d\n", (ans == inf ? -1 : ans));
}
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5228 KB |
Output is correct |
3 |
Correct |
3 ms |
5228 KB |
Output is correct |
4 |
Correct |
3 ms |
5228 KB |
Output is correct |
5 |
Correct |
3 ms |
5228 KB |
Output is correct |
6 |
Correct |
3 ms |
5228 KB |
Output is correct |
7 |
Correct |
3 ms |
5228 KB |
Output is correct |
8 |
Correct |
4 ms |
5228 KB |
Output is correct |
9 |
Correct |
3 ms |
5228 KB |
Output is correct |
10 |
Correct |
3 ms |
5228 KB |
Output is correct |
11 |
Correct |
3 ms |
5228 KB |
Output is correct |
12 |
Correct |
4 ms |
5228 KB |
Output is correct |
13 |
Correct |
5 ms |
5228 KB |
Output is correct |
14 |
Correct |
4 ms |
5228 KB |
Output is correct |
15 |
Correct |
4 ms |
5228 KB |
Output is correct |
16 |
Correct |
3 ms |
5228 KB |
Output is correct |
17 |
Correct |
3 ms |
5228 KB |
Output is correct |
18 |
Correct |
4 ms |
5228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5228 KB |
Output is correct |
3 |
Correct |
3 ms |
5228 KB |
Output is correct |
4 |
Correct |
3 ms |
5228 KB |
Output is correct |
5 |
Correct |
3 ms |
5228 KB |
Output is correct |
6 |
Correct |
3 ms |
5228 KB |
Output is correct |
7 |
Correct |
3 ms |
5228 KB |
Output is correct |
8 |
Correct |
4 ms |
5228 KB |
Output is correct |
9 |
Correct |
3 ms |
5228 KB |
Output is correct |
10 |
Correct |
3 ms |
5228 KB |
Output is correct |
11 |
Correct |
3 ms |
5228 KB |
Output is correct |
12 |
Correct |
4 ms |
5228 KB |
Output is correct |
13 |
Correct |
5 ms |
5228 KB |
Output is correct |
14 |
Correct |
4 ms |
5228 KB |
Output is correct |
15 |
Correct |
4 ms |
5228 KB |
Output is correct |
16 |
Correct |
3 ms |
5228 KB |
Output is correct |
17 |
Correct |
3 ms |
5228 KB |
Output is correct |
18 |
Correct |
4 ms |
5228 KB |
Output is correct |
19 |
Correct |
3 ms |
5228 KB |
Output is correct |
20 |
Correct |
3 ms |
5228 KB |
Output is correct |
21 |
Correct |
4 ms |
5356 KB |
Output is correct |
22 |
Correct |
149 ms |
5356 KB |
Output is correct |
23 |
Correct |
104 ms |
5356 KB |
Output is correct |
24 |
Correct |
129 ms |
5356 KB |
Output is correct |
25 |
Correct |
25 ms |
5356 KB |
Output is correct |
26 |
Correct |
52 ms |
5356 KB |
Output is correct |
27 |
Correct |
4 ms |
5356 KB |
Output is correct |
28 |
Correct |
19 ms |
5356 KB |
Output is correct |
29 |
Correct |
20 ms |
5356 KB |
Output is correct |
30 |
Correct |
21 ms |
5356 KB |
Output is correct |
31 |
Correct |
21 ms |
5356 KB |
Output is correct |
32 |
Correct |
22 ms |
5356 KB |
Output is correct |
33 |
Correct |
27 ms |
5356 KB |
Output is correct |
34 |
Correct |
29 ms |
5356 KB |
Output is correct |
35 |
Correct |
32 ms |
5356 KB |
Output is correct |
36 |
Correct |
35 ms |
5484 KB |
Output is correct |
37 |
Correct |
8 ms |
5356 KB |
Output is correct |
38 |
Correct |
7 ms |
5356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5228 KB |
Output is correct |
3 |
Correct |
3 ms |
5228 KB |
Output is correct |
4 |
Correct |
3 ms |
5228 KB |
Output is correct |
5 |
Correct |
3 ms |
5228 KB |
Output is correct |
6 |
Correct |
3 ms |
5228 KB |
Output is correct |
7 |
Correct |
3 ms |
5228 KB |
Output is correct |
8 |
Correct |
4 ms |
5228 KB |
Output is correct |
9 |
Correct |
3 ms |
5228 KB |
Output is correct |
10 |
Correct |
3 ms |
5228 KB |
Output is correct |
11 |
Correct |
3 ms |
5228 KB |
Output is correct |
12 |
Correct |
4 ms |
5228 KB |
Output is correct |
13 |
Correct |
5 ms |
5228 KB |
Output is correct |
14 |
Correct |
4 ms |
5228 KB |
Output is correct |
15 |
Correct |
4 ms |
5228 KB |
Output is correct |
16 |
Correct |
3 ms |
5228 KB |
Output is correct |
17 |
Correct |
3 ms |
5228 KB |
Output is correct |
18 |
Correct |
4 ms |
5228 KB |
Output is correct |
19 |
Correct |
126 ms |
10568 KB |
Output is correct |
20 |
Correct |
119 ms |
10476 KB |
Output is correct |
21 |
Correct |
132 ms |
10476 KB |
Output is correct |
22 |
Correct |
118 ms |
10548 KB |
Output is correct |
23 |
Correct |
69 ms |
10604 KB |
Output is correct |
24 |
Correct |
50 ms |
10604 KB |
Output is correct |
25 |
Correct |
110 ms |
13932 KB |
Output is correct |
26 |
Correct |
92 ms |
16748 KB |
Output is correct |
27 |
Correct |
140 ms |
15980 KB |
Output is correct |
28 |
Correct |
195 ms |
28652 KB |
Output is correct |
29 |
Correct |
203 ms |
27756 KB |
Output is correct |
30 |
Correct |
143 ms |
15980 KB |
Output is correct |
31 |
Correct |
143 ms |
16004 KB |
Output is correct |
32 |
Correct |
192 ms |
16184 KB |
Output is correct |
33 |
Correct |
168 ms |
16108 KB |
Output is correct |
34 |
Correct |
138 ms |
17004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5228 KB |
Output is correct |
3 |
Correct |
3 ms |
5228 KB |
Output is correct |
4 |
Correct |
3 ms |
5228 KB |
Output is correct |
5 |
Correct |
3 ms |
5228 KB |
Output is correct |
6 |
Correct |
3 ms |
5228 KB |
Output is correct |
7 |
Correct |
3 ms |
5228 KB |
Output is correct |
8 |
Correct |
4 ms |
5228 KB |
Output is correct |
9 |
Correct |
3 ms |
5228 KB |
Output is correct |
10 |
Correct |
3 ms |
5228 KB |
Output is correct |
11 |
Correct |
3 ms |
5228 KB |
Output is correct |
12 |
Correct |
4 ms |
5228 KB |
Output is correct |
13 |
Correct |
5 ms |
5228 KB |
Output is correct |
14 |
Correct |
4 ms |
5228 KB |
Output is correct |
15 |
Correct |
4 ms |
5228 KB |
Output is correct |
16 |
Correct |
3 ms |
5228 KB |
Output is correct |
17 |
Correct |
3 ms |
5228 KB |
Output is correct |
18 |
Correct |
4 ms |
5228 KB |
Output is correct |
19 |
Correct |
3 ms |
5228 KB |
Output is correct |
20 |
Correct |
3 ms |
5228 KB |
Output is correct |
21 |
Correct |
4 ms |
5356 KB |
Output is correct |
22 |
Correct |
149 ms |
5356 KB |
Output is correct |
23 |
Correct |
104 ms |
5356 KB |
Output is correct |
24 |
Correct |
129 ms |
5356 KB |
Output is correct |
25 |
Correct |
25 ms |
5356 KB |
Output is correct |
26 |
Correct |
52 ms |
5356 KB |
Output is correct |
27 |
Correct |
4 ms |
5356 KB |
Output is correct |
28 |
Correct |
19 ms |
5356 KB |
Output is correct |
29 |
Correct |
20 ms |
5356 KB |
Output is correct |
30 |
Correct |
21 ms |
5356 KB |
Output is correct |
31 |
Correct |
21 ms |
5356 KB |
Output is correct |
32 |
Correct |
22 ms |
5356 KB |
Output is correct |
33 |
Correct |
27 ms |
5356 KB |
Output is correct |
34 |
Correct |
29 ms |
5356 KB |
Output is correct |
35 |
Correct |
32 ms |
5356 KB |
Output is correct |
36 |
Correct |
35 ms |
5484 KB |
Output is correct |
37 |
Correct |
8 ms |
5356 KB |
Output is correct |
38 |
Correct |
7 ms |
5356 KB |
Output is correct |
39 |
Correct |
126 ms |
10568 KB |
Output is correct |
40 |
Correct |
119 ms |
10476 KB |
Output is correct |
41 |
Correct |
132 ms |
10476 KB |
Output is correct |
42 |
Correct |
118 ms |
10548 KB |
Output is correct |
43 |
Correct |
69 ms |
10604 KB |
Output is correct |
44 |
Correct |
50 ms |
10604 KB |
Output is correct |
45 |
Correct |
110 ms |
13932 KB |
Output is correct |
46 |
Correct |
92 ms |
16748 KB |
Output is correct |
47 |
Correct |
140 ms |
15980 KB |
Output is correct |
48 |
Correct |
195 ms |
28652 KB |
Output is correct |
49 |
Correct |
203 ms |
27756 KB |
Output is correct |
50 |
Correct |
143 ms |
15980 KB |
Output is correct |
51 |
Correct |
143 ms |
16004 KB |
Output is correct |
52 |
Correct |
192 ms |
16184 KB |
Output is correct |
53 |
Correct |
168 ms |
16108 KB |
Output is correct |
54 |
Correct |
138 ms |
17004 KB |
Output is correct |
55 |
Correct |
14 ms |
5740 KB |
Output is correct |
56 |
Correct |
13 ms |
5740 KB |
Output is correct |
57 |
Correct |
79 ms |
10476 KB |
Output is correct |
58 |
Correct |
36 ms |
10220 KB |
Output is correct |
59 |
Correct |
98 ms |
16876 KB |
Output is correct |
60 |
Correct |
308 ms |
27884 KB |
Output is correct |
61 |
Correct |
174 ms |
16172 KB |
Output is correct |
62 |
Correct |
181 ms |
16108 KB |
Output is correct |
63 |
Correct |
251 ms |
16228 KB |
Output is correct |
64 |
Correct |
1041 ms |
16580 KB |
Output is correct |
65 |
Correct |
162 ms |
16876 KB |
Output is correct |
66 |
Correct |
590 ms |
25836 KB |
Output is correct |
67 |
Correct |
84 ms |
16492 KB |
Output is correct |
68 |
Correct |
404 ms |
16588 KB |
Output is correct |
69 |
Correct |
394 ms |
16860 KB |
Output is correct |
70 |
Correct |
377 ms |
16096 KB |
Output is correct |