#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
cerr<<vars<<" = ";
string delim="";
(...,(cerr<<delim<<values,delim=", "));
cerr<<"\n";
}
#else
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {}
#endif
#define pb push_back
#define sz(x) (int)(x.size())
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> inline void maxa(T& a,T b){a=max(a,b);}
template<typename T> inline void mina(T& a,T b){a=min(a,b);}
const int mxN=2e5+5,mxK=1e6+5;
vector<pll> graph[mxN];
int seen[mxN],sub[mxN],ans=1e9,K,N;
int mp[mxK];
vector<int> path;
int dfs(int at,int p){
sub[at]=1;
for(pll &u:graph[at]){
int i=u.first; int w=u.second;
if(i==p || seen[i])
continue;
sub[at]+=dfs(i,at);
}
return sub[at];
}
int centroid(int at,int p,int s){
for(pll &u:graph[at]){
int i=u.first; int w=u.second;
if(i==p || seen[i])
continue;
if(2*sub[i]>=s)
return centroid(i,at,s);
}
return at;
}
void solve(int at,int p,ll len,int filling,int h){
if(len>K)
return;
if(filling){
path.pb(len);
mina(mp[len],h);
} else{
mina(ans,mp[K-len]+h);
}
for(pll &u:graph[at]){
int i=u.first; int w=u.second;
if(i==p || seen[i])
continue;
solve(i,at,len+w,filling,h+1);
}
}
void build(int at){
int s=dfs(at,-1);
int cent=centroid(at,-1,s);
mp[0]=0;
for(pll &u:graph[cent]){
int i=u.first; int w=u.second;
if(!seen[i]){
solve(i,cent,w,0,1);
solve(i,cent,w,1,1);
}
}
while(sz(path)){
mp[path.back()]=1e9;
path.pop_back();
}
seen[cent]=1;
for(pll &u:graph[cent]){
int i=u.first; int w=u.second;
if(!seen[i])
build(i);
}
}
int best_path(int n,int k,int h[][2],int l[]){
N=n; K=k;
for(int i=0;i<n-1;i++){
graph[h[i][0]].pb({h[i][1],l[i]});
graph[h[i][1]].pb({h[i][0],l[i]});
}
fill(mp,mp+K+1,1e9);
build(0);
return ans==1e9?-1:ans;
}
Compilation message
race.cpp: In function 'int dfs(int, int)':
race.cpp:38:28: warning: unused variable 'w' [-Wunused-variable]
38 | int i=u.first; int w=u.second;
| ^
race.cpp: In function 'int centroid(int, int, int)':
race.cpp:48:28: warning: unused variable 'w' [-Wunused-variable]
48 | int i=u.first; int w=u.second;
| ^
race.cpp: In function 'void build(int)':
race.cpp:91:28: warning: unused variable 'w' [-Wunused-variable]
91 | int i=u.first; int w=u.second;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 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 |
3 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 |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 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 |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 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 |
3 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 |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 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 |
6 ms |
8636 KB |
Output is correct |
23 |
Correct |
6 ms |
8012 KB |
Output is correct |
24 |
Correct |
6 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8268 KB |
Output is correct |
26 |
Correct |
5 ms |
6348 KB |
Output is correct |
27 |
Correct |
6 ms |
8140 KB |
Output is correct |
28 |
Correct |
4 ms |
5836 KB |
Output is correct |
29 |
Correct |
5 ms |
6348 KB |
Output is correct |
30 |
Correct |
5 ms |
6476 KB |
Output is correct |
31 |
Correct |
6 ms |
7628 KB |
Output is correct |
32 |
Correct |
6 ms |
7764 KB |
Output is correct |
33 |
Correct |
6 ms |
8140 KB |
Output is correct |
34 |
Correct |
5 ms |
7372 KB |
Output is correct |
35 |
Correct |
6 ms |
8268 KB |
Output is correct |
36 |
Correct |
7 ms |
8652 KB |
Output is correct |
37 |
Correct |
6 ms |
8140 KB |
Output is correct |
38 |
Correct |
5 ms |
6988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 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 |
3 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 |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
184 ms |
12172 KB |
Output is correct |
20 |
Correct |
161 ms |
12096 KB |
Output is correct |
21 |
Correct |
178 ms |
12296 KB |
Output is correct |
22 |
Correct |
182 ms |
12480 KB |
Output is correct |
23 |
Correct |
85 ms |
12356 KB |
Output is correct |
24 |
Correct |
66 ms |
11568 KB |
Output is correct |
25 |
Correct |
136 ms |
14912 KB |
Output is correct |
26 |
Correct |
109 ms |
18420 KB |
Output is correct |
27 |
Correct |
196 ms |
19596 KB |
Output is correct |
28 |
Correct |
315 ms |
30824 KB |
Output is correct |
29 |
Correct |
317 ms |
29816 KB |
Output is correct |
30 |
Correct |
202 ms |
19664 KB |
Output is correct |
31 |
Correct |
230 ms |
19652 KB |
Output is correct |
32 |
Correct |
271 ms |
19764 KB |
Output is correct |
33 |
Correct |
261 ms |
18372 KB |
Output is correct |
34 |
Correct |
223 ms |
18480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 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 |
3 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 |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 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 |
6 ms |
8636 KB |
Output is correct |
23 |
Correct |
6 ms |
8012 KB |
Output is correct |
24 |
Correct |
6 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8268 KB |
Output is correct |
26 |
Correct |
5 ms |
6348 KB |
Output is correct |
27 |
Correct |
6 ms |
8140 KB |
Output is correct |
28 |
Correct |
4 ms |
5836 KB |
Output is correct |
29 |
Correct |
5 ms |
6348 KB |
Output is correct |
30 |
Correct |
5 ms |
6476 KB |
Output is correct |
31 |
Correct |
6 ms |
7628 KB |
Output is correct |
32 |
Correct |
6 ms |
7764 KB |
Output is correct |
33 |
Correct |
6 ms |
8140 KB |
Output is correct |
34 |
Correct |
5 ms |
7372 KB |
Output is correct |
35 |
Correct |
6 ms |
8268 KB |
Output is correct |
36 |
Correct |
7 ms |
8652 KB |
Output is correct |
37 |
Correct |
6 ms |
8140 KB |
Output is correct |
38 |
Correct |
5 ms |
6988 KB |
Output is correct |
39 |
Correct |
184 ms |
12172 KB |
Output is correct |
40 |
Correct |
161 ms |
12096 KB |
Output is correct |
41 |
Correct |
178 ms |
12296 KB |
Output is correct |
42 |
Correct |
182 ms |
12480 KB |
Output is correct |
43 |
Correct |
85 ms |
12356 KB |
Output is correct |
44 |
Correct |
66 ms |
11568 KB |
Output is correct |
45 |
Correct |
136 ms |
14912 KB |
Output is correct |
46 |
Correct |
109 ms |
18420 KB |
Output is correct |
47 |
Correct |
196 ms |
19596 KB |
Output is correct |
48 |
Correct |
315 ms |
30824 KB |
Output is correct |
49 |
Correct |
317 ms |
29816 KB |
Output is correct |
50 |
Correct |
202 ms |
19664 KB |
Output is correct |
51 |
Correct |
230 ms |
19652 KB |
Output is correct |
52 |
Correct |
271 ms |
19764 KB |
Output is correct |
53 |
Correct |
261 ms |
18372 KB |
Output is correct |
54 |
Correct |
223 ms |
18480 KB |
Output is correct |
55 |
Correct |
12 ms |
5772 KB |
Output is correct |
56 |
Correct |
13 ms |
5788 KB |
Output is correct |
57 |
Correct |
91 ms |
12960 KB |
Output is correct |
58 |
Correct |
41 ms |
12548 KB |
Output is correct |
59 |
Correct |
109 ms |
19136 KB |
Output is correct |
60 |
Correct |
569 ms |
34992 KB |
Output is correct |
61 |
Correct |
217 ms |
19860 KB |
Output is correct |
62 |
Correct |
247 ms |
24496 KB |
Output is correct |
63 |
Correct |
324 ms |
24376 KB |
Output is correct |
64 |
Correct |
693 ms |
21988 KB |
Output is correct |
65 |
Correct |
289 ms |
19180 KB |
Output is correct |
66 |
Correct |
502 ms |
31708 KB |
Output is correct |
67 |
Correct |
142 ms |
23976 KB |
Output is correct |
68 |
Correct |
335 ms |
23532 KB |
Output is correct |
69 |
Correct |
338 ms |
23288 KB |
Output is correct |
70 |
Correct |
293 ms |
22856 KB |
Output is correct |