#include"race.h"
#include<bits/stdc++.h>
#ifdef IOI2021SG
#include"grader.cpp"
#endif
using namespace std;
#define fr first
#define sc second
vector<pair<int,int>>g[200005];
const int INF=2e9;
int k,ans=INF;
int sz[200005];
bool vis[200005];
pair<int,int>mt[1000005];
int timer;
void calc_sz(int v,int p){
sz[v]=1;
for(auto to:g[v])if(to.fr!=p&&!vis[to.fr]){
calc_sz(to.fr,v);
sz[v]+=sz[to.fr];
}
}
int centroid(int v,int p,int num){
for(auto to:g[v])if(to.fr!=p&&!vis[to.fr])
if(sz[to.fr]>num)return centroid(to.fr,v,num);
return v;
}
void go(int v,int p,int depth,int weight){
if(weight>k)return;
if(mt[k-weight].fr==timer)
ans=min(ans,depth+mt[k-weight].sc);
for(auto to:g[v])if(to.fr!=p&&!vis[to.fr])go(to.fr,v,depth+1,weight+to.sc);
}
void update(int v,int p,int depth,int weight){
if(weight>k)return;
if(mt[weight].fr==timer)mt[weight].sc=min(mt[weight].sc,depth);
else mt[weight]={timer,depth};
for(auto to:g[v])if(to.fr!=p&&!vis[to.fr])update(to.fr,v,depth+1,weight+to.sc);
}
void decompose(int v){
calc_sz(v,v);
v=centroid(v,v,sz[v]>>1);
timer++;
mt[0]={timer,0};
for(auto to:g[v]){
if(vis[to.fr])continue;
go(to.fr,v,1,to.sc);
update(to.fr,v,1,to.sc);
}
vis[v]=true;
for(auto to:g[v])if(!vis[to.fr])decompose(to.fr);
}
int best_path(int n,int K,int h[][2],int l[]){
k=K;
for(int i=1;i^n;i++){
g[h[i-1][1]].push_back({h[i-1][0],l[i-1]});
g[h[i-1][0]].push_back({h[i-1][1],l[i-1]});
}
decompose(0);
if(ans==INF)return -1;
return ans;
}
/*
3 3
0 1 1
1 2 1
-1
4 3
0 1 1
1 2 2
1 3 4
2
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
5032 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
5032 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
7 ms |
8396 KB |
Output is correct |
23 |
Correct |
6 ms |
7884 KB |
Output is correct |
24 |
Correct |
7 ms |
9164 KB |
Output is correct |
25 |
Correct |
7 ms |
8140 KB |
Output is correct |
26 |
Correct |
5 ms |
7372 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
6604 KB |
Output is correct |
29 |
Correct |
6 ms |
7508 KB |
Output is correct |
30 |
Correct |
6 ms |
7756 KB |
Output is correct |
31 |
Correct |
7 ms |
8396 KB |
Output is correct |
32 |
Correct |
7 ms |
8396 KB |
Output is correct |
33 |
Correct |
8 ms |
9804 KB |
Output is correct |
34 |
Correct |
7 ms |
9036 KB |
Output is correct |
35 |
Correct |
8 ms |
9936 KB |
Output is correct |
36 |
Correct |
8 ms |
10444 KB |
Output is correct |
37 |
Correct |
7 ms |
8268 KB |
Output is correct |
38 |
Correct |
6 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
5032 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
141 ms |
10288 KB |
Output is correct |
20 |
Correct |
132 ms |
10240 KB |
Output is correct |
21 |
Correct |
149 ms |
10256 KB |
Output is correct |
22 |
Correct |
139 ms |
10384 KB |
Output is correct |
23 |
Correct |
78 ms |
10580 KB |
Output is correct |
24 |
Correct |
61 ms |
10436 KB |
Output is correct |
25 |
Correct |
127 ms |
13912 KB |
Output is correct |
26 |
Correct |
96 ms |
17696 KB |
Output is correct |
27 |
Correct |
181 ms |
15884 KB |
Output is correct |
28 |
Correct |
306 ms |
30148 KB |
Output is correct |
29 |
Correct |
282 ms |
28992 KB |
Output is correct |
30 |
Correct |
181 ms |
15876 KB |
Output is correct |
31 |
Correct |
194 ms |
15812 KB |
Output is correct |
32 |
Correct |
231 ms |
15956 KB |
Output is correct |
33 |
Correct |
218 ms |
14784 KB |
Output is correct |
34 |
Correct |
198 ms |
14744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
5032 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
7 ms |
8396 KB |
Output is correct |
23 |
Correct |
6 ms |
7884 KB |
Output is correct |
24 |
Correct |
7 ms |
9164 KB |
Output is correct |
25 |
Correct |
7 ms |
8140 KB |
Output is correct |
26 |
Correct |
5 ms |
7372 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
6604 KB |
Output is correct |
29 |
Correct |
6 ms |
7508 KB |
Output is correct |
30 |
Correct |
6 ms |
7756 KB |
Output is correct |
31 |
Correct |
7 ms |
8396 KB |
Output is correct |
32 |
Correct |
7 ms |
8396 KB |
Output is correct |
33 |
Correct |
8 ms |
9804 KB |
Output is correct |
34 |
Correct |
7 ms |
9036 KB |
Output is correct |
35 |
Correct |
8 ms |
9936 KB |
Output is correct |
36 |
Correct |
8 ms |
10444 KB |
Output is correct |
37 |
Correct |
7 ms |
8268 KB |
Output is correct |
38 |
Correct |
6 ms |
7252 KB |
Output is correct |
39 |
Correct |
141 ms |
10288 KB |
Output is correct |
40 |
Correct |
132 ms |
10240 KB |
Output is correct |
41 |
Correct |
149 ms |
10256 KB |
Output is correct |
42 |
Correct |
139 ms |
10384 KB |
Output is correct |
43 |
Correct |
78 ms |
10580 KB |
Output is correct |
44 |
Correct |
61 ms |
10436 KB |
Output is correct |
45 |
Correct |
127 ms |
13912 KB |
Output is correct |
46 |
Correct |
96 ms |
17696 KB |
Output is correct |
47 |
Correct |
181 ms |
15884 KB |
Output is correct |
48 |
Correct |
306 ms |
30148 KB |
Output is correct |
49 |
Correct |
282 ms |
28992 KB |
Output is correct |
50 |
Correct |
181 ms |
15876 KB |
Output is correct |
51 |
Correct |
194 ms |
15812 KB |
Output is correct |
52 |
Correct |
231 ms |
15956 KB |
Output is correct |
53 |
Correct |
218 ms |
14784 KB |
Output is correct |
54 |
Correct |
198 ms |
14744 KB |
Output is correct |
55 |
Correct |
12 ms |
5556 KB |
Output is correct |
56 |
Correct |
13 ms |
5568 KB |
Output is correct |
57 |
Correct |
82 ms |
10560 KB |
Output is correct |
58 |
Correct |
41 ms |
10492 KB |
Output is correct |
59 |
Correct |
98 ms |
19724 KB |
Output is correct |
60 |
Correct |
464 ms |
36212 KB |
Output is correct |
61 |
Correct |
197 ms |
17860 KB |
Output is correct |
62 |
Correct |
214 ms |
17860 KB |
Output is correct |
63 |
Correct |
290 ms |
17992 KB |
Output is correct |
64 |
Correct |
524 ms |
19872 KB |
Output is correct |
65 |
Correct |
267 ms |
17808 KB |
Output is correct |
66 |
Correct |
429 ms |
36220 KB |
Output is correct |
67 |
Correct |
127 ms |
18140 KB |
Output is correct |
68 |
Correct |
300 ms |
23192 KB |
Output is correct |
69 |
Correct |
298 ms |
23484 KB |
Output is correct |
70 |
Correct |
277 ms |
22668 KB |
Output is correct |