#include <bits/stdc++.h>
#include "race.h"
using namespace std;
struct edge{
int v,cost;
};
const int INF=1e9;
int N,K,Ans,t;
int Size[200000],Min_Depth[1000001],Min_Depth_Visited[1000001];
bool Visited[200000];
vector<edge> Edge[200000];
int get_size(int v,int par=-1){
int &ret=Size[v]=1;
for (edge i:Edge[v]){
if (Visited[i.v]||i.v==par)
continue;
ret+=get_size(i.v,v);
}
return ret;
}
int get_cent(int v,int sz,int par=-1){
for (edge i:Edge[v]){
if (Visited[i.v]||i.v==par)
continue;
if (Size[i.v]*2>sz)
return get_cent(i.v,sz,v);
}
return v;
}
int dfs1(int v,int dist,int par,int depth=1){
if (dist>K)
return INF;
int ret=INF;
if (Min_Depth_Visited[K-dist]==t)
ret=depth+Min_Depth[K-dist];
for (edge i:Edge[v]){
if (Visited[i.v]||i.v==par)
continue;
ret=min(ret,dfs1(i.v,dist+i.cost,v,depth+1));
}
return ret;
}
void dfs2(int v,int dist,int par,int depth=1){
if (dist>K)
return;
if (Min_Depth_Visited[dist]!=t){
Min_Depth_Visited[dist]=t;
Min_Depth[dist]=depth;
}
else
Min_Depth[dist]=min(Min_Depth[dist],depth);
for (edge i:Edge[v]){
if (Visited[i.v]||i.v==par)
continue;
dfs2(i.v,dist+i.cost,v,depth+1);
}
}
int solve(int v){
t++;
Min_Depth_Visited[0]=t;
int ret=INF;
int cent=get_cent(v,get_size(v));
Visited[cent]=true;
for (edge i:Edge[cent]){
if (Visited[i.v])
continue;
ret=min(ret,dfs1(i.v,i.cost,cent));
dfs2(i.v,i.cost,cent);
}
for (edge i:Edge[cent]){
if (Visited[i.v])
continue;
ret=min(ret,solve(i.v));
}
return ret;
}
int best_path(int n,int k,int h[][2],int l[]){
N=n;
K=k;
for (int i=0; i<N-1; i++){
Edge[h[i][0]].push_back({h[i][1],l[i]});
Edge[h[i][1]].push_back({h[i][0],l[i]});
}
Ans=solve(0);
if (Ans==INF)
Ans=-1;
return Ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5004 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 |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 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 |
5004 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 |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4944 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
10060 KB |
Output is correct |
23 |
Correct |
7 ms |
9420 KB |
Output is correct |
24 |
Correct |
7 ms |
10828 KB |
Output is correct |
25 |
Correct |
6 ms |
8604 KB |
Output is correct |
26 |
Correct |
6 ms |
7708 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
6604 KB |
Output is correct |
29 |
Correct |
8 ms |
7628 KB |
Output is correct |
30 |
Correct |
5 ms |
7884 KB |
Output is correct |
31 |
Correct |
6 ms |
8600 KB |
Output is correct |
32 |
Correct |
6 ms |
8780 KB |
Output is correct |
33 |
Correct |
7 ms |
10316 KB |
Output is correct |
34 |
Correct |
8 ms |
9548 KB |
Output is correct |
35 |
Correct |
8 ms |
10956 KB |
Output is correct |
36 |
Correct |
8 ms |
11540 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
Correct |
5 ms |
7372 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 |
5004 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 |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
136 ms |
10648 KB |
Output is correct |
20 |
Correct |
133 ms |
10720 KB |
Output is correct |
21 |
Correct |
141 ms |
10744 KB |
Output is correct |
22 |
Correct |
124 ms |
10688 KB |
Output is correct |
23 |
Correct |
91 ms |
10984 KB |
Output is correct |
24 |
Correct |
61 ms |
10820 KB |
Output is correct |
25 |
Correct |
128 ms |
13556 KB |
Output is correct |
26 |
Correct |
103 ms |
16440 KB |
Output is correct |
27 |
Correct |
201 ms |
16264 KB |
Output is correct |
28 |
Correct |
282 ms |
27452 KB |
Output is correct |
29 |
Correct |
283 ms |
26532 KB |
Output is correct |
30 |
Correct |
191 ms |
16368 KB |
Output is correct |
31 |
Correct |
196 ms |
16196 KB |
Output is correct |
32 |
Correct |
219 ms |
16156 KB |
Output is correct |
33 |
Correct |
222 ms |
15116 KB |
Output is correct |
34 |
Correct |
187 ms |
15132 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 |
5004 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 |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4944 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
10060 KB |
Output is correct |
23 |
Correct |
7 ms |
9420 KB |
Output is correct |
24 |
Correct |
7 ms |
10828 KB |
Output is correct |
25 |
Correct |
6 ms |
8604 KB |
Output is correct |
26 |
Correct |
6 ms |
7708 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
6604 KB |
Output is correct |
29 |
Correct |
8 ms |
7628 KB |
Output is correct |
30 |
Correct |
5 ms |
7884 KB |
Output is correct |
31 |
Correct |
6 ms |
8600 KB |
Output is correct |
32 |
Correct |
6 ms |
8780 KB |
Output is correct |
33 |
Correct |
7 ms |
10316 KB |
Output is correct |
34 |
Correct |
8 ms |
9548 KB |
Output is correct |
35 |
Correct |
8 ms |
10956 KB |
Output is correct |
36 |
Correct |
8 ms |
11540 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
Correct |
5 ms |
7372 KB |
Output is correct |
39 |
Correct |
136 ms |
10648 KB |
Output is correct |
40 |
Correct |
133 ms |
10720 KB |
Output is correct |
41 |
Correct |
141 ms |
10744 KB |
Output is correct |
42 |
Correct |
124 ms |
10688 KB |
Output is correct |
43 |
Correct |
91 ms |
10984 KB |
Output is correct |
44 |
Correct |
61 ms |
10820 KB |
Output is correct |
45 |
Correct |
128 ms |
13556 KB |
Output is correct |
46 |
Correct |
103 ms |
16440 KB |
Output is correct |
47 |
Correct |
201 ms |
16264 KB |
Output is correct |
48 |
Correct |
282 ms |
27452 KB |
Output is correct |
49 |
Correct |
283 ms |
26532 KB |
Output is correct |
50 |
Correct |
191 ms |
16368 KB |
Output is correct |
51 |
Correct |
196 ms |
16196 KB |
Output is correct |
52 |
Correct |
219 ms |
16156 KB |
Output is correct |
53 |
Correct |
222 ms |
15116 KB |
Output is correct |
54 |
Correct |
187 ms |
15132 KB |
Output is correct |
55 |
Correct |
11 ms |
5656 KB |
Output is correct |
56 |
Correct |
12 ms |
5644 KB |
Output is correct |
57 |
Correct |
82 ms |
10940 KB |
Output is correct |
58 |
Correct |
39 ms |
10936 KB |
Output is correct |
59 |
Correct |
109 ms |
17176 KB |
Output is correct |
60 |
Correct |
474 ms |
34048 KB |
Output is correct |
61 |
Correct |
189 ms |
18988 KB |
Output is correct |
62 |
Correct |
225 ms |
18944 KB |
Output is correct |
63 |
Correct |
282 ms |
18896 KB |
Output is correct |
64 |
Correct |
548 ms |
21332 KB |
Output is correct |
65 |
Correct |
266 ms |
19644 KB |
Output is correct |
66 |
Correct |
427 ms |
35132 KB |
Output is correct |
67 |
Correct |
144 ms |
19508 KB |
Output is correct |
68 |
Correct |
296 ms |
24900 KB |
Output is correct |
69 |
Correct |
284 ms |
25108 KB |
Output is correct |
70 |
Correct |
275 ms |
24120 KB |
Output is correct |