#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 |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 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 |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
3 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 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 |
14800 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 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 |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
3 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 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 |
14800 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
2 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16860 KB |
Output is correct |
24 |
Correct |
4 ms |
16732 KB |
Output is correct |
25 |
Correct |
3 ms |
16732 KB |
Output is correct |
26 |
Correct |
3 ms |
16732 KB |
Output is correct |
27 |
Correct |
4 ms |
16732 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
29 |
Correct |
4 ms |
14860 KB |
Output is correct |
30 |
Correct |
3 ms |
16732 KB |
Output is correct |
31 |
Correct |
3 ms |
16732 KB |
Output is correct |
32 |
Correct |
4 ms |
16860 KB |
Output is correct |
33 |
Correct |
4 ms |
16908 KB |
Output is correct |
34 |
Correct |
3 ms |
16984 KB |
Output is correct |
35 |
Correct |
4 ms |
16732 KB |
Output is correct |
36 |
Correct |
4 ms |
16984 KB |
Output is correct |
37 |
Correct |
4 ms |
16728 KB |
Output is correct |
38 |
Correct |
3 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 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 |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
3 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 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 |
14800 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
19 |
Correct |
84 ms |
22484 KB |
Output is correct |
20 |
Correct |
76 ms |
22100 KB |
Output is correct |
21 |
Correct |
79 ms |
22572 KB |
Output is correct |
22 |
Correct |
73 ms |
22864 KB |
Output is correct |
23 |
Correct |
53 ms |
22360 KB |
Output is correct |
24 |
Correct |
40 ms |
22108 KB |
Output is correct |
25 |
Correct |
84 ms |
25684 KB |
Output is correct |
26 |
Correct |
76 ms |
30276 KB |
Output is correct |
27 |
Correct |
136 ms |
27472 KB |
Output is correct |
28 |
Correct |
178 ms |
41808 KB |
Output is correct |
29 |
Correct |
165 ms |
40816 KB |
Output is correct |
30 |
Correct |
110 ms |
27800 KB |
Output is correct |
31 |
Correct |
121 ms |
27496 KB |
Output is correct |
32 |
Correct |
126 ms |
27416 KB |
Output is correct |
33 |
Correct |
137 ms |
26360 KB |
Output is correct |
34 |
Correct |
111 ms |
27224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 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 |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
3 ms |
14684 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 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 |
14800 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
2 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16860 KB |
Output is correct |
24 |
Correct |
4 ms |
16732 KB |
Output is correct |
25 |
Correct |
3 ms |
16732 KB |
Output is correct |
26 |
Correct |
3 ms |
16732 KB |
Output is correct |
27 |
Correct |
4 ms |
16732 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
29 |
Correct |
4 ms |
14860 KB |
Output is correct |
30 |
Correct |
3 ms |
16732 KB |
Output is correct |
31 |
Correct |
3 ms |
16732 KB |
Output is correct |
32 |
Correct |
4 ms |
16860 KB |
Output is correct |
33 |
Correct |
4 ms |
16908 KB |
Output is correct |
34 |
Correct |
3 ms |
16984 KB |
Output is correct |
35 |
Correct |
4 ms |
16732 KB |
Output is correct |
36 |
Correct |
4 ms |
16984 KB |
Output is correct |
37 |
Correct |
4 ms |
16728 KB |
Output is correct |
38 |
Correct |
3 ms |
16732 KB |
Output is correct |
39 |
Correct |
84 ms |
22484 KB |
Output is correct |
40 |
Correct |
76 ms |
22100 KB |
Output is correct |
41 |
Correct |
79 ms |
22572 KB |
Output is correct |
42 |
Correct |
73 ms |
22864 KB |
Output is correct |
43 |
Correct |
53 ms |
22360 KB |
Output is correct |
44 |
Correct |
40 ms |
22108 KB |
Output is correct |
45 |
Correct |
84 ms |
25684 KB |
Output is correct |
46 |
Correct |
76 ms |
30276 KB |
Output is correct |
47 |
Correct |
136 ms |
27472 KB |
Output is correct |
48 |
Correct |
178 ms |
41808 KB |
Output is correct |
49 |
Correct |
165 ms |
40816 KB |
Output is correct |
50 |
Correct |
110 ms |
27800 KB |
Output is correct |
51 |
Correct |
121 ms |
27496 KB |
Output is correct |
52 |
Correct |
126 ms |
27416 KB |
Output is correct |
53 |
Correct |
137 ms |
26360 KB |
Output is correct |
54 |
Correct |
111 ms |
27224 KB |
Output is correct |
55 |
Correct |
10 ms |
15192 KB |
Output is correct |
56 |
Correct |
9 ms |
15452 KB |
Output is correct |
57 |
Correct |
59 ms |
22964 KB |
Output is correct |
58 |
Correct |
31 ms |
22728 KB |
Output is correct |
59 |
Correct |
74 ms |
30268 KB |
Output is correct |
60 |
Correct |
251 ms |
45532 KB |
Output is correct |
61 |
Correct |
117 ms |
27844 KB |
Output is correct |
62 |
Correct |
147 ms |
31720 KB |
Output is correct |
63 |
Correct |
146 ms |
31176 KB |
Output is correct |
64 |
Correct |
276 ms |
31632 KB |
Output is correct |
65 |
Correct |
113 ms |
28356 KB |
Output is correct |
66 |
Correct |
234 ms |
41040 KB |
Output is correct |
67 |
Correct |
74 ms |
32492 KB |
Output is correct |
68 |
Correct |
169 ms |
31944 KB |
Output is correct |
69 |
Correct |
173 ms |
32196 KB |
Output is correct |
70 |
Correct |
147 ms |
31428 KB |
Output is correct |