#include"race.h"
#include<bits/stdc++.h>
#define fr first
#define sc second
using namespace std;
int k,inf=2e9,ans=inf,t,res[1000005],mt[1000005],a[200005];
bool vis[200005];
vector<pair<int,int>>g[200005];
void app(int v,int p,int len,int depth){
if(k>len&&mt[k-len]==t)ans=min(ans,depth+res[k-len]);
else if(k==len)ans=min(ans,depth);
if(len>k)return;
for(auto to:g[v])
if(to.fr!=p&&!vis[to.fr])app(to.fr,v,len+to.sc,depth+1);
}
void update(int v,int p,int len,int depth){
if(k>len&&mt[len]!=t){
mt[len]=t;
res[len]=depth;
}else if(k>len)res[len]=min(depth,res[len]);
else return;
for(auto to:g[v])
if(to.fr!=p&&!vis[to.fr])update(to.fr,v,len+to.sc,depth+1);
}
int dfs(int v,int p){
a[v]=1;
for(auto to:g[v])
if(to.fr!=p&&!vis[to.fr])
a[v]+=dfs(to.fr,v);
return a[v];
}
int centroid(int c){
dfs(c,c);
bool flag=true;
int v=c,p=v;
while(flag){
flag=false;
for(auto to:g[v]){
if(to.fr!=p&&!vis[to.fr]&&(a[to.fr]<<1)>=a[c]){
p=v;
v=to.fr;
flag=true;
break;
}
}
}
return v;
}
void go(int v){
v=centroid(v);
vis[v]=1;
t++;
for(auto to:g[v])
if(!vis[to.fr]){
app(to.fr,v,to.sc,1);
update(to.fr,v,to.sc,1);
}
for(auto to:g[v])
if(!vis[to.fr])go(to.fr);
}
int best_path(int N,int K,int H[][2],int L[]){
k=K;
for(int i=0;i+1<N;i++){
g[H[i][0]].push_back({H[i][1],L[i]});
g[H[i][1]].push_back({H[i][0],L[i]});
}
go(0);
if(ans==inf)return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5044 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5044 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
10112 KB |
Output is correct |
23 |
Correct |
9 ms |
9344 KB |
Output is correct |
24 |
Correct |
8 ms |
10880 KB |
Output is correct |
25 |
Correct |
7 ms |
8576 KB |
Output is correct |
26 |
Correct |
6 ms |
7680 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
6656 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7936 KB |
Output is correct |
31 |
Correct |
7 ms |
8704 KB |
Output is correct |
32 |
Correct |
7 ms |
8832 KB |
Output is correct |
33 |
Correct |
8 ms |
10368 KB |
Output is correct |
34 |
Correct |
8 ms |
9600 KB |
Output is correct |
35 |
Correct |
9 ms |
11136 KB |
Output is correct |
36 |
Correct |
9 ms |
11520 KB |
Output is correct |
37 |
Correct |
7 ms |
8960 KB |
Output is correct |
38 |
Correct |
7 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5044 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
163 ms |
10596 KB |
Output is correct |
20 |
Correct |
154 ms |
10360 KB |
Output is correct |
21 |
Correct |
170 ms |
10360 KB |
Output is correct |
22 |
Correct |
183 ms |
10488 KB |
Output is correct |
23 |
Correct |
104 ms |
10616 KB |
Output is correct |
24 |
Correct |
76 ms |
10616 KB |
Output is correct |
25 |
Correct |
135 ms |
12280 KB |
Output is correct |
26 |
Correct |
94 ms |
14584 KB |
Output is correct |
27 |
Correct |
226 ms |
15992 KB |
Output is correct |
28 |
Correct |
326 ms |
23928 KB |
Output is correct |
29 |
Correct |
324 ms |
23352 KB |
Output is correct |
30 |
Correct |
208 ms |
15864 KB |
Output is correct |
31 |
Correct |
208 ms |
15864 KB |
Output is correct |
32 |
Correct |
265 ms |
15992 KB |
Output is correct |
33 |
Correct |
263 ms |
14712 KB |
Output is correct |
34 |
Correct |
242 ms |
14712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
5044 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
10112 KB |
Output is correct |
23 |
Correct |
9 ms |
9344 KB |
Output is correct |
24 |
Correct |
8 ms |
10880 KB |
Output is correct |
25 |
Correct |
7 ms |
8576 KB |
Output is correct |
26 |
Correct |
6 ms |
7680 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
6656 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7936 KB |
Output is correct |
31 |
Correct |
7 ms |
8704 KB |
Output is correct |
32 |
Correct |
7 ms |
8832 KB |
Output is correct |
33 |
Correct |
8 ms |
10368 KB |
Output is correct |
34 |
Correct |
8 ms |
9600 KB |
Output is correct |
35 |
Correct |
9 ms |
11136 KB |
Output is correct |
36 |
Correct |
9 ms |
11520 KB |
Output is correct |
37 |
Correct |
7 ms |
8960 KB |
Output is correct |
38 |
Correct |
7 ms |
7424 KB |
Output is correct |
39 |
Correct |
163 ms |
10596 KB |
Output is correct |
40 |
Correct |
154 ms |
10360 KB |
Output is correct |
41 |
Correct |
170 ms |
10360 KB |
Output is correct |
42 |
Correct |
183 ms |
10488 KB |
Output is correct |
43 |
Correct |
104 ms |
10616 KB |
Output is correct |
44 |
Correct |
76 ms |
10616 KB |
Output is correct |
45 |
Correct |
135 ms |
12280 KB |
Output is correct |
46 |
Correct |
94 ms |
14584 KB |
Output is correct |
47 |
Correct |
226 ms |
15992 KB |
Output is correct |
48 |
Correct |
326 ms |
23928 KB |
Output is correct |
49 |
Correct |
324 ms |
23352 KB |
Output is correct |
50 |
Correct |
208 ms |
15864 KB |
Output is correct |
51 |
Correct |
208 ms |
15864 KB |
Output is correct |
52 |
Correct |
265 ms |
15992 KB |
Output is correct |
53 |
Correct |
263 ms |
14712 KB |
Output is correct |
54 |
Correct |
242 ms |
14712 KB |
Output is correct |
55 |
Correct |
13 ms |
5632 KB |
Output is correct |
56 |
Correct |
14 ms |
5632 KB |
Output is correct |
57 |
Correct |
90 ms |
10616 KB |
Output is correct |
58 |
Correct |
46 ms |
10604 KB |
Output is correct |
59 |
Correct |
103 ms |
15352 KB |
Output is correct |
60 |
Correct |
534 ms |
28408 KB |
Output is correct |
61 |
Correct |
234 ms |
15864 KB |
Output is correct |
62 |
Correct |
247 ms |
15864 KB |
Output is correct |
63 |
Correct |
331 ms |
15992 KB |
Output is correct |
64 |
Correct |
624 ms |
18040 KB |
Output is correct |
65 |
Correct |
296 ms |
15864 KB |
Output is correct |
66 |
Correct |
480 ms |
29560 KB |
Output is correct |
67 |
Correct |
150 ms |
16228 KB |
Output is correct |
68 |
Correct |
325 ms |
21372 KB |
Output is correct |
69 |
Correct |
335 ms |
21668 KB |
Output is correct |
70 |
Correct |
315 ms |
20856 KB |
Output is correct |