#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 5;
const int inf = 1e9;
int n, k, res = inf, sz[MAX], min_length[MAX];
bool deleted[MAX];
vector<int> undo_length;
vector<pair<int, int>> adj[MAX];
stack<pair<int, int>> updates;
int get_size(int u, int pre){
sz[u] = 1;
for(auto [v, w] : adj[u]) if(v != pre and !deleted[v]){
sz[u] += get_size(v, u);
}
return sz[u];
}
int find_centroid(int u, int pre, int target){
for(auto [v, w] : adj[u]) if(v != pre and !deleted[v] and sz[v] * 2 > target){
return find_centroid(v, u, target);
}
return u;
}
void compute(int u, int pre, int len, int sum){
if(sum > k) return;
res = min(res, len + min_length[k - sum]);
updates.push(make_pair(len, sum));
for(auto [v, w] : adj[u]) if(v != pre and !deleted[v]){
compute(v, u, len + 1, sum + w);
}
}
void decompose(int u){
int c = find_centroid(u, -1, get_size(u, -1));
deleted[c] = true;
undo_length.clear();
for(auto [v, w] : adj[c]) if(!deleted[v]){
compute(v, c, 1, w);
while(!updates.empty()){
int len, sum;
tie(len, sum) = updates.top(); updates.pop();
min_length[sum] = min(min_length[sum], len);
undo_length.push_back(sum);
}
}
for(auto v : undo_length) min_length[v] = inf;
for(auto [v, w] : adj[c]) if(!deleted[v]){
decompose(v);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
for(int i = 0; i < n - 1; ++i){
int u = H[i][0], v = H[i][1], w = L[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
fill(min_length + 1, min_length + 1 + k, inf);
decompose(1);
if(res >= inf) res = -1;
return res;
}
//#define Zero_OP
#ifdef Zero_OP
void init(void){
}
void process(){
int H[2][2];
H[0][0] = 0;
H[0][1] = 1;
H[1][0] = 1;
H[1][1] = 2;
int L[2];
L[0] = 1;
L[1] = 1;
cout << best_path(3, 3, H, L) << '\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
init();
process();
return 0;
}
#endif
Compilation message
race.cpp: In function 'int get_size(int, int)':
race.cpp:16:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
16 | for(auto [v, w] : adj[u]) if(v != pre and !deleted[v]){
| ^
race.cpp: In function 'int find_centroid(int, int, int)':
race.cpp:23:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
23 | for(auto [v, w] : adj[u]) if(v != pre and !deleted[v] and sz[v] * 2 > target){
| ^
race.cpp: In function 'void compute(int, int, int, int)':
race.cpp:33:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for(auto [v, w] : adj[u]) if(v != pre and !deleted[v]){
| ^
race.cpp: In function 'void decompose(int)':
race.cpp:43:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
43 | for(auto [v, w] : adj[c]) if(!deleted[v]){
| ^
race.cpp:54:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
54 | for(auto [v, w] : adj[c]) if(!deleted[v]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31068 KB |
Output is correct |
2 |
Correct |
7 ms |
31068 KB |
Output is correct |
3 |
Correct |
7 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
6 ms |
31068 KB |
Output is correct |
6 |
Correct |
6 ms |
31068 KB |
Output is correct |
7 |
Correct |
6 ms |
31204 KB |
Output is correct |
8 |
Correct |
6 ms |
31228 KB |
Output is correct |
9 |
Correct |
6 ms |
31232 KB |
Output is correct |
10 |
Correct |
6 ms |
31068 KB |
Output is correct |
11 |
Correct |
6 ms |
31068 KB |
Output is correct |
12 |
Correct |
6 ms |
31220 KB |
Output is correct |
13 |
Correct |
6 ms |
31068 KB |
Output is correct |
14 |
Correct |
6 ms |
31204 KB |
Output is correct |
15 |
Correct |
6 ms |
31068 KB |
Output is correct |
16 |
Correct |
6 ms |
31068 KB |
Output is correct |
17 |
Correct |
6 ms |
31068 KB |
Output is correct |
18 |
Correct |
6 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31068 KB |
Output is correct |
2 |
Correct |
7 ms |
31068 KB |
Output is correct |
3 |
Correct |
7 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
6 ms |
31068 KB |
Output is correct |
6 |
Correct |
6 ms |
31068 KB |
Output is correct |
7 |
Correct |
6 ms |
31204 KB |
Output is correct |
8 |
Correct |
6 ms |
31228 KB |
Output is correct |
9 |
Correct |
6 ms |
31232 KB |
Output is correct |
10 |
Correct |
6 ms |
31068 KB |
Output is correct |
11 |
Correct |
6 ms |
31068 KB |
Output is correct |
12 |
Correct |
6 ms |
31220 KB |
Output is correct |
13 |
Correct |
6 ms |
31068 KB |
Output is correct |
14 |
Correct |
6 ms |
31204 KB |
Output is correct |
15 |
Correct |
6 ms |
31068 KB |
Output is correct |
16 |
Correct |
6 ms |
31068 KB |
Output is correct |
17 |
Correct |
6 ms |
31068 KB |
Output is correct |
18 |
Correct |
6 ms |
31068 KB |
Output is correct |
19 |
Correct |
6 ms |
31064 KB |
Output is correct |
20 |
Correct |
6 ms |
31228 KB |
Output is correct |
21 |
Correct |
6 ms |
31068 KB |
Output is correct |
22 |
Correct |
7 ms |
33116 KB |
Output is correct |
23 |
Correct |
7 ms |
33116 KB |
Output is correct |
24 |
Correct |
7 ms |
33328 KB |
Output is correct |
25 |
Correct |
7 ms |
33116 KB |
Output is correct |
26 |
Correct |
7 ms |
33116 KB |
Output is correct |
27 |
Correct |
8 ms |
33116 KB |
Output is correct |
28 |
Correct |
7 ms |
33276 KB |
Output is correct |
29 |
Correct |
7 ms |
33348 KB |
Output is correct |
30 |
Correct |
7 ms |
33116 KB |
Output is correct |
31 |
Correct |
8 ms |
33116 KB |
Output is correct |
32 |
Correct |
8 ms |
33112 KB |
Output is correct |
33 |
Correct |
7 ms |
33116 KB |
Output is correct |
34 |
Correct |
7 ms |
33116 KB |
Output is correct |
35 |
Correct |
7 ms |
33272 KB |
Output is correct |
36 |
Correct |
7 ms |
33116 KB |
Output is correct |
37 |
Correct |
7 ms |
33116 KB |
Output is correct |
38 |
Correct |
7 ms |
33344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31068 KB |
Output is correct |
2 |
Correct |
7 ms |
31068 KB |
Output is correct |
3 |
Correct |
7 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
6 ms |
31068 KB |
Output is correct |
6 |
Correct |
6 ms |
31068 KB |
Output is correct |
7 |
Correct |
6 ms |
31204 KB |
Output is correct |
8 |
Correct |
6 ms |
31228 KB |
Output is correct |
9 |
Correct |
6 ms |
31232 KB |
Output is correct |
10 |
Correct |
6 ms |
31068 KB |
Output is correct |
11 |
Correct |
6 ms |
31068 KB |
Output is correct |
12 |
Correct |
6 ms |
31220 KB |
Output is correct |
13 |
Correct |
6 ms |
31068 KB |
Output is correct |
14 |
Correct |
6 ms |
31204 KB |
Output is correct |
15 |
Correct |
6 ms |
31068 KB |
Output is correct |
16 |
Correct |
6 ms |
31068 KB |
Output is correct |
17 |
Correct |
6 ms |
31068 KB |
Output is correct |
18 |
Correct |
6 ms |
31068 KB |
Output is correct |
19 |
Correct |
75 ms |
37200 KB |
Output is correct |
20 |
Correct |
74 ms |
37172 KB |
Output is correct |
21 |
Correct |
78 ms |
37456 KB |
Output is correct |
22 |
Correct |
73 ms |
38100 KB |
Output is correct |
23 |
Correct |
54 ms |
37460 KB |
Output is correct |
24 |
Correct |
41 ms |
37204 KB |
Output is correct |
25 |
Correct |
86 ms |
42064 KB |
Output is correct |
26 |
Correct |
65 ms |
43860 KB |
Output is correct |
27 |
Correct |
101 ms |
41048 KB |
Output is correct |
28 |
Correct |
161 ms |
51608 KB |
Output is correct |
29 |
Correct |
150 ms |
46932 KB |
Output is correct |
30 |
Correct |
101 ms |
41020 KB |
Output is correct |
31 |
Correct |
101 ms |
41040 KB |
Output is correct |
32 |
Correct |
117 ms |
40848 KB |
Output is correct |
33 |
Correct |
121 ms |
39764 KB |
Output is correct |
34 |
Correct |
108 ms |
39760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31068 KB |
Output is correct |
2 |
Correct |
7 ms |
31068 KB |
Output is correct |
3 |
Correct |
7 ms |
31068 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
6 ms |
31068 KB |
Output is correct |
6 |
Correct |
6 ms |
31068 KB |
Output is correct |
7 |
Correct |
6 ms |
31204 KB |
Output is correct |
8 |
Correct |
6 ms |
31228 KB |
Output is correct |
9 |
Correct |
6 ms |
31232 KB |
Output is correct |
10 |
Correct |
6 ms |
31068 KB |
Output is correct |
11 |
Correct |
6 ms |
31068 KB |
Output is correct |
12 |
Correct |
6 ms |
31220 KB |
Output is correct |
13 |
Correct |
6 ms |
31068 KB |
Output is correct |
14 |
Correct |
6 ms |
31204 KB |
Output is correct |
15 |
Correct |
6 ms |
31068 KB |
Output is correct |
16 |
Correct |
6 ms |
31068 KB |
Output is correct |
17 |
Correct |
6 ms |
31068 KB |
Output is correct |
18 |
Correct |
6 ms |
31068 KB |
Output is correct |
19 |
Correct |
6 ms |
31064 KB |
Output is correct |
20 |
Correct |
6 ms |
31228 KB |
Output is correct |
21 |
Correct |
6 ms |
31068 KB |
Output is correct |
22 |
Correct |
7 ms |
33116 KB |
Output is correct |
23 |
Correct |
7 ms |
33116 KB |
Output is correct |
24 |
Correct |
7 ms |
33328 KB |
Output is correct |
25 |
Correct |
7 ms |
33116 KB |
Output is correct |
26 |
Correct |
7 ms |
33116 KB |
Output is correct |
27 |
Correct |
8 ms |
33116 KB |
Output is correct |
28 |
Correct |
7 ms |
33276 KB |
Output is correct |
29 |
Correct |
7 ms |
33348 KB |
Output is correct |
30 |
Correct |
7 ms |
33116 KB |
Output is correct |
31 |
Correct |
8 ms |
33116 KB |
Output is correct |
32 |
Correct |
8 ms |
33112 KB |
Output is correct |
33 |
Correct |
7 ms |
33116 KB |
Output is correct |
34 |
Correct |
7 ms |
33116 KB |
Output is correct |
35 |
Correct |
7 ms |
33272 KB |
Output is correct |
36 |
Correct |
7 ms |
33116 KB |
Output is correct |
37 |
Correct |
7 ms |
33116 KB |
Output is correct |
38 |
Correct |
7 ms |
33344 KB |
Output is correct |
39 |
Correct |
75 ms |
37200 KB |
Output is correct |
40 |
Correct |
74 ms |
37172 KB |
Output is correct |
41 |
Correct |
78 ms |
37456 KB |
Output is correct |
42 |
Correct |
73 ms |
38100 KB |
Output is correct |
43 |
Correct |
54 ms |
37460 KB |
Output is correct |
44 |
Correct |
41 ms |
37204 KB |
Output is correct |
45 |
Correct |
86 ms |
42064 KB |
Output is correct |
46 |
Correct |
65 ms |
43860 KB |
Output is correct |
47 |
Correct |
101 ms |
41048 KB |
Output is correct |
48 |
Correct |
161 ms |
51608 KB |
Output is correct |
49 |
Correct |
150 ms |
46932 KB |
Output is correct |
50 |
Correct |
101 ms |
41020 KB |
Output is correct |
51 |
Correct |
101 ms |
41040 KB |
Output is correct |
52 |
Correct |
117 ms |
40848 KB |
Output is correct |
53 |
Correct |
121 ms |
39764 KB |
Output is correct |
54 |
Correct |
108 ms |
39760 KB |
Output is correct |
55 |
Correct |
11 ms |
31576 KB |
Output is correct |
56 |
Correct |
12 ms |
31776 KB |
Output is correct |
57 |
Correct |
53 ms |
38364 KB |
Output is correct |
58 |
Correct |
33 ms |
39120 KB |
Output is correct |
59 |
Correct |
65 ms |
46676 KB |
Output is correct |
60 |
Correct |
202 ms |
53324 KB |
Output is correct |
61 |
Correct |
111 ms |
42712 KB |
Output is correct |
62 |
Correct |
121 ms |
46420 KB |
Output is correct |
63 |
Correct |
167 ms |
46064 KB |
Output is correct |
64 |
Correct |
202 ms |
45632 KB |
Output is correct |
65 |
Correct |
143 ms |
44116 KB |
Output is correct |
66 |
Correct |
218 ms |
52312 KB |
Output is correct |
67 |
Correct |
78 ms |
48228 KB |
Output is correct |
68 |
Correct |
137 ms |
46208 KB |
Output is correct |
69 |
Correct |
143 ms |
43988 KB |
Output is correct |
70 |
Correct |
128 ms |
46220 KB |
Output is correct |