#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#include "race.h"
const int N=1e6+99;
int n,k,cent,ans=N,sz[N],vis[N],res[N];
int a[N][2],b[N];
vector<int> alh;
vector<pair<int,int>> vec,g[N];
void dfs1(int u,int p,int n){
sz[u]=1;
for(auto v : g[u]){
if(p==v.F || vis[v.F]) continue ;
dfs1(v.F,u,n);
sz[u]+=sz[v.F];
}
if(2*sz[u]>=n && cent==-1) cent=u;
}
void dfs2(int u,int p,int h1,int h2){
sz[u]=1;
if(h2<=k){
minm(ans,h1+res[k-h2]);
alh.pb(h2);
vec.pb({h2,h1});
}
else h2=k+1;
for(auto v : g[u]){
if(p==v.F || vis[v.F]) continue ;
dfs2(v.F,u,h1+1,h2+v.S);
sz[u]+=sz[v.F];
}
}
void solve(int rt,int n){
cent=-1;
dfs1(rt,rt,n);
//cout<<rt<<" "<<n<<endl;
if(cent==-1) assert(0);
res[0]=0;
alh.clear();
for(auto v : g[cent]){
if(vis[v.F]) continue ;
vec.clear();
dfs2(v.F,cent,1,v.S);
//eror(sz[v.F]);
for(auto p : vec){
minm(res[p.F],p.S);
}
}
for(auto x : alh) res[x]=N;
vis[cent]=1;
//eror(cent);
for(auto v : g[cent]){
if(vis[v.F]) continue ;
//cout<<v.F<<" "<<sz[v.F]<<" - "<<rt<<" "<<cent<<endl;
solve(v.F,sz[v.F]);
}
}
int best_path(int _n, int _k,int H[][2],int L[]){
fill(res,res+N,N);
n=_n,k=_k;
f(i,0,n-1){
int u=H[i][0],v=H[i][1],w=L[i];
g[u].pb({v,w});
g[v].pb({u,w});
}
solve(1,n);
if(ans==N) return -1;
return ans;
}
/*
int32_t main(){
int n,k;
cin>>n>>k;
f(i,0,n-1) cin>>a[i][0]>>a[i][1];
f(i,0,n-1) cin>>b[i];
cout<<best_path(n,k,a,b);
}
*/
/*
11 12
0 1
0 2
2 3
3 4
4 5
0 6
6 7
6 8
8 9
8 10
3
4
5
4
6
3
2
5
6
7
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27704 KB |
Output is correct |
2 |
Correct |
15 ms |
27656 KB |
Output is correct |
3 |
Correct |
15 ms |
27632 KB |
Output is correct |
4 |
Correct |
17 ms |
27732 KB |
Output is correct |
5 |
Correct |
16 ms |
27684 KB |
Output is correct |
6 |
Correct |
15 ms |
27732 KB |
Output is correct |
7 |
Correct |
18 ms |
27732 KB |
Output is correct |
8 |
Correct |
15 ms |
27732 KB |
Output is correct |
9 |
Correct |
16 ms |
27732 KB |
Output is correct |
10 |
Correct |
14 ms |
27732 KB |
Output is correct |
11 |
Correct |
16 ms |
27676 KB |
Output is correct |
12 |
Correct |
15 ms |
27732 KB |
Output is correct |
13 |
Correct |
15 ms |
27732 KB |
Output is correct |
14 |
Correct |
15 ms |
27684 KB |
Output is correct |
15 |
Correct |
15 ms |
27748 KB |
Output is correct |
16 |
Correct |
15 ms |
27636 KB |
Output is correct |
17 |
Correct |
14 ms |
27732 KB |
Output is correct |
18 |
Correct |
15 ms |
27732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27704 KB |
Output is correct |
2 |
Correct |
15 ms |
27656 KB |
Output is correct |
3 |
Correct |
15 ms |
27632 KB |
Output is correct |
4 |
Correct |
17 ms |
27732 KB |
Output is correct |
5 |
Correct |
16 ms |
27684 KB |
Output is correct |
6 |
Correct |
15 ms |
27732 KB |
Output is correct |
7 |
Correct |
18 ms |
27732 KB |
Output is correct |
8 |
Correct |
15 ms |
27732 KB |
Output is correct |
9 |
Correct |
16 ms |
27732 KB |
Output is correct |
10 |
Correct |
14 ms |
27732 KB |
Output is correct |
11 |
Correct |
16 ms |
27676 KB |
Output is correct |
12 |
Correct |
15 ms |
27732 KB |
Output is correct |
13 |
Correct |
15 ms |
27732 KB |
Output is correct |
14 |
Correct |
15 ms |
27684 KB |
Output is correct |
15 |
Correct |
15 ms |
27748 KB |
Output is correct |
16 |
Correct |
15 ms |
27636 KB |
Output is correct |
17 |
Correct |
14 ms |
27732 KB |
Output is correct |
18 |
Correct |
15 ms |
27732 KB |
Output is correct |
19 |
Correct |
15 ms |
27728 KB |
Output is correct |
20 |
Correct |
14 ms |
27668 KB |
Output is correct |
21 |
Correct |
19 ms |
27732 KB |
Output is correct |
22 |
Correct |
19 ms |
27796 KB |
Output is correct |
23 |
Correct |
16 ms |
27700 KB |
Output is correct |
24 |
Correct |
15 ms |
27688 KB |
Output is correct |
25 |
Correct |
16 ms |
27812 KB |
Output is correct |
26 |
Correct |
15 ms |
27732 KB |
Output is correct |
27 |
Correct |
16 ms |
27756 KB |
Output is correct |
28 |
Correct |
16 ms |
27732 KB |
Output is correct |
29 |
Correct |
16 ms |
27712 KB |
Output is correct |
30 |
Correct |
16 ms |
27732 KB |
Output is correct |
31 |
Correct |
16 ms |
27732 KB |
Output is correct |
32 |
Correct |
18 ms |
27680 KB |
Output is correct |
33 |
Correct |
17 ms |
27800 KB |
Output is correct |
34 |
Correct |
17 ms |
27732 KB |
Output is correct |
35 |
Correct |
16 ms |
27772 KB |
Output is correct |
36 |
Correct |
16 ms |
27732 KB |
Output is correct |
37 |
Correct |
16 ms |
27732 KB |
Output is correct |
38 |
Correct |
16 ms |
27748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27704 KB |
Output is correct |
2 |
Correct |
15 ms |
27656 KB |
Output is correct |
3 |
Correct |
15 ms |
27632 KB |
Output is correct |
4 |
Correct |
17 ms |
27732 KB |
Output is correct |
5 |
Correct |
16 ms |
27684 KB |
Output is correct |
6 |
Correct |
15 ms |
27732 KB |
Output is correct |
7 |
Correct |
18 ms |
27732 KB |
Output is correct |
8 |
Correct |
15 ms |
27732 KB |
Output is correct |
9 |
Correct |
16 ms |
27732 KB |
Output is correct |
10 |
Correct |
14 ms |
27732 KB |
Output is correct |
11 |
Correct |
16 ms |
27676 KB |
Output is correct |
12 |
Correct |
15 ms |
27732 KB |
Output is correct |
13 |
Correct |
15 ms |
27732 KB |
Output is correct |
14 |
Correct |
15 ms |
27684 KB |
Output is correct |
15 |
Correct |
15 ms |
27748 KB |
Output is correct |
16 |
Correct |
15 ms |
27636 KB |
Output is correct |
17 |
Correct |
14 ms |
27732 KB |
Output is correct |
18 |
Correct |
15 ms |
27732 KB |
Output is correct |
19 |
Correct |
138 ms |
33416 KB |
Output is correct |
20 |
Correct |
156 ms |
33440 KB |
Output is correct |
21 |
Correct |
147 ms |
33912 KB |
Output is correct |
22 |
Correct |
123 ms |
34064 KB |
Output is correct |
23 |
Correct |
99 ms |
33592 KB |
Output is correct |
24 |
Correct |
89 ms |
33432 KB |
Output is correct |
25 |
Correct |
158 ms |
39920 KB |
Output is correct |
26 |
Correct |
103 ms |
41588 KB |
Output is correct |
27 |
Correct |
179 ms |
39260 KB |
Output is correct |
28 |
Correct |
390 ms |
52532 KB |
Output is correct |
29 |
Correct |
349 ms |
47296 KB |
Output is correct |
30 |
Correct |
196 ms |
39320 KB |
Output is correct |
31 |
Correct |
175 ms |
39256 KB |
Output is correct |
32 |
Correct |
268 ms |
39108 KB |
Output is correct |
33 |
Correct |
250 ms |
38184 KB |
Output is correct |
34 |
Correct |
246 ms |
37932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27704 KB |
Output is correct |
2 |
Correct |
15 ms |
27656 KB |
Output is correct |
3 |
Correct |
15 ms |
27632 KB |
Output is correct |
4 |
Correct |
17 ms |
27732 KB |
Output is correct |
5 |
Correct |
16 ms |
27684 KB |
Output is correct |
6 |
Correct |
15 ms |
27732 KB |
Output is correct |
7 |
Correct |
18 ms |
27732 KB |
Output is correct |
8 |
Correct |
15 ms |
27732 KB |
Output is correct |
9 |
Correct |
16 ms |
27732 KB |
Output is correct |
10 |
Correct |
14 ms |
27732 KB |
Output is correct |
11 |
Correct |
16 ms |
27676 KB |
Output is correct |
12 |
Correct |
15 ms |
27732 KB |
Output is correct |
13 |
Correct |
15 ms |
27732 KB |
Output is correct |
14 |
Correct |
15 ms |
27684 KB |
Output is correct |
15 |
Correct |
15 ms |
27748 KB |
Output is correct |
16 |
Correct |
15 ms |
27636 KB |
Output is correct |
17 |
Correct |
14 ms |
27732 KB |
Output is correct |
18 |
Correct |
15 ms |
27732 KB |
Output is correct |
19 |
Correct |
15 ms |
27728 KB |
Output is correct |
20 |
Correct |
14 ms |
27668 KB |
Output is correct |
21 |
Correct |
19 ms |
27732 KB |
Output is correct |
22 |
Correct |
19 ms |
27796 KB |
Output is correct |
23 |
Correct |
16 ms |
27700 KB |
Output is correct |
24 |
Correct |
15 ms |
27688 KB |
Output is correct |
25 |
Correct |
16 ms |
27812 KB |
Output is correct |
26 |
Correct |
15 ms |
27732 KB |
Output is correct |
27 |
Correct |
16 ms |
27756 KB |
Output is correct |
28 |
Correct |
16 ms |
27732 KB |
Output is correct |
29 |
Correct |
16 ms |
27712 KB |
Output is correct |
30 |
Correct |
16 ms |
27732 KB |
Output is correct |
31 |
Correct |
16 ms |
27732 KB |
Output is correct |
32 |
Correct |
18 ms |
27680 KB |
Output is correct |
33 |
Correct |
17 ms |
27800 KB |
Output is correct |
34 |
Correct |
17 ms |
27732 KB |
Output is correct |
35 |
Correct |
16 ms |
27772 KB |
Output is correct |
36 |
Correct |
16 ms |
27732 KB |
Output is correct |
37 |
Correct |
16 ms |
27732 KB |
Output is correct |
38 |
Correct |
16 ms |
27748 KB |
Output is correct |
39 |
Correct |
138 ms |
33416 KB |
Output is correct |
40 |
Correct |
156 ms |
33440 KB |
Output is correct |
41 |
Correct |
147 ms |
33912 KB |
Output is correct |
42 |
Correct |
123 ms |
34064 KB |
Output is correct |
43 |
Correct |
99 ms |
33592 KB |
Output is correct |
44 |
Correct |
89 ms |
33432 KB |
Output is correct |
45 |
Correct |
158 ms |
39920 KB |
Output is correct |
46 |
Correct |
103 ms |
41588 KB |
Output is correct |
47 |
Correct |
179 ms |
39260 KB |
Output is correct |
48 |
Correct |
390 ms |
52532 KB |
Output is correct |
49 |
Correct |
349 ms |
47296 KB |
Output is correct |
50 |
Correct |
196 ms |
39320 KB |
Output is correct |
51 |
Correct |
175 ms |
39256 KB |
Output is correct |
52 |
Correct |
268 ms |
39108 KB |
Output is correct |
53 |
Correct |
250 ms |
38184 KB |
Output is correct |
54 |
Correct |
246 ms |
37932 KB |
Output is correct |
55 |
Correct |
22 ms |
28372 KB |
Output is correct |
56 |
Correct |
24 ms |
28400 KB |
Output is correct |
57 |
Correct |
94 ms |
34388 KB |
Output is correct |
58 |
Correct |
46 ms |
34500 KB |
Output is correct |
59 |
Correct |
103 ms |
41556 KB |
Output is correct |
60 |
Correct |
381 ms |
49080 KB |
Output is correct |
61 |
Correct |
177 ms |
39396 KB |
Output is correct |
62 |
Correct |
193 ms |
40088 KB |
Output is correct |
63 |
Correct |
272 ms |
40084 KB |
Output is correct |
64 |
Correct |
410 ms |
40588 KB |
Output is correct |
65 |
Correct |
387 ms |
39036 KB |
Output is correct |
66 |
Correct |
419 ms |
51132 KB |
Output is correct |
67 |
Correct |
138 ms |
41244 KB |
Output is correct |
68 |
Correct |
246 ms |
39716 KB |
Output is correct |
69 |
Correct |
239 ms |
39908 KB |
Output is correct |
70 |
Correct |
240 ms |
39276 KB |
Output is correct |