#include <bits/stdc++.h>
#include "race.h"
using namespace std;
const int MAX_N = 2e5 + 5;
const int MAX_K = 1e6 + 5;
vector<vector<pair<int,int>>> g (MAX_N);
vector<int> sub(MAX_N), dist(MAX_K, 1e9), del(MAX_N);
int ans = 1e9;
int calc_sub(int node, int p) {
sub[node] = 1;
for (auto to : g[node]) {
if (!del[to.first] && to.first != p) {
sub[node] += calc_sub(to.first, node);
}
}
return sub[node];
}
int calc_centroid(int node, int p, int tam) {
for (auto to : g[node]) {
if (!del[to.first] && to.first != p && sub[to.first] > tam / 2) {
return calc_centroid(to.first, node, tam);
}
}
return node;
}
int _K;
void change(int node, int p, int w, int t, int type) {
if (w > _K) return;
if (type == 0) dist[w] = min (dist[w], t);
else dist[w] = 1e9;
for (auto to : g[node]) {
if (!del[to.first] && to.first != p) {
change(to.first, node, w + to.second, t + 1, type);
}
}
}
void solve(int node, int p, int w, int t) {
if (w > _K) return;
ans = min (ans, t + dist[_K - w]);
for (auto to : g[node]) {
if (!del[to.first] && to.first != p) {
solve(to.first, node, w + to.second, t + 1);
}
}
}
void decompose(int node = 0) {
const int centroid = calc_centroid(node, -1, calc_sub(node, -1));
del[centroid] = true;
dist[0] = 0;
for (auto to : g[centroid]) {
if (del[to.first]) continue;
solve(to.first, -1, to.second, 1);
change(to.first, -1, to.second, 1, 0);
}
for (auto to : g[centroid]) {
if (del[to.first]) continue;
change(to.first, -1, to.second, 1, 1);
}
for (auto to : g[centroid]) {
if (del[to.first]) continue;
decompose(to.first);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
_K = K;
for (int i = 0; i < N - 1; i++) {
int st = H[i][0];
int et = H[i][1];
int w = L[i];
g[st].emplace_back(et, w);
g[et].emplace_back(st, w);
}
decompose();
return (ans > 1e8 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10496 KB |
Output is correct |
3 |
Correct |
12 ms |
10624 KB |
Output is correct |
4 |
Correct |
11 ms |
10624 KB |
Output is correct |
5 |
Correct |
10 ms |
10496 KB |
Output is correct |
6 |
Correct |
10 ms |
10496 KB |
Output is correct |
7 |
Correct |
11 ms |
10496 KB |
Output is correct |
8 |
Correct |
12 ms |
10496 KB |
Output is correct |
9 |
Correct |
11 ms |
10496 KB |
Output is correct |
10 |
Correct |
12 ms |
10496 KB |
Output is correct |
11 |
Correct |
12 ms |
10496 KB |
Output is correct |
12 |
Correct |
11 ms |
10496 KB |
Output is correct |
13 |
Correct |
11 ms |
10496 KB |
Output is correct |
14 |
Correct |
10 ms |
10496 KB |
Output is correct |
15 |
Correct |
10 ms |
10496 KB |
Output is correct |
16 |
Correct |
11 ms |
10496 KB |
Output is correct |
17 |
Correct |
11 ms |
10496 KB |
Output is correct |
18 |
Correct |
10 ms |
10496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10496 KB |
Output is correct |
3 |
Correct |
12 ms |
10624 KB |
Output is correct |
4 |
Correct |
11 ms |
10624 KB |
Output is correct |
5 |
Correct |
10 ms |
10496 KB |
Output is correct |
6 |
Correct |
10 ms |
10496 KB |
Output is correct |
7 |
Correct |
11 ms |
10496 KB |
Output is correct |
8 |
Correct |
12 ms |
10496 KB |
Output is correct |
9 |
Correct |
11 ms |
10496 KB |
Output is correct |
10 |
Correct |
12 ms |
10496 KB |
Output is correct |
11 |
Correct |
12 ms |
10496 KB |
Output is correct |
12 |
Correct |
11 ms |
10496 KB |
Output is correct |
13 |
Correct |
11 ms |
10496 KB |
Output is correct |
14 |
Correct |
10 ms |
10496 KB |
Output is correct |
15 |
Correct |
10 ms |
10496 KB |
Output is correct |
16 |
Correct |
11 ms |
10496 KB |
Output is correct |
17 |
Correct |
11 ms |
10496 KB |
Output is correct |
18 |
Correct |
10 ms |
10496 KB |
Output is correct |
19 |
Correct |
12 ms |
10496 KB |
Output is correct |
20 |
Correct |
11 ms |
10496 KB |
Output is correct |
21 |
Correct |
11 ms |
10624 KB |
Output is correct |
22 |
Correct |
11 ms |
10624 KB |
Output is correct |
23 |
Correct |
13 ms |
10624 KB |
Output is correct |
24 |
Correct |
12 ms |
10624 KB |
Output is correct |
25 |
Correct |
12 ms |
10624 KB |
Output is correct |
26 |
Correct |
12 ms |
10624 KB |
Output is correct |
27 |
Correct |
12 ms |
10624 KB |
Output is correct |
28 |
Correct |
13 ms |
10624 KB |
Output is correct |
29 |
Correct |
11 ms |
10624 KB |
Output is correct |
30 |
Correct |
11 ms |
10624 KB |
Output is correct |
31 |
Correct |
11 ms |
10624 KB |
Output is correct |
32 |
Correct |
12 ms |
10624 KB |
Output is correct |
33 |
Correct |
11 ms |
10624 KB |
Output is correct |
34 |
Correct |
11 ms |
10624 KB |
Output is correct |
35 |
Correct |
11 ms |
10624 KB |
Output is correct |
36 |
Correct |
12 ms |
10624 KB |
Output is correct |
37 |
Correct |
13 ms |
10624 KB |
Output is correct |
38 |
Correct |
12 ms |
10624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10496 KB |
Output is correct |
3 |
Correct |
12 ms |
10624 KB |
Output is correct |
4 |
Correct |
11 ms |
10624 KB |
Output is correct |
5 |
Correct |
10 ms |
10496 KB |
Output is correct |
6 |
Correct |
10 ms |
10496 KB |
Output is correct |
7 |
Correct |
11 ms |
10496 KB |
Output is correct |
8 |
Correct |
12 ms |
10496 KB |
Output is correct |
9 |
Correct |
11 ms |
10496 KB |
Output is correct |
10 |
Correct |
12 ms |
10496 KB |
Output is correct |
11 |
Correct |
12 ms |
10496 KB |
Output is correct |
12 |
Correct |
11 ms |
10496 KB |
Output is correct |
13 |
Correct |
11 ms |
10496 KB |
Output is correct |
14 |
Correct |
10 ms |
10496 KB |
Output is correct |
15 |
Correct |
10 ms |
10496 KB |
Output is correct |
16 |
Correct |
11 ms |
10496 KB |
Output is correct |
17 |
Correct |
11 ms |
10496 KB |
Output is correct |
18 |
Correct |
10 ms |
10496 KB |
Output is correct |
19 |
Correct |
182 ms |
15608 KB |
Output is correct |
20 |
Correct |
175 ms |
15480 KB |
Output is correct |
21 |
Correct |
223 ms |
15620 KB |
Output is correct |
22 |
Correct |
203 ms |
15736 KB |
Output is correct |
23 |
Correct |
106 ms |
15780 KB |
Output is correct |
24 |
Correct |
78 ms |
15608 KB |
Output is correct |
25 |
Correct |
166 ms |
18424 KB |
Output is correct |
26 |
Correct |
135 ms |
21368 KB |
Output is correct |
27 |
Correct |
220 ms |
20600 KB |
Output is correct |
28 |
Correct |
330 ms |
31736 KB |
Output is correct |
29 |
Correct |
349 ms |
30944 KB |
Output is correct |
30 |
Correct |
216 ms |
20472 KB |
Output is correct |
31 |
Correct |
226 ms |
20600 KB |
Output is correct |
32 |
Correct |
251 ms |
20600 KB |
Output is correct |
33 |
Correct |
310 ms |
19448 KB |
Output is correct |
34 |
Correct |
259 ms |
19576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10496 KB |
Output is correct |
3 |
Correct |
12 ms |
10624 KB |
Output is correct |
4 |
Correct |
11 ms |
10624 KB |
Output is correct |
5 |
Correct |
10 ms |
10496 KB |
Output is correct |
6 |
Correct |
10 ms |
10496 KB |
Output is correct |
7 |
Correct |
11 ms |
10496 KB |
Output is correct |
8 |
Correct |
12 ms |
10496 KB |
Output is correct |
9 |
Correct |
11 ms |
10496 KB |
Output is correct |
10 |
Correct |
12 ms |
10496 KB |
Output is correct |
11 |
Correct |
12 ms |
10496 KB |
Output is correct |
12 |
Correct |
11 ms |
10496 KB |
Output is correct |
13 |
Correct |
11 ms |
10496 KB |
Output is correct |
14 |
Correct |
10 ms |
10496 KB |
Output is correct |
15 |
Correct |
10 ms |
10496 KB |
Output is correct |
16 |
Correct |
11 ms |
10496 KB |
Output is correct |
17 |
Correct |
11 ms |
10496 KB |
Output is correct |
18 |
Correct |
10 ms |
10496 KB |
Output is correct |
19 |
Correct |
12 ms |
10496 KB |
Output is correct |
20 |
Correct |
11 ms |
10496 KB |
Output is correct |
21 |
Correct |
11 ms |
10624 KB |
Output is correct |
22 |
Correct |
11 ms |
10624 KB |
Output is correct |
23 |
Correct |
13 ms |
10624 KB |
Output is correct |
24 |
Correct |
12 ms |
10624 KB |
Output is correct |
25 |
Correct |
12 ms |
10624 KB |
Output is correct |
26 |
Correct |
12 ms |
10624 KB |
Output is correct |
27 |
Correct |
12 ms |
10624 KB |
Output is correct |
28 |
Correct |
13 ms |
10624 KB |
Output is correct |
29 |
Correct |
11 ms |
10624 KB |
Output is correct |
30 |
Correct |
11 ms |
10624 KB |
Output is correct |
31 |
Correct |
11 ms |
10624 KB |
Output is correct |
32 |
Correct |
12 ms |
10624 KB |
Output is correct |
33 |
Correct |
11 ms |
10624 KB |
Output is correct |
34 |
Correct |
11 ms |
10624 KB |
Output is correct |
35 |
Correct |
11 ms |
10624 KB |
Output is correct |
36 |
Correct |
12 ms |
10624 KB |
Output is correct |
37 |
Correct |
13 ms |
10624 KB |
Output is correct |
38 |
Correct |
12 ms |
10624 KB |
Output is correct |
39 |
Correct |
182 ms |
15608 KB |
Output is correct |
40 |
Correct |
175 ms |
15480 KB |
Output is correct |
41 |
Correct |
223 ms |
15620 KB |
Output is correct |
42 |
Correct |
203 ms |
15736 KB |
Output is correct |
43 |
Correct |
106 ms |
15780 KB |
Output is correct |
44 |
Correct |
78 ms |
15608 KB |
Output is correct |
45 |
Correct |
166 ms |
18424 KB |
Output is correct |
46 |
Correct |
135 ms |
21368 KB |
Output is correct |
47 |
Correct |
220 ms |
20600 KB |
Output is correct |
48 |
Correct |
330 ms |
31736 KB |
Output is correct |
49 |
Correct |
349 ms |
30944 KB |
Output is correct |
50 |
Correct |
216 ms |
20472 KB |
Output is correct |
51 |
Correct |
226 ms |
20600 KB |
Output is correct |
52 |
Correct |
251 ms |
20600 KB |
Output is correct |
53 |
Correct |
310 ms |
19448 KB |
Output is correct |
54 |
Correct |
259 ms |
19576 KB |
Output is correct |
55 |
Correct |
23 ms |
11136 KB |
Output is correct |
56 |
Correct |
24 ms |
11136 KB |
Output is correct |
57 |
Correct |
109 ms |
16888 KB |
Output is correct |
58 |
Correct |
50 ms |
16748 KB |
Output is correct |
59 |
Correct |
126 ms |
22392 KB |
Output is correct |
60 |
Correct |
772 ms |
34016 KB |
Output is correct |
61 |
Correct |
232 ms |
23548 KB |
Output is correct |
62 |
Correct |
266 ms |
23544 KB |
Output is correct |
63 |
Correct |
356 ms |
23416 KB |
Output is correct |
64 |
Correct |
697 ms |
23800 KB |
Output is correct |
65 |
Correct |
310 ms |
24312 KB |
Output is correct |
66 |
Correct |
503 ms |
31864 KB |
Output is correct |
67 |
Correct |
157 ms |
24040 KB |
Output is correct |
68 |
Correct |
386 ms |
24228 KB |
Output is correct |
69 |
Correct |
344 ms |
24312 KB |
Output is correct |
70 |
Correct |
343 ms |
23800 KB |
Output is correct |