#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define For(i,a,b) for(int i=a;i<=b;i++)
typedef pair<int,int> pii;
typedef vector<int> vi;
#define pb push_back
const int Size=2e5;
const int Maxn=1e6;
const int INF=1<<20;
int ans;
int dp[Maxn+20],sz[Size+20],cnt[Maxn+20];
vector<pii> adj[Size+20];
bitset<Size+20> vis;
void dfs_sz(int u,int pa){
sz[u]=1;
for(auto [v,w]:adj[u]){
if(v==pa||vis[v]) continue;
dfs_sz(v,u);
sz[u]+=sz[v];
}
}
int dfs_cen(int u,int pa,int tot_sz){
for(auto [v,w]:adj[u]){
if(v==pa||vis[v]) continue;
if(sz[v]>(tot_sz>>1)) return dfs_cen(v,u,tot_sz);
}
return u;
}
void get_path(int u,int pa,int len,int dep,vi &vec,int K){
if(len>K) return ;
cnt[len]=min(cnt[len],dep);
ans=min(ans,dp[K-len]+cnt[len]);
vec.pb(len);
for(auto [v,w]:adj[u]){
if(v==pa||vis[v]) continue;
get_path(v,u,len+w,dep+1,vec,K);
}
}
void c_d(int rt,int K){
dfs_sz(rt,rt);
int centroid=dfs_cen(rt,rt,sz[rt]);
vis[centroid]=true;
vi vec;
for(auto [v,w]:adj[centroid]){
if(vis[v]) continue;
vi vec2;
get_path(v,centroid,w,1,vec2,K);
for(auto i:vec2){
dp[i]=min(dp[i],cnt[i]);
vec.pb(i);
}
for(auto i:vec2) cnt[i]=INF;
}
for(auto i:vec) dp[i]=INF;
for(auto [v,w]:adj[centroid]){
if(vis[v]) continue;
c_d(v,K);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
For(i,0,N-2){
int u=H[i][0],v=H[i][1],w=L[i];
u++,v++;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
ans=INF;
For(i,1,K) dp[i]=cnt[i]=INF;
c_d(1,K);
return (ans==INF?-1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14804 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14824 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14780 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14796 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14796 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14804 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14824 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14780 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14796 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14796 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
19 |
Correct |
2 ms |
14936 KB |
Output is correct |
20 |
Correct |
2 ms |
14680 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16732 KB |
Output is correct |
24 |
Correct |
3 ms |
16732 KB |
Output is correct |
25 |
Correct |
3 ms |
16908 KB |
Output is correct |
26 |
Correct |
3 ms |
16856 KB |
Output is correct |
27 |
Correct |
3 ms |
16732 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
29 |
Correct |
4 ms |
14684 KB |
Output is correct |
30 |
Correct |
3 ms |
16732 KB |
Output is correct |
31 |
Correct |
4 ms |
16912 KB |
Output is correct |
32 |
Correct |
4 ms |
16728 KB |
Output is correct |
33 |
Correct |
3 ms |
16728 KB |
Output is correct |
34 |
Correct |
3 ms |
16728 KB |
Output is correct |
35 |
Correct |
3 ms |
16732 KB |
Output is correct |
36 |
Correct |
3 ms |
17112 KB |
Output is correct |
37 |
Correct |
4 ms |
16896 KB |
Output is correct |
38 |
Correct |
3 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14804 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14824 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14780 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14796 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14796 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
19 |
Correct |
74 ms |
22100 KB |
Output is correct |
20 |
Correct |
73 ms |
22172 KB |
Output is correct |
21 |
Correct |
78 ms |
22452 KB |
Output is correct |
22 |
Correct |
71 ms |
23020 KB |
Output is correct |
23 |
Correct |
58 ms |
22364 KB |
Output is correct |
24 |
Correct |
42 ms |
22096 KB |
Output is correct |
25 |
Correct |
82 ms |
25576 KB |
Output is correct |
26 |
Correct |
74 ms |
30276 KB |
Output is correct |
27 |
Correct |
106 ms |
27476 KB |
Output is correct |
28 |
Correct |
161 ms |
41808 KB |
Output is correct |
29 |
Correct |
174 ms |
40788 KB |
Output is correct |
30 |
Correct |
112 ms |
27344 KB |
Output is correct |
31 |
Correct |
130 ms |
27624 KB |
Output is correct |
32 |
Correct |
123 ms |
27644 KB |
Output is correct |
33 |
Correct |
131 ms |
26196 KB |
Output is correct |
34 |
Correct |
104 ms |
27220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14804 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
3 ms |
14824 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14780 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14796 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14796 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
19 |
Correct |
2 ms |
14936 KB |
Output is correct |
20 |
Correct |
2 ms |
14680 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16732 KB |
Output is correct |
24 |
Correct |
3 ms |
16732 KB |
Output is correct |
25 |
Correct |
3 ms |
16908 KB |
Output is correct |
26 |
Correct |
3 ms |
16856 KB |
Output is correct |
27 |
Correct |
3 ms |
16732 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
29 |
Correct |
4 ms |
14684 KB |
Output is correct |
30 |
Correct |
3 ms |
16732 KB |
Output is correct |
31 |
Correct |
4 ms |
16912 KB |
Output is correct |
32 |
Correct |
4 ms |
16728 KB |
Output is correct |
33 |
Correct |
3 ms |
16728 KB |
Output is correct |
34 |
Correct |
3 ms |
16728 KB |
Output is correct |
35 |
Correct |
3 ms |
16732 KB |
Output is correct |
36 |
Correct |
3 ms |
17112 KB |
Output is correct |
37 |
Correct |
4 ms |
16896 KB |
Output is correct |
38 |
Correct |
3 ms |
16732 KB |
Output is correct |
39 |
Correct |
74 ms |
22100 KB |
Output is correct |
40 |
Correct |
73 ms |
22172 KB |
Output is correct |
41 |
Correct |
78 ms |
22452 KB |
Output is correct |
42 |
Correct |
71 ms |
23020 KB |
Output is correct |
43 |
Correct |
58 ms |
22364 KB |
Output is correct |
44 |
Correct |
42 ms |
22096 KB |
Output is correct |
45 |
Correct |
82 ms |
25576 KB |
Output is correct |
46 |
Correct |
74 ms |
30276 KB |
Output is correct |
47 |
Correct |
106 ms |
27476 KB |
Output is correct |
48 |
Correct |
161 ms |
41808 KB |
Output is correct |
49 |
Correct |
174 ms |
40788 KB |
Output is correct |
50 |
Correct |
112 ms |
27344 KB |
Output is correct |
51 |
Correct |
130 ms |
27624 KB |
Output is correct |
52 |
Correct |
123 ms |
27644 KB |
Output is correct |
53 |
Correct |
131 ms |
26196 KB |
Output is correct |
54 |
Correct |
104 ms |
27220 KB |
Output is correct |
55 |
Correct |
8 ms |
15196 KB |
Output is correct |
56 |
Correct |
9 ms |
15468 KB |
Output is correct |
57 |
Correct |
62 ms |
22956 KB |
Output is correct |
58 |
Correct |
31 ms |
22780 KB |
Output is correct |
59 |
Correct |
73 ms |
30304 KB |
Output is correct |
60 |
Correct |
241 ms |
45532 KB |
Output is correct |
61 |
Correct |
127 ms |
27988 KB |
Output is correct |
62 |
Correct |
122 ms |
31208 KB |
Output is correct |
63 |
Correct |
142 ms |
31188 KB |
Output is correct |
64 |
Correct |
212 ms |
31684 KB |
Output is correct |
65 |
Correct |
104 ms |
28240 KB |
Output is correct |
66 |
Correct |
227 ms |
41040 KB |
Output is correct |
67 |
Correct |
84 ms |
32632 KB |
Output is correct |
68 |
Correct |
161 ms |
32460 KB |
Output is correct |
69 |
Correct |
158 ms |
32204 KB |
Output is correct |
70 |
Correct |
137 ms |
31428 KB |
Output is correct |