#include <iostream>
#include <vector>
#include "race.h"
using namespace std;
vector<pair<int, int> > g[200001];
int sz[200001];
bool del[200001];
void rcn(int v, int pred) {
sz[v] = 1;
for (auto [to, c] : g[v]) {
if (to == pred || del[to]) continue;
rcn(to, v);
sz[v] += sz[to];
}
}
int centroid(int v, int pred, int N) {
for (auto [to, c] : g[v]) {
if (to == pred || del[to])continue;
if (sz[to] > N / 2)
return centroid(to, v, N);
}
return v;
}
int Kur;
pair<int, int> have[1000001];
int res = 1010101010;
int ID;
void dfs(int v, int pred, int sum, int depth) {
if (have[Kur - sum].first == ID)
res = min(res, have[Kur - sum].second + depth);
for (auto [to, c] : g[v]) {
if (to == pred || del[to] || sum + c > Kur)continue;
dfs(to, v, sum + c, depth + 1);
}
}
void put(int v, int pred, int sum, int depth) {
if (have[sum].first == ID)
have[sum].second = min(have[sum].second, depth);
else {
have[sum].first = ID;
have[sum].second = depth;
}
for (auto [to, c] : g[v]) {
if (to == pred || del[to] || sum + c > Kur)continue;
put(to, v, sum + c, depth + 1);
}
}
void decomposition(int v) {
del[v] = true;
rcn(v, v);
ID = v;
have[0] = {v, 0};
for (auto [to, c] : g[v]) {
if (del[to] || c > Kur)continue;
dfs(to, v, c, 1);
put(to, v, c, 1);
}
for (auto [to, c] : g[v]) {
if (del[to])continue;
decomposition(centroid(to, v, sz[to]));
}
}
int best_path(int N, int K, int H[][2], int L[]) {
for (int i = 0; i < N - 1; i++) {
g[H[i][0] + 1].push_back({H[i][1] + 1, L[i]});
g[H[i][1] + 1].push_back({H[i][0] + 1, L[i]});
}
Kur = K;
rcn(1, 1);
decomposition(centroid(1, 1, N));
// cout << centroid(1, 1, N) << " !!" << endl;
if (res == 1010101010)return -1;
return res;
}
/**
3 3
0 1 1
1 2 1
-1
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2
*/
Compilation message
race.cpp: In function 'void rcn(int, int)':
race.cpp:11:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
11 | for (auto [to, c] : g[v]) {
| ^
race.cpp: In function 'int centroid(int, int, int)':
race.cpp:18:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
18 | for (auto [to, c] : g[v]) {
| ^
race.cpp: In function 'void dfs(int, int, int, int)':
race.cpp:32:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | for (auto [to, c] : g[v]) {
| ^
race.cpp: In function 'void put(int, int, int, int)':
race.cpp:44:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for (auto [to, c] : g[v]) {
| ^
race.cpp: In function 'void decomposition(int)':
race.cpp:55:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for (auto [to, c] : g[v]) {
| ^
race.cpp:60:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | for (auto [to, c] : g[v]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5028 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5036 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
5 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5028 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5036 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
5 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
8396 KB |
Output is correct |
23 |
Correct |
6 ms |
7884 KB |
Output is correct |
24 |
Correct |
7 ms |
9164 KB |
Output is correct |
25 |
Correct |
7 ms |
8140 KB |
Output is correct |
26 |
Correct |
6 ms |
7404 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
6604 KB |
Output is correct |
29 |
Correct |
5 ms |
7500 KB |
Output is correct |
30 |
Correct |
6 ms |
7756 KB |
Output is correct |
31 |
Correct |
7 ms |
8396 KB |
Output is correct |
32 |
Correct |
7 ms |
8396 KB |
Output is correct |
33 |
Correct |
7 ms |
9804 KB |
Output is correct |
34 |
Correct |
7 ms |
9036 KB |
Output is correct |
35 |
Correct |
8 ms |
9932 KB |
Output is correct |
36 |
Correct |
8 ms |
10396 KB |
Output is correct |
37 |
Correct |
7 ms |
8268 KB |
Output is correct |
38 |
Correct |
6 ms |
7188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5028 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5036 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
5 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
144 ms |
10220 KB |
Output is correct |
20 |
Correct |
142 ms |
10216 KB |
Output is correct |
21 |
Correct |
173 ms |
10448 KB |
Output is correct |
22 |
Correct |
146 ms |
10408 KB |
Output is correct |
23 |
Correct |
83 ms |
10576 KB |
Output is correct |
24 |
Correct |
65 ms |
10432 KB |
Output is correct |
25 |
Correct |
139 ms |
13884 KB |
Output is correct |
26 |
Correct |
96 ms |
17576 KB |
Output is correct |
27 |
Correct |
213 ms |
16012 KB |
Output is correct |
28 |
Correct |
410 ms |
30152 KB |
Output is correct |
29 |
Correct |
328 ms |
28996 KB |
Output is correct |
30 |
Correct |
252 ms |
15844 KB |
Output is correct |
31 |
Correct |
190 ms |
15856 KB |
Output is correct |
32 |
Correct |
268 ms |
16020 KB |
Output is correct |
33 |
Correct |
267 ms |
14688 KB |
Output is correct |
34 |
Correct |
242 ms |
14852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5028 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5036 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
5 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
8396 KB |
Output is correct |
23 |
Correct |
6 ms |
7884 KB |
Output is correct |
24 |
Correct |
7 ms |
9164 KB |
Output is correct |
25 |
Correct |
7 ms |
8140 KB |
Output is correct |
26 |
Correct |
6 ms |
7404 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
6604 KB |
Output is correct |
29 |
Correct |
5 ms |
7500 KB |
Output is correct |
30 |
Correct |
6 ms |
7756 KB |
Output is correct |
31 |
Correct |
7 ms |
8396 KB |
Output is correct |
32 |
Correct |
7 ms |
8396 KB |
Output is correct |
33 |
Correct |
7 ms |
9804 KB |
Output is correct |
34 |
Correct |
7 ms |
9036 KB |
Output is correct |
35 |
Correct |
8 ms |
9932 KB |
Output is correct |
36 |
Correct |
8 ms |
10396 KB |
Output is correct |
37 |
Correct |
7 ms |
8268 KB |
Output is correct |
38 |
Correct |
6 ms |
7188 KB |
Output is correct |
39 |
Correct |
144 ms |
10220 KB |
Output is correct |
40 |
Correct |
142 ms |
10216 KB |
Output is correct |
41 |
Correct |
173 ms |
10448 KB |
Output is correct |
42 |
Correct |
146 ms |
10408 KB |
Output is correct |
43 |
Correct |
83 ms |
10576 KB |
Output is correct |
44 |
Correct |
65 ms |
10432 KB |
Output is correct |
45 |
Correct |
139 ms |
13884 KB |
Output is correct |
46 |
Correct |
96 ms |
17576 KB |
Output is correct |
47 |
Correct |
213 ms |
16012 KB |
Output is correct |
48 |
Correct |
410 ms |
30152 KB |
Output is correct |
49 |
Correct |
328 ms |
28996 KB |
Output is correct |
50 |
Correct |
252 ms |
15844 KB |
Output is correct |
51 |
Correct |
190 ms |
15856 KB |
Output is correct |
52 |
Correct |
268 ms |
16020 KB |
Output is correct |
53 |
Correct |
267 ms |
14688 KB |
Output is correct |
54 |
Correct |
242 ms |
14852 KB |
Output is correct |
55 |
Correct |
12 ms |
5592 KB |
Output is correct |
56 |
Correct |
13 ms |
5452 KB |
Output is correct |
57 |
Correct |
105 ms |
10560 KB |
Output is correct |
58 |
Correct |
40 ms |
10528 KB |
Output is correct |
59 |
Correct |
102 ms |
18372 KB |
Output is correct |
60 |
Correct |
493 ms |
34216 KB |
Output is correct |
61 |
Correct |
267 ms |
18948 KB |
Output is correct |
62 |
Correct |
234 ms |
18900 KB |
Output is correct |
63 |
Correct |
345 ms |
18944 KB |
Output is correct |
64 |
Correct |
619 ms |
21308 KB |
Output is correct |
65 |
Correct |
302 ms |
19612 KB |
Output is correct |
66 |
Correct |
475 ms |
37544 KB |
Output is correct |
67 |
Correct |
161 ms |
19460 KB |
Output is correct |
68 |
Correct |
344 ms |
25028 KB |
Output is correct |
69 |
Correct |
332 ms |
25040 KB |
Output is correct |
70 |
Correct |
326 ms |
24268 KB |
Output is correct |