#include "race.h"
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> >adj[200005];
int sz[200005];
int used[200005];
long long cnt[1000005];
int inf=1e9;
long long ans=inf;
vector<pair<int,long long> >v[200005];
int n,k;
int dfssz(int u,int p=-1){
sz[u]=1;
for(auto x:adj[u])if(!used[x.first]&&x.first!=p)sz[u]+=dfssz(x.first,u);
return sz[u];
}
int centroid(int u,int rsz,int p=-1){
for(auto x:adj[u])if(!used[x.first]&&x.first!=p&&sz[x.first]>rsz/2)return centroid(x.first,rsz,u);
return u;
}
void dfs1(int u,int d,int l,int rt,int p=-1){
if(d<=k)v[rt].push_back({d,l});else return;
for(auto x:adj[u])if(!used[x.first]&&x.first!=p)dfs1(x.first,d+x.second,l+1,rt,u);
}
void decom(int u){
u=centroid(u,dfssz(u));
//cerr<<"u:"<<u<<endl;
used[u]=1;
for(auto x:adj[u])if(!used[x.first]){
dfs1(x.first,x.second,1,x.first);
for(auto d:v[x.first])if(d.first<=k)ans=min(ans,cnt[k-d.first]+d.second);
for(auto d:v[x.first])if(d.first<=k)cnt[d.first]=min(cnt[d.first],d.second);
}
for(auto x:adj[u])if(!used[x.first]){
for(auto d:v[x.first])if(d.first<=k)cnt[d.first]=inf;
v[x.first].clear();
}
for(auto x:adj[u])if(!used[x.first])decom(x.first);
}
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]].push_back({H[i][1],L[i]});
adj[H[i][1]].push_back({H[i][0],L[i]});
}
for(int i=0;i<=K;i++)cnt[i]=inf;
cnt[0]=0;
decom(1);
return ans==inf?-1:ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16476 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16476 KB |
Output is correct |
8 |
Correct |
3 ms |
16476 KB |
Output is correct |
9 |
Correct |
3 ms |
16568 KB |
Output is correct |
10 |
Correct |
5 ms |
16728 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
3 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16684 KB |
Output is correct |
17 |
Correct |
3 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16476 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16476 KB |
Output is correct |
8 |
Correct |
3 ms |
16476 KB |
Output is correct |
9 |
Correct |
3 ms |
16568 KB |
Output is correct |
10 |
Correct |
5 ms |
16728 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
3 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16684 KB |
Output is correct |
17 |
Correct |
3 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16732 KB |
Output is correct |
19 |
Correct |
3 ms |
16476 KB |
Output is correct |
20 |
Correct |
3 ms |
16732 KB |
Output is correct |
21 |
Correct |
4 ms |
16728 KB |
Output is correct |
22 |
Correct |
6 ms |
22860 KB |
Output is correct |
23 |
Correct |
5 ms |
20828 KB |
Output is correct |
24 |
Correct |
5 ms |
22876 KB |
Output is correct |
25 |
Correct |
5 ms |
22876 KB |
Output is correct |
26 |
Correct |
4 ms |
18776 KB |
Output is correct |
27 |
Correct |
5 ms |
22876 KB |
Output is correct |
28 |
Correct |
4 ms |
16732 KB |
Output is correct |
29 |
Correct |
4 ms |
18768 KB |
Output is correct |
30 |
Correct |
4 ms |
18764 KB |
Output is correct |
31 |
Correct |
5 ms |
20824 KB |
Output is correct |
32 |
Correct |
5 ms |
20828 KB |
Output is correct |
33 |
Correct |
5 ms |
23040 KB |
Output is correct |
34 |
Correct |
4 ms |
20828 KB |
Output is correct |
35 |
Correct |
5 ms |
22876 KB |
Output is correct |
36 |
Correct |
5 ms |
22876 KB |
Output is correct |
37 |
Correct |
5 ms |
22872 KB |
Output is correct |
38 |
Correct |
4 ms |
20828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16476 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16476 KB |
Output is correct |
8 |
Correct |
3 ms |
16476 KB |
Output is correct |
9 |
Correct |
3 ms |
16568 KB |
Output is correct |
10 |
Correct |
5 ms |
16728 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
3 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16684 KB |
Output is correct |
17 |
Correct |
3 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16732 KB |
Output is correct |
19 |
Correct |
103 ms |
38744 KB |
Output is correct |
20 |
Correct |
96 ms |
38156 KB |
Output is correct |
21 |
Correct |
98 ms |
40248 KB |
Output is correct |
22 |
Correct |
94 ms |
38344 KB |
Output is correct |
23 |
Correct |
62 ms |
29072 KB |
Output is correct |
24 |
Correct |
54 ms |
29008 KB |
Output is correct |
25 |
Correct |
104 ms |
49748 KB |
Output is correct |
26 |
Correct |
92 ms |
58536 KB |
Output is correct |
27 |
Correct |
130 ms |
41020 KB |
Output is correct |
28 |
Correct |
202 ms |
45532 KB |
Output is correct |
29 |
Correct |
173 ms |
41188 KB |
Output is correct |
30 |
Correct |
131 ms |
41096 KB |
Output is correct |
31 |
Correct |
135 ms |
41328 KB |
Output is correct |
32 |
Correct |
157 ms |
39508 KB |
Output is correct |
33 |
Correct |
172 ms |
56144 KB |
Output is correct |
34 |
Correct |
150 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16728 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16476 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16476 KB |
Output is correct |
8 |
Correct |
3 ms |
16476 KB |
Output is correct |
9 |
Correct |
3 ms |
16568 KB |
Output is correct |
10 |
Correct |
5 ms |
16728 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
3 ms |
16728 KB |
Output is correct |
14 |
Correct |
3 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16684 KB |
Output is correct |
17 |
Correct |
3 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16732 KB |
Output is correct |
19 |
Correct |
3 ms |
16476 KB |
Output is correct |
20 |
Correct |
3 ms |
16732 KB |
Output is correct |
21 |
Correct |
4 ms |
16728 KB |
Output is correct |
22 |
Correct |
6 ms |
22860 KB |
Output is correct |
23 |
Correct |
5 ms |
20828 KB |
Output is correct |
24 |
Correct |
5 ms |
22876 KB |
Output is correct |
25 |
Correct |
5 ms |
22876 KB |
Output is correct |
26 |
Correct |
4 ms |
18776 KB |
Output is correct |
27 |
Correct |
5 ms |
22876 KB |
Output is correct |
28 |
Correct |
4 ms |
16732 KB |
Output is correct |
29 |
Correct |
4 ms |
18768 KB |
Output is correct |
30 |
Correct |
4 ms |
18764 KB |
Output is correct |
31 |
Correct |
5 ms |
20824 KB |
Output is correct |
32 |
Correct |
5 ms |
20828 KB |
Output is correct |
33 |
Correct |
5 ms |
23040 KB |
Output is correct |
34 |
Correct |
4 ms |
20828 KB |
Output is correct |
35 |
Correct |
5 ms |
22876 KB |
Output is correct |
36 |
Correct |
5 ms |
22876 KB |
Output is correct |
37 |
Correct |
5 ms |
22872 KB |
Output is correct |
38 |
Correct |
4 ms |
20828 KB |
Output is correct |
39 |
Correct |
103 ms |
38744 KB |
Output is correct |
40 |
Correct |
96 ms |
38156 KB |
Output is correct |
41 |
Correct |
98 ms |
40248 KB |
Output is correct |
42 |
Correct |
94 ms |
38344 KB |
Output is correct |
43 |
Correct |
62 ms |
29072 KB |
Output is correct |
44 |
Correct |
54 ms |
29008 KB |
Output is correct |
45 |
Correct |
104 ms |
49748 KB |
Output is correct |
46 |
Correct |
92 ms |
58536 KB |
Output is correct |
47 |
Correct |
130 ms |
41020 KB |
Output is correct |
48 |
Correct |
202 ms |
45532 KB |
Output is correct |
49 |
Correct |
173 ms |
41188 KB |
Output is correct |
50 |
Correct |
131 ms |
41096 KB |
Output is correct |
51 |
Correct |
135 ms |
41328 KB |
Output is correct |
52 |
Correct |
157 ms |
39508 KB |
Output is correct |
53 |
Correct |
172 ms |
56144 KB |
Output is correct |
54 |
Correct |
150 ms |
29264 KB |
Output is correct |
55 |
Correct |
9 ms |
18264 KB |
Output is correct |
56 |
Correct |
11 ms |
18640 KB |
Output is correct |
57 |
Correct |
76 ms |
48292 KB |
Output is correct |
58 |
Correct |
32 ms |
27080 KB |
Output is correct |
59 |
Correct |
92 ms |
58592 KB |
Output is correct |
60 |
Correct |
323 ms |
106016 KB |
Output is correct |
61 |
Correct |
137 ms |
47652 KB |
Output is correct |
62 |
Correct |
155 ms |
58704 KB |
Output is correct |
63 |
Correct |
198 ms |
58708 KB |
Output is correct |
64 |
Correct |
318 ms |
73660 KB |
Output is correct |
65 |
Correct |
128 ms |
30284 KB |
Output is correct |
66 |
Correct |
311 ms |
89844 KB |
Output is correct |
67 |
Correct |
101 ms |
42552 KB |
Output is correct |
68 |
Correct |
204 ms |
54996 KB |
Output is correct |
69 |
Correct |
217 ms |
54940 KB |
Output is correct |
70 |
Correct |
193 ms |
53328 KB |
Output is correct |