#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define mp make_pair
#define pb push_back
int n,k,res;
vector<pair<int,int> > adj[200001];
int sub[200001],lvl[200001],par[200001];
int ans[1000001];
stack<int> st;
int dfs1(int u,int p,int l){
sub[u]=1;
for (auto i:adj[u]){
if (lvl[i.first]!=-1) continue;
if (i.first==p) continue;
sub[u]+=dfs1(i.first,u,l);
}
return sub[u];
}
int dfs2(int u,int p,int sn){
for (auto i:adj[u]){
if (lvl[i.first]!=-1) continue;
if (i.first==p) continue;
if (sub[i.first]>sn/2){
return dfs2(i.first,u,sn);
}
}
return u;
}
vector<pair<int,int> > branch;
void dfs3(int u,int p,long long dist,int edges){
//cout<<dist<<" "<<edges<<'\n';
if (dist>k){
return;
}
branch.pb(mp(dist,edges));
st.push(dist);
for (auto i:adj[u]){
if (lvl[i.first]!=-1) continue;
if (i.first==p) continue;
dfs3(i.first,u,dist+i.second,edges+1);
if (p==-1){
branch.pb(mp(dist,edges));
for (auto j:branch){
res=min(res,ans[k-j.first]+j.second);
}
for (auto j:branch){
ans[j.first]=min(ans[j.first],j.second);
}
branch.clear();
}
}
}
// Building from node u, with a parent p and level l
void build(int u,int p,int l){
int cn=dfs1(u,p,l);
int cent=dfs2(u,p,cn);
if (p==-1) p=cent;
par[cent]=p;
lvl[cent]=l;
//cout<<"centroid: "<<cent<<'\n';
dfs3(cent,-1,0,0);
while (!st.empty()){
int cur=st.top();
ans[cur]=1e9;
st.pop();
}
for (auto i:adj[cent]){
if (lvl[i.first]!=-1) continue;
build(i.first,cent,l+1);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n=N;
k=K;
for (int i=0;i<n-1;i++){
adj[H[i][0]].pb(mp(H[i][1],L[i]));
adj[H[i][1]].pb(mp(H[i][0],L[i]));
}
res=1e9;
for (int i=0;i<=k;i++) ans[i]=1e9;
memset(lvl,-1,sizeof(lvl));
build(0,-1,0);
if (res==1e9) return -1;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5880 KB |
Output is correct |
2 |
Correct |
7 ms |
5880 KB |
Output is correct |
3 |
Correct |
7 ms |
5880 KB |
Output is correct |
4 |
Correct |
8 ms |
5880 KB |
Output is correct |
5 |
Correct |
7 ms |
5880 KB |
Output is correct |
6 |
Correct |
7 ms |
5880 KB |
Output is correct |
7 |
Correct |
7 ms |
5880 KB |
Output is correct |
8 |
Correct |
7 ms |
5880 KB |
Output is correct |
9 |
Correct |
8 ms |
5876 KB |
Output is correct |
10 |
Correct |
7 ms |
5756 KB |
Output is correct |
11 |
Correct |
7 ms |
5880 KB |
Output is correct |
12 |
Correct |
7 ms |
5880 KB |
Output is correct |
13 |
Correct |
8 ms |
5872 KB |
Output is correct |
14 |
Correct |
7 ms |
5880 KB |
Output is correct |
15 |
Correct |
7 ms |
5880 KB |
Output is correct |
16 |
Correct |
7 ms |
5880 KB |
Output is correct |
17 |
Correct |
7 ms |
5880 KB |
Output is correct |
18 |
Correct |
7 ms |
5880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5880 KB |
Output is correct |
2 |
Correct |
7 ms |
5880 KB |
Output is correct |
3 |
Correct |
7 ms |
5880 KB |
Output is correct |
4 |
Correct |
8 ms |
5880 KB |
Output is correct |
5 |
Correct |
7 ms |
5880 KB |
Output is correct |
6 |
Correct |
7 ms |
5880 KB |
Output is correct |
7 |
Correct |
7 ms |
5880 KB |
Output is correct |
8 |
Correct |
7 ms |
5880 KB |
Output is correct |
9 |
Correct |
8 ms |
5876 KB |
Output is correct |
10 |
Correct |
7 ms |
5756 KB |
Output is correct |
11 |
Correct |
7 ms |
5880 KB |
Output is correct |
12 |
Correct |
7 ms |
5880 KB |
Output is correct |
13 |
Correct |
8 ms |
5872 KB |
Output is correct |
14 |
Correct |
7 ms |
5880 KB |
Output is correct |
15 |
Correct |
7 ms |
5880 KB |
Output is correct |
16 |
Correct |
7 ms |
5880 KB |
Output is correct |
17 |
Correct |
7 ms |
5880 KB |
Output is correct |
18 |
Correct |
7 ms |
5880 KB |
Output is correct |
19 |
Correct |
7 ms |
5752 KB |
Output is correct |
20 |
Correct |
7 ms |
5880 KB |
Output is correct |
21 |
Correct |
8 ms |
5880 KB |
Output is correct |
22 |
Correct |
11 ms |
9464 KB |
Output is correct |
23 |
Correct |
11 ms |
8876 KB |
Output is correct |
24 |
Correct |
11 ms |
9336 KB |
Output is correct |
25 |
Correct |
11 ms |
9208 KB |
Output is correct |
26 |
Correct |
9 ms |
7160 KB |
Output is correct |
27 |
Correct |
11 ms |
9076 KB |
Output is correct |
28 |
Correct |
9 ms |
6648 KB |
Output is correct |
29 |
Correct |
9 ms |
7160 KB |
Output is correct |
30 |
Correct |
9 ms |
7420 KB |
Output is correct |
31 |
Correct |
11 ms |
8440 KB |
Output is correct |
32 |
Correct |
11 ms |
8700 KB |
Output is correct |
33 |
Correct |
11 ms |
8952 KB |
Output is correct |
34 |
Correct |
11 ms |
8184 KB |
Output is correct |
35 |
Correct |
12 ms |
9080 KB |
Output is correct |
36 |
Correct |
12 ms |
9592 KB |
Output is correct |
37 |
Correct |
11 ms |
8952 KB |
Output is correct |
38 |
Correct |
10 ms |
7928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5880 KB |
Output is correct |
2 |
Correct |
7 ms |
5880 KB |
Output is correct |
3 |
Correct |
7 ms |
5880 KB |
Output is correct |
4 |
Correct |
8 ms |
5880 KB |
Output is correct |
5 |
Correct |
7 ms |
5880 KB |
Output is correct |
6 |
Correct |
7 ms |
5880 KB |
Output is correct |
7 |
Correct |
7 ms |
5880 KB |
Output is correct |
8 |
Correct |
7 ms |
5880 KB |
Output is correct |
9 |
Correct |
8 ms |
5876 KB |
Output is correct |
10 |
Correct |
7 ms |
5756 KB |
Output is correct |
11 |
Correct |
7 ms |
5880 KB |
Output is correct |
12 |
Correct |
7 ms |
5880 KB |
Output is correct |
13 |
Correct |
8 ms |
5872 KB |
Output is correct |
14 |
Correct |
7 ms |
5880 KB |
Output is correct |
15 |
Correct |
7 ms |
5880 KB |
Output is correct |
16 |
Correct |
7 ms |
5880 KB |
Output is correct |
17 |
Correct |
7 ms |
5880 KB |
Output is correct |
18 |
Correct |
7 ms |
5880 KB |
Output is correct |
19 |
Correct |
166 ms |
12972 KB |
Output is correct |
20 |
Correct |
166 ms |
12880 KB |
Output is correct |
21 |
Correct |
175 ms |
13208 KB |
Output is correct |
22 |
Correct |
163 ms |
13608 KB |
Output is correct |
23 |
Correct |
134 ms |
13172 KB |
Output is correct |
24 |
Correct |
92 ms |
13048 KB |
Output is correct |
25 |
Correct |
202 ms |
15712 KB |
Output is correct |
26 |
Correct |
131 ms |
19516 KB |
Output is correct |
27 |
Correct |
233 ms |
20316 KB |
Output is correct |
28 |
Correct |
446 ms |
31760 KB |
Output is correct |
29 |
Correct |
465 ms |
30712 KB |
Output is correct |
30 |
Correct |
238 ms |
20328 KB |
Output is correct |
31 |
Correct |
238 ms |
20216 KB |
Output is correct |
32 |
Correct |
287 ms |
20344 KB |
Output is correct |
33 |
Correct |
362 ms |
19064 KB |
Output is correct |
34 |
Correct |
349 ms |
19964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5880 KB |
Output is correct |
2 |
Correct |
7 ms |
5880 KB |
Output is correct |
3 |
Correct |
7 ms |
5880 KB |
Output is correct |
4 |
Correct |
8 ms |
5880 KB |
Output is correct |
5 |
Correct |
7 ms |
5880 KB |
Output is correct |
6 |
Correct |
7 ms |
5880 KB |
Output is correct |
7 |
Correct |
7 ms |
5880 KB |
Output is correct |
8 |
Correct |
7 ms |
5880 KB |
Output is correct |
9 |
Correct |
8 ms |
5876 KB |
Output is correct |
10 |
Correct |
7 ms |
5756 KB |
Output is correct |
11 |
Correct |
7 ms |
5880 KB |
Output is correct |
12 |
Correct |
7 ms |
5880 KB |
Output is correct |
13 |
Correct |
8 ms |
5872 KB |
Output is correct |
14 |
Correct |
7 ms |
5880 KB |
Output is correct |
15 |
Correct |
7 ms |
5880 KB |
Output is correct |
16 |
Correct |
7 ms |
5880 KB |
Output is correct |
17 |
Correct |
7 ms |
5880 KB |
Output is correct |
18 |
Correct |
7 ms |
5880 KB |
Output is correct |
19 |
Correct |
7 ms |
5752 KB |
Output is correct |
20 |
Correct |
7 ms |
5880 KB |
Output is correct |
21 |
Correct |
8 ms |
5880 KB |
Output is correct |
22 |
Correct |
11 ms |
9464 KB |
Output is correct |
23 |
Correct |
11 ms |
8876 KB |
Output is correct |
24 |
Correct |
11 ms |
9336 KB |
Output is correct |
25 |
Correct |
11 ms |
9208 KB |
Output is correct |
26 |
Correct |
9 ms |
7160 KB |
Output is correct |
27 |
Correct |
11 ms |
9076 KB |
Output is correct |
28 |
Correct |
9 ms |
6648 KB |
Output is correct |
29 |
Correct |
9 ms |
7160 KB |
Output is correct |
30 |
Correct |
9 ms |
7420 KB |
Output is correct |
31 |
Correct |
11 ms |
8440 KB |
Output is correct |
32 |
Correct |
11 ms |
8700 KB |
Output is correct |
33 |
Correct |
11 ms |
8952 KB |
Output is correct |
34 |
Correct |
11 ms |
8184 KB |
Output is correct |
35 |
Correct |
12 ms |
9080 KB |
Output is correct |
36 |
Correct |
12 ms |
9592 KB |
Output is correct |
37 |
Correct |
11 ms |
8952 KB |
Output is correct |
38 |
Correct |
10 ms |
7928 KB |
Output is correct |
39 |
Correct |
166 ms |
12972 KB |
Output is correct |
40 |
Correct |
166 ms |
12880 KB |
Output is correct |
41 |
Correct |
175 ms |
13208 KB |
Output is correct |
42 |
Correct |
163 ms |
13608 KB |
Output is correct |
43 |
Correct |
134 ms |
13172 KB |
Output is correct |
44 |
Correct |
92 ms |
13048 KB |
Output is correct |
45 |
Correct |
202 ms |
15712 KB |
Output is correct |
46 |
Correct |
131 ms |
19516 KB |
Output is correct |
47 |
Correct |
233 ms |
20316 KB |
Output is correct |
48 |
Correct |
446 ms |
31760 KB |
Output is correct |
49 |
Correct |
465 ms |
30712 KB |
Output is correct |
50 |
Correct |
238 ms |
20328 KB |
Output is correct |
51 |
Correct |
238 ms |
20216 KB |
Output is correct |
52 |
Correct |
287 ms |
20344 KB |
Output is correct |
53 |
Correct |
362 ms |
19064 KB |
Output is correct |
54 |
Correct |
349 ms |
19964 KB |
Output is correct |
55 |
Correct |
19 ms |
6548 KB |
Output is correct |
56 |
Correct |
19 ms |
6540 KB |
Output is correct |
57 |
Correct |
109 ms |
13836 KB |
Output is correct |
58 |
Correct |
54 ms |
14700 KB |
Output is correct |
59 |
Correct |
132 ms |
20028 KB |
Output is correct |
60 |
Correct |
713 ms |
36428 KB |
Output is correct |
61 |
Correct |
246 ms |
20600 KB |
Output is correct |
62 |
Correct |
269 ms |
24860 KB |
Output is correct |
63 |
Correct |
362 ms |
24980 KB |
Output is correct |
64 |
Correct |
581 ms |
24196 KB |
Output is correct |
65 |
Correct |
321 ms |
21344 KB |
Output is correct |
66 |
Correct |
584 ms |
32716 KB |
Output is correct |
67 |
Correct |
199 ms |
25684 KB |
Output is correct |
68 |
Correct |
354 ms |
25300 KB |
Output is correct |
69 |
Correct |
361 ms |
25472 KB |
Output is correct |
70 |
Correct |
341 ms |
24688 KB |
Output is correct |