#include <bits/stdc++.h>
#include "race.h"
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
const int maxn = 200001;
int N, K;
vector<int> adj[maxn];
vector<pair<int, int>> adj2[maxn];
int sub[maxn];
bool done[maxn];
int subtree_size(int s, int p){
sub[s] = 1;
for(auto u: adj[s]){
if(u == p or done[u]) continue;
sub[s] += subtree_size(u, s);
}
return sub[s];
}
int get_centroid(int s, int p, int num){
for(auto u: adj[s]){
if(u == p or done[u]) continue;
if(2*sub[u] > num) return get_centroid(u, s, num);
}
return s;
}
ll ans = 1e18;
map<ll, ll> best;
void calc(int s, int p, bool fill, int d, int wt){
if(wt > K) return;
if(fill){
if(best.count(wt)) best[wt] = min(best[wt], 1LL*d);
else best[wt] = d;
}
else{
if(best.count(K-wt)){
ans = min(ans, best[K-wt] + 1LL*d);
}
}
for(auto &[u, t]: adj2[s]){
if(u == p or done[u]) continue;
calc(u, s, fill, d+1, wt+t);
}
}
void centroid_decomp(int s){
int num = subtree_size(s, 0);
int cent = get_centroid(s, 0, num);
best[0] = 0;
for(auto &[u, w]: adj2[cent]){
if(done[u]) continue;
calc(u, cent, false, 1, w);
calc(u, cent, true, 1, w);
}
best.clear();
done[cent] = true;
for(auto u: adj[cent]){
if(!done[u]) centroid_decomp(u);
}
}
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 a = H[i][0], b = H[i][1], w = L[i];
a++; b++;
adj[a].pb(b); adj[b].pb(a);
adj2[a].pb({b, w}); adj2[b].pb({a, w});
}
centroid_decomp(1);
if(ans == 1e18) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Correct |
3 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14680 KB |
Output is correct |
5 |
Correct |
3 ms |
14428 KB |
Output is correct |
6 |
Correct |
3 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
4 ms |
14256 KB |
Output is correct |
10 |
Correct |
3 ms |
14424 KB |
Output is correct |
11 |
Correct |
3 ms |
14428 KB |
Output is correct |
12 |
Correct |
3 ms |
14428 KB |
Output is correct |
13 |
Correct |
3 ms |
14428 KB |
Output is correct |
14 |
Correct |
3 ms |
14428 KB |
Output is correct |
15 |
Correct |
3 ms |
14428 KB |
Output is correct |
16 |
Correct |
3 ms |
14428 KB |
Output is correct |
17 |
Correct |
3 ms |
14428 KB |
Output is correct |
18 |
Correct |
3 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Correct |
3 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14680 KB |
Output is correct |
5 |
Correct |
3 ms |
14428 KB |
Output is correct |
6 |
Correct |
3 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
4 ms |
14256 KB |
Output is correct |
10 |
Correct |
3 ms |
14424 KB |
Output is correct |
11 |
Correct |
3 ms |
14428 KB |
Output is correct |
12 |
Correct |
3 ms |
14428 KB |
Output is correct |
13 |
Correct |
3 ms |
14428 KB |
Output is correct |
14 |
Correct |
3 ms |
14428 KB |
Output is correct |
15 |
Correct |
3 ms |
14428 KB |
Output is correct |
16 |
Correct |
3 ms |
14428 KB |
Output is correct |
17 |
Correct |
3 ms |
14428 KB |
Output is correct |
18 |
Correct |
3 ms |
14428 KB |
Output is correct |
19 |
Correct |
3 ms |
14428 KB |
Output is correct |
20 |
Correct |
3 ms |
14428 KB |
Output is correct |
21 |
Correct |
4 ms |
14424 KB |
Output is correct |
22 |
Correct |
3 ms |
14428 KB |
Output is correct |
23 |
Correct |
3 ms |
14428 KB |
Output is correct |
24 |
Correct |
3 ms |
14428 KB |
Output is correct |
25 |
Correct |
5 ms |
14428 KB |
Output is correct |
26 |
Correct |
4 ms |
14428 KB |
Output is correct |
27 |
Correct |
3 ms |
14428 KB |
Output is correct |
28 |
Correct |
5 ms |
14428 KB |
Output is correct |
29 |
Correct |
4 ms |
14428 KB |
Output is correct |
30 |
Correct |
4 ms |
14428 KB |
Output is correct |
31 |
Correct |
4 ms |
14432 KB |
Output is correct |
32 |
Correct |
4 ms |
14428 KB |
Output is correct |
33 |
Correct |
4 ms |
14428 KB |
Output is correct |
34 |
Correct |
4 ms |
14428 KB |
Output is correct |
35 |
Correct |
4 ms |
14436 KB |
Output is correct |
36 |
Correct |
4 ms |
14428 KB |
Output is correct |
37 |
Correct |
4 ms |
14580 KB |
Output is correct |
38 |
Correct |
5 ms |
14448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Correct |
3 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14680 KB |
Output is correct |
5 |
Correct |
3 ms |
14428 KB |
Output is correct |
6 |
Correct |
3 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
4 ms |
14256 KB |
Output is correct |
10 |
Correct |
3 ms |
14424 KB |
Output is correct |
11 |
Correct |
3 ms |
14428 KB |
Output is correct |
12 |
Correct |
3 ms |
14428 KB |
Output is correct |
13 |
Correct |
3 ms |
14428 KB |
Output is correct |
14 |
Correct |
3 ms |
14428 KB |
Output is correct |
15 |
Correct |
3 ms |
14428 KB |
Output is correct |
16 |
Correct |
3 ms |
14428 KB |
Output is correct |
17 |
Correct |
3 ms |
14428 KB |
Output is correct |
18 |
Correct |
3 ms |
14428 KB |
Output is correct |
19 |
Correct |
156 ms |
23500 KB |
Output is correct |
20 |
Correct |
172 ms |
23572 KB |
Output is correct |
21 |
Correct |
173 ms |
23536 KB |
Output is correct |
22 |
Correct |
162 ms |
23636 KB |
Output is correct |
23 |
Correct |
71 ms |
23632 KB |
Output is correct |
24 |
Correct |
85 ms |
23820 KB |
Output is correct |
25 |
Correct |
121 ms |
26140 KB |
Output is correct |
26 |
Correct |
100 ms |
28952 KB |
Output is correct |
27 |
Correct |
201 ms |
31060 KB |
Output is correct |
28 |
Correct |
228 ms |
41608 KB |
Output is correct |
29 |
Correct |
231 ms |
40532 KB |
Output is correct |
30 |
Correct |
210 ms |
31084 KB |
Output is correct |
31 |
Correct |
238 ms |
31060 KB |
Output is correct |
32 |
Correct |
239 ms |
31316 KB |
Output is correct |
33 |
Correct |
239 ms |
29308 KB |
Output is correct |
34 |
Correct |
163 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14424 KB |
Output is correct |
2 |
Correct |
3 ms |
14428 KB |
Output is correct |
3 |
Correct |
3 ms |
14428 KB |
Output is correct |
4 |
Correct |
3 ms |
14680 KB |
Output is correct |
5 |
Correct |
3 ms |
14428 KB |
Output is correct |
6 |
Correct |
3 ms |
14428 KB |
Output is correct |
7 |
Correct |
3 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
14428 KB |
Output is correct |
9 |
Correct |
4 ms |
14256 KB |
Output is correct |
10 |
Correct |
3 ms |
14424 KB |
Output is correct |
11 |
Correct |
3 ms |
14428 KB |
Output is correct |
12 |
Correct |
3 ms |
14428 KB |
Output is correct |
13 |
Correct |
3 ms |
14428 KB |
Output is correct |
14 |
Correct |
3 ms |
14428 KB |
Output is correct |
15 |
Correct |
3 ms |
14428 KB |
Output is correct |
16 |
Correct |
3 ms |
14428 KB |
Output is correct |
17 |
Correct |
3 ms |
14428 KB |
Output is correct |
18 |
Correct |
3 ms |
14428 KB |
Output is correct |
19 |
Correct |
3 ms |
14428 KB |
Output is correct |
20 |
Correct |
3 ms |
14428 KB |
Output is correct |
21 |
Correct |
4 ms |
14424 KB |
Output is correct |
22 |
Correct |
3 ms |
14428 KB |
Output is correct |
23 |
Correct |
3 ms |
14428 KB |
Output is correct |
24 |
Correct |
3 ms |
14428 KB |
Output is correct |
25 |
Correct |
5 ms |
14428 KB |
Output is correct |
26 |
Correct |
4 ms |
14428 KB |
Output is correct |
27 |
Correct |
3 ms |
14428 KB |
Output is correct |
28 |
Correct |
5 ms |
14428 KB |
Output is correct |
29 |
Correct |
4 ms |
14428 KB |
Output is correct |
30 |
Correct |
4 ms |
14428 KB |
Output is correct |
31 |
Correct |
4 ms |
14432 KB |
Output is correct |
32 |
Correct |
4 ms |
14428 KB |
Output is correct |
33 |
Correct |
4 ms |
14428 KB |
Output is correct |
34 |
Correct |
4 ms |
14428 KB |
Output is correct |
35 |
Correct |
4 ms |
14436 KB |
Output is correct |
36 |
Correct |
4 ms |
14428 KB |
Output is correct |
37 |
Correct |
4 ms |
14580 KB |
Output is correct |
38 |
Correct |
5 ms |
14448 KB |
Output is correct |
39 |
Correct |
156 ms |
23500 KB |
Output is correct |
40 |
Correct |
172 ms |
23572 KB |
Output is correct |
41 |
Correct |
173 ms |
23536 KB |
Output is correct |
42 |
Correct |
162 ms |
23636 KB |
Output is correct |
43 |
Correct |
71 ms |
23632 KB |
Output is correct |
44 |
Correct |
85 ms |
23820 KB |
Output is correct |
45 |
Correct |
121 ms |
26140 KB |
Output is correct |
46 |
Correct |
100 ms |
28952 KB |
Output is correct |
47 |
Correct |
201 ms |
31060 KB |
Output is correct |
48 |
Correct |
228 ms |
41608 KB |
Output is correct |
49 |
Correct |
231 ms |
40532 KB |
Output is correct |
50 |
Correct |
210 ms |
31084 KB |
Output is correct |
51 |
Correct |
238 ms |
31060 KB |
Output is correct |
52 |
Correct |
239 ms |
31316 KB |
Output is correct |
53 |
Correct |
239 ms |
29308 KB |
Output is correct |
54 |
Correct |
163 ms |
29264 KB |
Output is correct |
55 |
Correct |
21 ms |
15200 KB |
Output is correct |
56 |
Correct |
14 ms |
14940 KB |
Output is correct |
57 |
Correct |
142 ms |
23664 KB |
Output is correct |
58 |
Correct |
43 ms |
24064 KB |
Output is correct |
59 |
Correct |
249 ms |
33864 KB |
Output is correct |
60 |
Correct |
788 ms |
53328 KB |
Output is correct |
61 |
Correct |
253 ms |
31320 KB |
Output is correct |
62 |
Correct |
263 ms |
31300 KB |
Output is correct |
63 |
Correct |
340 ms |
31316 KB |
Output is correct |
64 |
Correct |
868 ms |
36552 KB |
Output is correct |
65 |
Correct |
239 ms |
30604 KB |
Output is correct |
66 |
Correct |
471 ms |
39116 KB |
Output is correct |
67 |
Correct |
124 ms |
31540 KB |
Output is correct |
68 |
Correct |
389 ms |
36124 KB |
Output is correct |
69 |
Correct |
393 ms |
35860 KB |
Output is correct |
70 |
Correct |
356 ms |
35396 KB |
Output is correct |