#include "race.h"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
vector<pair<ll,ll>> adj[200005];
bool del[200005];
ll sub[200005];
ll dp[1000000];
ll k;
ll ans = 1e10;
inline ll get_sub(ll node, ll parent){
sub[node] = 1;
for(auto go : adj[node]){
if(go.first == parent or del[go.first]) continue;
sub[node] += get_sub(go.first,node);
}
return sub[node];
}
inline ll get_centroid(ll node, ll parent, ll tree_size){
for(auto go : adj[node]){
if(go.first == parent or del[go.first]) continue;
if(sub[go.first] >= tree_size/2) return get_centroid(go.first,node,tree_size);
}
return node;
}
inline void control(ll node, ll parent, ll val, ll edge){
if(val > k) return;
if(dp[k-val] != -1) ans = min(ans,dp[k-val] + edge);
for(auto go : adj[node]){
if(go.first == parent or del[go.first]) continue;
control(go.first,node,val+go.second,edge+1);
}
}
inline void build(ll node, ll parent, ll val, ll edge){
if(val > k) return;
if(dp[val] != -1) dp[val] = min(dp[val],edge);
else dp[val] = edge;
for(auto go : adj[node]){
if(go.first == parent or del[go.first]) continue;
build(go.first,node,val+go.second,edge+1);
}
}
inline void dp_reset(ll node, ll parent, ll val){
if(val > k) return;
dp[val] = -1;
for(auto go : adj[node]){
if(go.first == parent or del[go.first]) continue;
dp_reset(go.first,node,val+go.second);
}
}
inline void solve(ll node){
ll centroid = get_centroid(node,-1,get_sub(node,-1));
dp[0] = 0;
for(auto go : adj[centroid]){
if(del[go.first]) continue;
control(go.first,centroid,go.second,1);
build(go.first,centroid,go.second,1);
}
for(auto go : adj[centroid]){
if(del[go.first]) continue;
dp_reset(go.first,centroid,go.second);
}
del[centroid] = 1;
for(auto go : adj[centroid]){
if(del[go.first]) continue;
solve(go.first);
}
}
int best_path(int N, int K, int H[][2], int L[]){
k = K;
memset(dp,-1,sizeof(dp));
for(ll i = 0 ; i < N-1 ; i++){
adj[H[i][1]].pb({H[i][0],L[i]});
adj[H[i][0]].pb({H[i][1],L[i]});
}
solve(0);
if(ans == 1e10) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
3 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18268 KB |
Output is correct |
5 |
Correct |
3 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
18268 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
3 ms |
18268 KB |
Output is correct |
9 |
Correct |
4 ms |
18504 KB |
Output is correct |
10 |
Correct |
3 ms |
18268 KB |
Output is correct |
11 |
Correct |
3 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18268 KB |
Output is correct |
14 |
Correct |
3 ms |
18268 KB |
Output is correct |
15 |
Correct |
3 ms |
18268 KB |
Output is correct |
16 |
Correct |
2 ms |
18356 KB |
Output is correct |
17 |
Correct |
3 ms |
18268 KB |
Output is correct |
18 |
Correct |
3 ms |
18268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
3 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18268 KB |
Output is correct |
5 |
Correct |
3 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
18268 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
3 ms |
18268 KB |
Output is correct |
9 |
Correct |
4 ms |
18504 KB |
Output is correct |
10 |
Correct |
3 ms |
18268 KB |
Output is correct |
11 |
Correct |
3 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18268 KB |
Output is correct |
14 |
Correct |
3 ms |
18268 KB |
Output is correct |
15 |
Correct |
3 ms |
18268 KB |
Output is correct |
16 |
Correct |
2 ms |
18356 KB |
Output is correct |
17 |
Correct |
3 ms |
18268 KB |
Output is correct |
18 |
Correct |
3 ms |
18268 KB |
Output is correct |
19 |
Correct |
3 ms |
18268 KB |
Output is correct |
20 |
Correct |
3 ms |
18268 KB |
Output is correct |
21 |
Correct |
3 ms |
18404 KB |
Output is correct |
22 |
Correct |
3 ms |
18268 KB |
Output is correct |
23 |
Correct |
4 ms |
18268 KB |
Output is correct |
24 |
Correct |
3 ms |
18268 KB |
Output is correct |
25 |
Correct |
5 ms |
18268 KB |
Output is correct |
26 |
Correct |
3 ms |
18268 KB |
Output is correct |
27 |
Correct |
4 ms |
18268 KB |
Output is correct |
28 |
Correct |
4 ms |
18268 KB |
Output is correct |
29 |
Correct |
3 ms |
18268 KB |
Output is correct |
30 |
Correct |
4 ms |
18268 KB |
Output is correct |
31 |
Correct |
4 ms |
18268 KB |
Output is correct |
32 |
Correct |
3 ms |
18268 KB |
Output is correct |
33 |
Correct |
4 ms |
18268 KB |
Output is correct |
34 |
Correct |
3 ms |
18268 KB |
Output is correct |
35 |
Correct |
3 ms |
18364 KB |
Output is correct |
36 |
Correct |
4 ms |
18268 KB |
Output is correct |
37 |
Correct |
3 ms |
18268 KB |
Output is correct |
38 |
Correct |
4 ms |
18268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
3 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18268 KB |
Output is correct |
5 |
Correct |
3 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
18268 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
3 ms |
18268 KB |
Output is correct |
9 |
Correct |
4 ms |
18504 KB |
Output is correct |
10 |
Correct |
3 ms |
18268 KB |
Output is correct |
11 |
Correct |
3 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18268 KB |
Output is correct |
14 |
Correct |
3 ms |
18268 KB |
Output is correct |
15 |
Correct |
3 ms |
18268 KB |
Output is correct |
16 |
Correct |
2 ms |
18356 KB |
Output is correct |
17 |
Correct |
3 ms |
18268 KB |
Output is correct |
18 |
Correct |
3 ms |
18268 KB |
Output is correct |
19 |
Correct |
83 ms |
26964 KB |
Output is correct |
20 |
Correct |
95 ms |
26972 KB |
Output is correct |
21 |
Correct |
91 ms |
26964 KB |
Output is correct |
22 |
Correct |
85 ms |
26964 KB |
Output is correct |
23 |
Correct |
42 ms |
27476 KB |
Output is correct |
24 |
Correct |
40 ms |
26716 KB |
Output is correct |
25 |
Correct |
79 ms |
27988 KB |
Output is correct |
26 |
Correct |
63 ms |
29012 KB |
Output is correct |
27 |
Correct |
98 ms |
33984 KB |
Output is correct |
28 |
Correct |
139 ms |
38228 KB |
Output is correct |
29 |
Correct |
134 ms |
37716 KB |
Output is correct |
30 |
Correct |
97 ms |
34128 KB |
Output is correct |
31 |
Correct |
94 ms |
34132 KB |
Output is correct |
32 |
Correct |
109 ms |
34124 KB |
Output is correct |
33 |
Correct |
115 ms |
32716 KB |
Output is correct |
34 |
Correct |
100 ms |
33620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
3 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18268 KB |
Output is correct |
5 |
Correct |
3 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
18268 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
3 ms |
18268 KB |
Output is correct |
9 |
Correct |
4 ms |
18504 KB |
Output is correct |
10 |
Correct |
3 ms |
18268 KB |
Output is correct |
11 |
Correct |
3 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18268 KB |
Output is correct |
14 |
Correct |
3 ms |
18268 KB |
Output is correct |
15 |
Correct |
3 ms |
18268 KB |
Output is correct |
16 |
Correct |
2 ms |
18356 KB |
Output is correct |
17 |
Correct |
3 ms |
18268 KB |
Output is correct |
18 |
Correct |
3 ms |
18268 KB |
Output is correct |
19 |
Correct |
3 ms |
18268 KB |
Output is correct |
20 |
Correct |
3 ms |
18268 KB |
Output is correct |
21 |
Correct |
3 ms |
18404 KB |
Output is correct |
22 |
Correct |
3 ms |
18268 KB |
Output is correct |
23 |
Correct |
4 ms |
18268 KB |
Output is correct |
24 |
Correct |
3 ms |
18268 KB |
Output is correct |
25 |
Correct |
5 ms |
18268 KB |
Output is correct |
26 |
Correct |
3 ms |
18268 KB |
Output is correct |
27 |
Correct |
4 ms |
18268 KB |
Output is correct |
28 |
Correct |
4 ms |
18268 KB |
Output is correct |
29 |
Correct |
3 ms |
18268 KB |
Output is correct |
30 |
Correct |
4 ms |
18268 KB |
Output is correct |
31 |
Correct |
4 ms |
18268 KB |
Output is correct |
32 |
Correct |
3 ms |
18268 KB |
Output is correct |
33 |
Correct |
4 ms |
18268 KB |
Output is correct |
34 |
Correct |
3 ms |
18268 KB |
Output is correct |
35 |
Correct |
3 ms |
18364 KB |
Output is correct |
36 |
Correct |
4 ms |
18268 KB |
Output is correct |
37 |
Correct |
3 ms |
18268 KB |
Output is correct |
38 |
Correct |
4 ms |
18268 KB |
Output is correct |
39 |
Correct |
83 ms |
26964 KB |
Output is correct |
40 |
Correct |
95 ms |
26972 KB |
Output is correct |
41 |
Correct |
91 ms |
26964 KB |
Output is correct |
42 |
Correct |
85 ms |
26964 KB |
Output is correct |
43 |
Correct |
42 ms |
27476 KB |
Output is correct |
44 |
Correct |
40 ms |
26716 KB |
Output is correct |
45 |
Correct |
79 ms |
27988 KB |
Output is correct |
46 |
Correct |
63 ms |
29012 KB |
Output is correct |
47 |
Correct |
98 ms |
33984 KB |
Output is correct |
48 |
Correct |
139 ms |
38228 KB |
Output is correct |
49 |
Correct |
134 ms |
37716 KB |
Output is correct |
50 |
Correct |
97 ms |
34128 KB |
Output is correct |
51 |
Correct |
94 ms |
34132 KB |
Output is correct |
52 |
Correct |
109 ms |
34124 KB |
Output is correct |
53 |
Correct |
115 ms |
32716 KB |
Output is correct |
54 |
Correct |
100 ms |
33620 KB |
Output is correct |
55 |
Correct |
8 ms |
19036 KB |
Output is correct |
56 |
Correct |
9 ms |
19036 KB |
Output is correct |
57 |
Correct |
40 ms |
27244 KB |
Output is correct |
58 |
Correct |
22 ms |
26316 KB |
Output is correct |
59 |
Correct |
61 ms |
29012 KB |
Output is correct |
60 |
Correct |
254 ms |
37716 KB |
Output is correct |
61 |
Correct |
109 ms |
34364 KB |
Output is correct |
62 |
Correct |
113 ms |
34120 KB |
Output is correct |
63 |
Correct |
130 ms |
34144 KB |
Output is correct |
64 |
Correct |
271 ms |
34224 KB |
Output is correct |
65 |
Correct |
109 ms |
34644 KB |
Output is correct |
66 |
Correct |
217 ms |
37192 KB |
Output is correct |
67 |
Correct |
64 ms |
34240 KB |
Output is correct |
68 |
Correct |
134 ms |
34388 KB |
Output is correct |
69 |
Correct |
134 ms |
34248 KB |
Output is correct |
70 |
Correct |
120 ms |
33620 KB |
Output is correct |