#include "race.h"
#include <vector>
#include <map>
#include <stdint.h>
static const int INF=1e9+7;
static std::vector<std::pair<int,int> > adj[200005];
static int ans=INF;
static int size[200005];
static int KK;
static bool dead[200005];
static int best[1000005];
static int dfs_size(int node,int parent){
size[node]=1;
for(auto e:adj[node]){
int child=e.first;
if(dead[child]||child==parent) continue;
dfs_size(child,node);
size[node]+=size[child];
}
}
static int find_centroid(int node,int parent,int total_size){
for(auto e:adj[node]){
int child=e.first;
if(dead[child]||child==parent) continue;
if(size[child]*2>total_size){
return find_centroid(child,node,total_size);
}
}
return node;
}
static void dfs_add(int node,int parent,int64_t dist,int len){
if(dist>KK) return;
best[dist]=std::min(best[dist],len);
for(auto e:adj[node]){
int child=e.first;
if(dead[child]||child==parent) continue;
dfs_add(child,node,dist+e.second,len+1);
}
}
static void dfs_test(int node,int parent,int64_t dist,int len){
if(dist>KK) return;
ans=std::min(ans,best[KK-dist]+len);
for(auto e:adj[node]){
int child=e.first;
if(dead[child]||child==parent) continue;
dfs_test(child,node,dist+e.second,len+1);
}
}
static void dfs_rem(int node,int parent,int64_t dist,int len){
if(dist>KK) return;
best[dist]=INF;
for(auto e:adj[node]){
int child=e.first;
if(dead[child]||child==parent) continue;
dfs_rem(child,node,dist+e.second,len+1);
}
}
static void solve(int node){
dfs_size(node,node);
node=find_centroid(node,node,size[node]);
best[0]=0;
for(auto e:adj[node]){
int child=e.first;
if(dead[child]) continue;
dfs_test(child,node,e.second,1);
dfs_add(child,node,e.second,1);
}
for(auto e:adj[node]){
int child=e.first;
if(dead[child]) continue;
dfs_rem(child,node,e.second,1);
}
dead[node]=true;
for(auto e:adj[node]){
int child=e.first;
if(dead[child]) continue;
solve(child);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
for(int i=0;i<N;i++){
adj[i].clear();
}
ans=INF;
std::fill(dead,dead+N,false);
KK=K;
std::fill(best,best+1000001,INF);
for(int i=0;i<N-1;i++){
adj[H[i][0]].emplace_back(H[i][1],L[i]);
adj[H[i][1]].emplace_back(H[i][0],L[i]);
}
solve(0);
return ans<INF?ans:-1;
}
Compilation message
race.cpp: In function 'int dfs_size(int, int)':
race.cpp:23:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
8 ms |
8960 KB |
Output is correct |
4 |
Correct |
8 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
9 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
9 ms |
8960 KB |
Output is correct |
12 |
Correct |
9 ms |
8932 KB |
Output is correct |
13 |
Correct |
8 ms |
8956 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
8960 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
8 ms |
8960 KB |
Output is correct |
4 |
Correct |
8 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
9 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
9 ms |
8960 KB |
Output is correct |
12 |
Correct |
9 ms |
8932 KB |
Output is correct |
13 |
Correct |
8 ms |
8956 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
8960 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
19 |
Correct |
9 ms |
8960 KB |
Output is correct |
20 |
Correct |
9 ms |
8960 KB |
Output is correct |
21 |
Correct |
10 ms |
8960 KB |
Output is correct |
22 |
Correct |
10 ms |
8960 KB |
Output is correct |
23 |
Correct |
9 ms |
8960 KB |
Output is correct |
24 |
Correct |
10 ms |
8960 KB |
Output is correct |
25 |
Correct |
10 ms |
8948 KB |
Output is correct |
26 |
Correct |
10 ms |
8960 KB |
Output is correct |
27 |
Correct |
10 ms |
8960 KB |
Output is correct |
28 |
Correct |
9 ms |
8960 KB |
Output is correct |
29 |
Correct |
9 ms |
8960 KB |
Output is correct |
30 |
Correct |
10 ms |
8932 KB |
Output is correct |
31 |
Correct |
10 ms |
8960 KB |
Output is correct |
32 |
Correct |
10 ms |
8960 KB |
Output is correct |
33 |
Correct |
9 ms |
8960 KB |
Output is correct |
34 |
Correct |
9 ms |
8960 KB |
Output is correct |
35 |
Correct |
10 ms |
8960 KB |
Output is correct |
36 |
Correct |
10 ms |
8960 KB |
Output is correct |
37 |
Correct |
10 ms |
8960 KB |
Output is correct |
38 |
Correct |
9 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
8 ms |
8960 KB |
Output is correct |
4 |
Correct |
8 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
9 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
9 ms |
8960 KB |
Output is correct |
12 |
Correct |
9 ms |
8932 KB |
Output is correct |
13 |
Correct |
8 ms |
8956 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
8960 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
19 |
Correct |
179 ms |
14312 KB |
Output is correct |
20 |
Correct |
173 ms |
14356 KB |
Output is correct |
21 |
Correct |
197 ms |
14456 KB |
Output is correct |
22 |
Correct |
162 ms |
14328 KB |
Output is correct |
23 |
Correct |
97 ms |
14456 KB |
Output is correct |
24 |
Correct |
78 ms |
14328 KB |
Output is correct |
25 |
Correct |
188 ms |
17016 KB |
Output is correct |
26 |
Correct |
115 ms |
20124 KB |
Output is correct |
27 |
Correct |
222 ms |
19928 KB |
Output is correct |
28 |
Correct |
412 ms |
31096 KB |
Output is correct |
29 |
Correct |
466 ms |
30072 KB |
Output is correct |
30 |
Correct |
220 ms |
19832 KB |
Output is correct |
31 |
Correct |
256 ms |
19832 KB |
Output is correct |
32 |
Correct |
258 ms |
19960 KB |
Output is correct |
33 |
Correct |
371 ms |
18680 KB |
Output is correct |
34 |
Correct |
368 ms |
18788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
8 ms |
8960 KB |
Output is correct |
4 |
Correct |
8 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
9 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
9 ms |
8960 KB |
Output is correct |
12 |
Correct |
9 ms |
8932 KB |
Output is correct |
13 |
Correct |
8 ms |
8956 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
10 ms |
8960 KB |
Output is correct |
18 |
Correct |
9 ms |
8960 KB |
Output is correct |
19 |
Correct |
9 ms |
8960 KB |
Output is correct |
20 |
Correct |
9 ms |
8960 KB |
Output is correct |
21 |
Correct |
10 ms |
8960 KB |
Output is correct |
22 |
Correct |
10 ms |
8960 KB |
Output is correct |
23 |
Correct |
9 ms |
8960 KB |
Output is correct |
24 |
Correct |
10 ms |
8960 KB |
Output is correct |
25 |
Correct |
10 ms |
8948 KB |
Output is correct |
26 |
Correct |
10 ms |
8960 KB |
Output is correct |
27 |
Correct |
10 ms |
8960 KB |
Output is correct |
28 |
Correct |
9 ms |
8960 KB |
Output is correct |
29 |
Correct |
9 ms |
8960 KB |
Output is correct |
30 |
Correct |
10 ms |
8932 KB |
Output is correct |
31 |
Correct |
10 ms |
8960 KB |
Output is correct |
32 |
Correct |
10 ms |
8960 KB |
Output is correct |
33 |
Correct |
9 ms |
8960 KB |
Output is correct |
34 |
Correct |
9 ms |
8960 KB |
Output is correct |
35 |
Correct |
10 ms |
8960 KB |
Output is correct |
36 |
Correct |
10 ms |
8960 KB |
Output is correct |
37 |
Correct |
10 ms |
8960 KB |
Output is correct |
38 |
Correct |
9 ms |
8960 KB |
Output is correct |
39 |
Correct |
179 ms |
14312 KB |
Output is correct |
40 |
Correct |
173 ms |
14356 KB |
Output is correct |
41 |
Correct |
197 ms |
14456 KB |
Output is correct |
42 |
Correct |
162 ms |
14328 KB |
Output is correct |
43 |
Correct |
97 ms |
14456 KB |
Output is correct |
44 |
Correct |
78 ms |
14328 KB |
Output is correct |
45 |
Correct |
188 ms |
17016 KB |
Output is correct |
46 |
Correct |
115 ms |
20124 KB |
Output is correct |
47 |
Correct |
222 ms |
19928 KB |
Output is correct |
48 |
Correct |
412 ms |
31096 KB |
Output is correct |
49 |
Correct |
466 ms |
30072 KB |
Output is correct |
50 |
Correct |
220 ms |
19832 KB |
Output is correct |
51 |
Correct |
256 ms |
19832 KB |
Output is correct |
52 |
Correct |
258 ms |
19960 KB |
Output is correct |
53 |
Correct |
371 ms |
18680 KB |
Output is correct |
54 |
Correct |
368 ms |
18788 KB |
Output is correct |
55 |
Correct |
20 ms |
9600 KB |
Output is correct |
56 |
Correct |
20 ms |
9472 KB |
Output is correct |
57 |
Correct |
89 ms |
15836 KB |
Output is correct |
58 |
Correct |
45 ms |
15468 KB |
Output is correct |
59 |
Correct |
121 ms |
21368 KB |
Output is correct |
60 |
Correct |
769 ms |
33260 KB |
Output is correct |
61 |
Correct |
232 ms |
22776 KB |
Output is correct |
62 |
Correct |
360 ms |
22904 KB |
Output is correct |
63 |
Correct |
378 ms |
22776 KB |
Output is correct |
64 |
Correct |
753 ms |
23132 KB |
Output is correct |
65 |
Correct |
261 ms |
23704 KB |
Output is correct |
66 |
Correct |
568 ms |
31428 KB |
Output is correct |
67 |
Correct |
135 ms |
23400 KB |
Output is correct |
68 |
Correct |
333 ms |
23496 KB |
Output is correct |
69 |
Correct |
358 ms |
23852 KB |
Output is correct |
70 |
Correct |
391 ms |
23104 KB |
Output is correct |