#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
vector<pii> ch[200005],vec;
vector<int> st;
set<int> se;
int sz[200005],cent[200005],dp[1000005],ret=1e6,l;
int getsz(int n,int pa){
sz[n]=1;
for(auto a : ch[n]){
if(a.fi==pa||cent[a.fi])continue;
sz[n]+=getsz(a.fi,n);
}
return sz[n];
}
int getcent(int now,int pa,int cap){
int b,m=cap-sz[now];
for(auto a : ch[now]){
if(a.fi==pa||cent[a.fi])continue;
m=max(m,sz[a.fi]);
b=getcent(a.fi,now,cap);
if(b)return b;
}
if(m*2<=cap)return now;
return 0;
}
int adjmax(int now,int pa){
int k=0;
for(auto a : ch[now]){
if(a.fi==pa)continue;
if(cent[a.fi])k=max(k,cent[a.fi]);
else k=max(k,adjmax(a.fi,now));
}
return k;
}
void dfs(int now,int pa,int th,int w,int dis){
if(w>l)return;
st.push_back(w);
if(l>=w&&dp[l-w]!=-1)ret=min(ret,dp[l-w]+dis);
for(auto a : ch[now]){
if(a.fi==pa)continue;
if(cent[a.fi]<=th)continue;
dfs(a.fi,now,th,w+a.se,dis+1);
}
vec.push_back({w,dis});
}
int best_path(int N,int K,int H[][2],int L[]){
int i,j,k,a,b,c,n;
//scanf("%lld%lld",&n,&l);
n=N;l=K;
for(i=0;i<n-1;i++){
//scanf("%lld%lld%lld",&a,&b,&c);
//a++;b++;
a=H[i][0];b=H[i][1];c=L[i];
a++;b++;
ch[a].push_back({b,c});
ch[b].push_back({a,c});
}
for(i=1;i<=n;i++){
se.insert(i);
}
while(!se.empty()){
a=*se.begin();
k=getsz(a,0);
b=getcent(a,0,k);
cent[b]=adjmax(a,0)+1;
//printf("%lld %lld\n",b,cent[b]);
se.erase(se.find(b));
}
for(i=1;i<=l;i++)dp[i]=-1;
for(i=1;i<=n;i++){
for(auto t : ch[i]){
if(cent[t.fi]<=cent[i])continue;
dfs(t.fi,i,cent[i],t.se,1);
while(vec.size()){
if(dp[vec.back().fi]==-1)dp[vec.back().fi]=vec.back().se;
else dp[vec.back().fi]=min(dp[vec.back().fi],vec.back().se);
vec.pop_back();
}
}
while(st.size()){
dp[st.back()]=-1;
st.pop_back();
}
}
if(ret==1e6)return -1;
return ret;
}
Compilation message
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:58:11: warning: unused variable 'j' [-Wunused-variable]
58 | int i,j,k,a,b,c,n;
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
5 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 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 |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 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 |
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 |
5 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 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 |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 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 |
5 ms |
8652 KB |
Output is correct |
23 |
Correct |
5 ms |
8012 KB |
Output is correct |
24 |
Correct |
6 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8524 KB |
Output is correct |
26 |
Correct |
5 ms |
6348 KB |
Output is correct |
27 |
Correct |
5 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 |
5 ms |
7628 KB |
Output is correct |
32 |
Correct |
6 ms |
7884 KB |
Output is correct |
33 |
Correct |
6 ms |
8140 KB |
Output is correct |
34 |
Correct |
5 ms |
7348 KB |
Output is correct |
35 |
Correct |
6 ms |
8184 KB |
Output is correct |
36 |
Correct |
6 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 |
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 |
5 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 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 |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 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 |
278 ms |
15428 KB |
Output is correct |
20 |
Correct |
320 ms |
15324 KB |
Output is correct |
21 |
Correct |
295 ms |
15252 KB |
Output is correct |
22 |
Correct |
308 ms |
15392 KB |
Output is correct |
23 |
Correct |
196 ms |
15576 KB |
Output is correct |
24 |
Correct |
141 ms |
15428 KB |
Output is correct |
25 |
Correct |
302 ms |
18156 KB |
Output is correct |
26 |
Correct |
165 ms |
21060 KB |
Output is correct |
27 |
Correct |
298 ms |
25784 KB |
Output is correct |
28 |
Correct |
745 ms |
37040 KB |
Output is correct |
29 |
Correct |
705 ms |
36116 KB |
Output is correct |
30 |
Correct |
312 ms |
25784 KB |
Output is correct |
31 |
Correct |
317 ms |
25904 KB |
Output is correct |
32 |
Correct |
531 ms |
25772 KB |
Output is correct |
33 |
Correct |
680 ms |
24644 KB |
Output is correct |
34 |
Correct |
547 ms |
24640 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 |
5 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 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 |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 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 |
5 ms |
8652 KB |
Output is correct |
23 |
Correct |
5 ms |
8012 KB |
Output is correct |
24 |
Correct |
6 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8524 KB |
Output is correct |
26 |
Correct |
5 ms |
6348 KB |
Output is correct |
27 |
Correct |
5 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 |
5 ms |
7628 KB |
Output is correct |
32 |
Correct |
6 ms |
7884 KB |
Output is correct |
33 |
Correct |
6 ms |
8140 KB |
Output is correct |
34 |
Correct |
5 ms |
7348 KB |
Output is correct |
35 |
Correct |
6 ms |
8184 KB |
Output is correct |
36 |
Correct |
6 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 |
278 ms |
15428 KB |
Output is correct |
40 |
Correct |
320 ms |
15324 KB |
Output is correct |
41 |
Correct |
295 ms |
15252 KB |
Output is correct |
42 |
Correct |
308 ms |
15392 KB |
Output is correct |
43 |
Correct |
196 ms |
15576 KB |
Output is correct |
44 |
Correct |
141 ms |
15428 KB |
Output is correct |
45 |
Correct |
302 ms |
18156 KB |
Output is correct |
46 |
Correct |
165 ms |
21060 KB |
Output is correct |
47 |
Correct |
298 ms |
25784 KB |
Output is correct |
48 |
Correct |
745 ms |
37040 KB |
Output is correct |
49 |
Correct |
705 ms |
36116 KB |
Output is correct |
50 |
Correct |
312 ms |
25784 KB |
Output is correct |
51 |
Correct |
317 ms |
25904 KB |
Output is correct |
52 |
Correct |
531 ms |
25772 KB |
Output is correct |
53 |
Correct |
680 ms |
24644 KB |
Output is correct |
54 |
Correct |
547 ms |
24640 KB |
Output is correct |
55 |
Correct |
15 ms |
5964 KB |
Output is correct |
56 |
Correct |
18 ms |
5988 KB |
Output is correct |
57 |
Correct |
138 ms |
16800 KB |
Output is correct |
58 |
Correct |
68 ms |
16536 KB |
Output is correct |
59 |
Correct |
169 ms |
23056 KB |
Output is correct |
60 |
Correct |
944 ms |
43348 KB |
Output is correct |
61 |
Correct |
314 ms |
28872 KB |
Output is correct |
62 |
Correct |
327 ms |
32616 KB |
Output is correct |
63 |
Correct |
665 ms |
32620 KB |
Output is correct |
64 |
Correct |
1035 ms |
31112 KB |
Output is correct |
65 |
Correct |
572 ms |
29664 KB |
Output is correct |
66 |
Correct |
900 ms |
41176 KB |
Output is correct |
67 |
Correct |
218 ms |
33344 KB |
Output is correct |
68 |
Correct |
555 ms |
33416 KB |
Output is correct |
69 |
Correct |
571 ms |
33644 KB |
Output is correct |
70 |
Correct |
545 ms |
32376 KB |
Output is correct |