#include "race.h"
#include<bits/stdc++.h>
using namespace::std;
struct CentroidDecomposition {
int n;
int L;
int T;
vector<int> a;
vector<int> h;
vector<int> in;
vector<int> sz;
vector<int> out;
vector<int> best;
vector<int> depth;
vector<bool> removed;
vector<vector<pair<int, int>>> G;
CentroidDecomposition(int n, int k) :
n(n),
L(k),
a(n),
h(n),
in(n),
sz(n),
out(n),
best(k + 1, -1),
depth(n),
removed(n, false),
G(n)
{}
void add_edge(int u, int v, int w) {
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
void DFS(int u, int p = -1) {
sz[u] = 1;
for(auto e : G[u]) {
if(e.first == p or removed[e.first]) continue;
DFS(e.first, u);
sz[u] += sz[e.first];
}
}
int centroid(int u, int p, int n) {
for(auto e : G[u]) {
if(e.first == p or removed[e.first]) continue;
if(2 * sz[e.first] > n) return centroid(e.first, u, n);
}
return u;
}
void set_info(int u, int p = -1) {
a[T] = u;
in[u] = T++;
for(auto e : G[u]) {
int v, w;
tie(v, w) = e;
if(v == p or removed[v]) continue;
depth[v] = depth[u] + 1;
h[v] = h[u] + w;
set_info(v, u);
}
out[u] = T - 1;
}
int compute(int u) {
h[u] = depth[u] = T = 0;
set_info(u);
int res = -1;
best[0] = 0;
for(auto e : G[u]) {
if(removed[e.first]) continue;
int v, w;
tie(v, w) = e;
for(int i = in[v]; i <= out[v]; i++) {
int x = a[i];
if(h[x] > L) continue;
if(~best[L - h[x]]) {
int cand = best[L - h[x]] + depth[x];
if(res == -1 or cand < res) res = cand;
}
}
for(int i = in[v]; i <= out[v]; i++) {
int x = a[i];
if(h[x] > L) continue;
if(best[h[x]] == -1 or best[h[x]] > depth[x]) best[h[x]] = depth[x];
}
}
for(int i = in[u]; i <= out[u]; i++) {
int x = a[i];
if(h[x] > L) continue;
best[h[x]] = -1;
}
return res;
}
int decompose(int rt, int p = -1) {
DFS(rt, p);
int c = centroid(rt, p, sz[rt]);
int res = compute(c);
removed[c] = true;
for(auto e : G[c]) {
if(removed[e.first]) continue;
int cur = decompose(e.first, c);
if(cur == -1) continue;
if(res == -1 or res > cur) res = cur;
}
return res;
}
int solve() {
return decompose(0);
}
};
int best_path(int N, int K, int H[][2], int L[]) {
CentroidDecomposition Solver(N, K);
for(int i = 0; i < N - 1; i++) {
Solver.add_edge(H[i][0], H[i][1], L[i]);
}
return Solver.solve();
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2488 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2488 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 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 |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2488 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2488 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
1 ms |
5724 KB |
Output is correct |
26 |
Correct |
1 ms |
3676 KB |
Output is correct |
27 |
Correct |
2 ms |
5724 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
1 ms |
3676 KB |
Output is correct |
30 |
Correct |
1 ms |
3784 KB |
Output is correct |
31 |
Correct |
1 ms |
4956 KB |
Output is correct |
32 |
Correct |
2 ms |
5260 KB |
Output is correct |
33 |
Correct |
1 ms |
5468 KB |
Output is correct |
34 |
Correct |
1 ms |
4700 KB |
Output is correct |
35 |
Correct |
2 ms |
5724 KB |
Output is correct |
36 |
Correct |
1 ms |
6236 KB |
Output is correct |
37 |
Correct |
1 ms |
5468 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 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 |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2488 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2488 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
79 ms |
14428 KB |
Output is correct |
20 |
Correct |
79 ms |
14428 KB |
Output is correct |
21 |
Correct |
76 ms |
14408 KB |
Output is correct |
22 |
Correct |
65 ms |
14540 KB |
Output is correct |
23 |
Correct |
57 ms |
14684 KB |
Output is correct |
24 |
Correct |
38 ms |
14552 KB |
Output is correct |
25 |
Correct |
86 ms |
16000 KB |
Output is correct |
26 |
Correct |
53 ms |
17868 KB |
Output is correct |
27 |
Correct |
85 ms |
24524 KB |
Output is correct |
28 |
Correct |
186 ms |
31312 KB |
Output is correct |
29 |
Correct |
179 ms |
30804 KB |
Output is correct |
30 |
Correct |
93 ms |
24400 KB |
Output is correct |
31 |
Correct |
85 ms |
24540 KB |
Output is correct |
32 |
Correct |
109 ms |
24492 KB |
Output is correct |
33 |
Correct |
136 ms |
23120 KB |
Output is correct |
34 |
Correct |
136 ms |
24216 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 |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2488 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2488 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
1 ms |
5724 KB |
Output is correct |
26 |
Correct |
1 ms |
3676 KB |
Output is correct |
27 |
Correct |
2 ms |
5724 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
1 ms |
3676 KB |
Output is correct |
30 |
Correct |
1 ms |
3784 KB |
Output is correct |
31 |
Correct |
1 ms |
4956 KB |
Output is correct |
32 |
Correct |
2 ms |
5260 KB |
Output is correct |
33 |
Correct |
1 ms |
5468 KB |
Output is correct |
34 |
Correct |
1 ms |
4700 KB |
Output is correct |
35 |
Correct |
2 ms |
5724 KB |
Output is correct |
36 |
Correct |
1 ms |
6236 KB |
Output is correct |
37 |
Correct |
1 ms |
5468 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
79 ms |
14428 KB |
Output is correct |
40 |
Correct |
79 ms |
14428 KB |
Output is correct |
41 |
Correct |
76 ms |
14408 KB |
Output is correct |
42 |
Correct |
65 ms |
14540 KB |
Output is correct |
43 |
Correct |
57 ms |
14684 KB |
Output is correct |
44 |
Correct |
38 ms |
14552 KB |
Output is correct |
45 |
Correct |
86 ms |
16000 KB |
Output is correct |
46 |
Correct |
53 ms |
17868 KB |
Output is correct |
47 |
Correct |
85 ms |
24524 KB |
Output is correct |
48 |
Correct |
186 ms |
31312 KB |
Output is correct |
49 |
Correct |
179 ms |
30804 KB |
Output is correct |
50 |
Correct |
93 ms |
24400 KB |
Output is correct |
51 |
Correct |
85 ms |
24540 KB |
Output is correct |
52 |
Correct |
109 ms |
24492 KB |
Output is correct |
53 |
Correct |
136 ms |
23120 KB |
Output is correct |
54 |
Correct |
136 ms |
24216 KB |
Output is correct |
55 |
Correct |
6 ms |
3416 KB |
Output is correct |
56 |
Correct |
6 ms |
3280 KB |
Output is correct |
57 |
Correct |
44 ms |
14684 KB |
Output is correct |
58 |
Correct |
19 ms |
14288 KB |
Output is correct |
59 |
Correct |
52 ms |
18512 KB |
Output is correct |
60 |
Correct |
194 ms |
34804 KB |
Output is correct |
61 |
Correct |
100 ms |
24656 KB |
Output is correct |
62 |
Correct |
88 ms |
28492 KB |
Output is correct |
63 |
Correct |
117 ms |
28504 KB |
Output is correct |
64 |
Correct |
179 ms |
26712 KB |
Output is correct |
65 |
Correct |
152 ms |
25684 KB |
Output is correct |
66 |
Correct |
204 ms |
33612 KB |
Output is correct |
67 |
Correct |
59 ms |
28992 KB |
Output is correct |
68 |
Correct |
128 ms |
29068 KB |
Output is correct |
69 |
Correct |
125 ms |
29372 KB |
Output is correct |
70 |
Correct |
109 ms |
28288 KB |
Output is correct |