#include <bits/stdc++.h>
#include "race.h"
using namespace std ;
const int MAX = 1e6 + 10 ;
vector< vector< pair<int , int> > >adj(MAX) ;
int n , k ;
int src ;
int sz[MAX] , mark[MAX] ;
void pre_dfs(int node , int par)
{
sz[node] = 1 ;
for(auto &childd : adj[node])
{
int child = childd.first , w = childd.second ;
if(child == par || mark[child])
continue ;
pre_dfs(child , node) ;
sz[node] += sz[child] ;
}
}
int FindCentroid(int node , int par)
{
for(auto &childd : adj[node])
{
int child = childd.first , w = childd.second ;
if(child == par || mark[child])
continue ;
if(sz[child] > sz[src] / 2)
return FindCentroid(child , node) ;
}
return node ;
}
int ans = 1e9 ;
int val[MAX] , dep[MAX] ;
void dfs(int node , int par , int w , int t)
{
w = min(w , k+1) ;
if(t == 0 && w <= k)
ans = min(ans , val[k-w] + dep[node]) ;
else if(t == 1 && w <= k)
val[w] = min(val[w] , dep[node]) ;
else if(t == 2 && w <= k)
val[w] = 1e9 ;
for(auto &childd : adj[node])
{
int child = childd.first , w2 = childd.second ;
if(mark[child] || child == par)
continue ;
dep[child] = dep[node] + 1 ;
dfs(child , node , w + w2 , t) ;
}
}
void solve(int Src)
{
src = Src ;
pre_dfs(src , -1) ;
int centroid = FindCentroid(src , -1) ;
mark[centroid] = 1 ;
dep[centroid] = 0 , val[0] = 0 ;
for(auto &childd : adj[centroid])
{
int child = childd.first , w = childd.second ;
if(mark[child])
continue ;
dep[child] = 1 ;
dfs(child , centroid , w , 0) ;
dfs(child , centroid , w , 1) ;
}
for(auto &childd : adj[centroid])
{
int child = childd.first , w = childd.second ;
if(mark[child])
continue ;
dfs(child , centroid , w , 2) ;
}
for(auto &childd : adj[centroid])
{
int child = childd.first , w = childd.second ;
if(mark[child])
continue ;
solve(child) ;
}
}
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]+1].emplace_back(H[i][1]+1 , L[i]) ;
adj[H[i][1]+1].emplace_back(H[i][0]+1 , L[i]) ;
}
for(int i = 0 ; i <= k ; ++i)
val[i] = 1e9 ;
solve(1) ;
if(ans == 1e9)
ans = -1 ;
return ans ;
}
Compilation message
race.cpp: In function 'void pre_dfs(int, int)':
race.cpp:19:30: warning: unused variable 'w' [-Wunused-variable]
19 | int child = childd.first , w = childd.second ;
| ^
race.cpp: In function 'int FindCentroid(int, int)':
race.cpp:31:30: warning: unused variable 'w' [-Wunused-variable]
31 | int child = childd.first , w = childd.second ;
| ^
race.cpp: In function 'void solve(int)':
race.cpp:87:30: warning: unused variable 'w' [-Wunused-variable]
87 | int child = childd.first , w = childd.second ;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23716 KB |
Output is correct |
20 |
Correct |
11 ms |
23744 KB |
Output is correct |
21 |
Correct |
12 ms |
24020 KB |
Output is correct |
22 |
Correct |
14 ms |
27432 KB |
Output is correct |
23 |
Correct |
15 ms |
26856 KB |
Output is correct |
24 |
Correct |
14 ms |
27220 KB |
Output is correct |
25 |
Correct |
14 ms |
27216 KB |
Output is correct |
26 |
Correct |
13 ms |
25172 KB |
Output is correct |
27 |
Correct |
14 ms |
26984 KB |
Output is correct |
28 |
Correct |
13 ms |
24660 KB |
Output is correct |
29 |
Correct |
13 ms |
25172 KB |
Output is correct |
30 |
Correct |
13 ms |
25300 KB |
Output is correct |
31 |
Correct |
16 ms |
26452 KB |
Output is correct |
32 |
Correct |
14 ms |
26580 KB |
Output is correct |
33 |
Correct |
14 ms |
26964 KB |
Output is correct |
34 |
Correct |
13 ms |
26196 KB |
Output is correct |
35 |
Correct |
14 ms |
27092 KB |
Output is correct |
36 |
Correct |
14 ms |
27476 KB |
Output is correct |
37 |
Correct |
16 ms |
26964 KB |
Output is correct |
38 |
Correct |
13 ms |
25812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
127 ms |
29828 KB |
Output is correct |
20 |
Correct |
156 ms |
30040 KB |
Output is correct |
21 |
Correct |
129 ms |
29860 KB |
Output is correct |
22 |
Correct |
106 ms |
29816 KB |
Output is correct |
23 |
Correct |
99 ms |
30084 KB |
Output is correct |
24 |
Correct |
58 ms |
29968 KB |
Output is correct |
25 |
Correct |
146 ms |
33420 KB |
Output is correct |
26 |
Correct |
90 ms |
36992 KB |
Output is correct |
27 |
Correct |
147 ms |
36064 KB |
Output is correct |
28 |
Correct |
366 ms |
50380 KB |
Output is correct |
29 |
Correct |
367 ms |
49252 KB |
Output is correct |
30 |
Correct |
142 ms |
36052 KB |
Output is correct |
31 |
Correct |
149 ms |
35972 KB |
Output is correct |
32 |
Correct |
184 ms |
36044 KB |
Output is correct |
33 |
Correct |
240 ms |
34892 KB |
Output is correct |
34 |
Correct |
237 ms |
34932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23716 KB |
Output is correct |
20 |
Correct |
11 ms |
23744 KB |
Output is correct |
21 |
Correct |
12 ms |
24020 KB |
Output is correct |
22 |
Correct |
14 ms |
27432 KB |
Output is correct |
23 |
Correct |
15 ms |
26856 KB |
Output is correct |
24 |
Correct |
14 ms |
27220 KB |
Output is correct |
25 |
Correct |
14 ms |
27216 KB |
Output is correct |
26 |
Correct |
13 ms |
25172 KB |
Output is correct |
27 |
Correct |
14 ms |
26984 KB |
Output is correct |
28 |
Correct |
13 ms |
24660 KB |
Output is correct |
29 |
Correct |
13 ms |
25172 KB |
Output is correct |
30 |
Correct |
13 ms |
25300 KB |
Output is correct |
31 |
Correct |
16 ms |
26452 KB |
Output is correct |
32 |
Correct |
14 ms |
26580 KB |
Output is correct |
33 |
Correct |
14 ms |
26964 KB |
Output is correct |
34 |
Correct |
13 ms |
26196 KB |
Output is correct |
35 |
Correct |
14 ms |
27092 KB |
Output is correct |
36 |
Correct |
14 ms |
27476 KB |
Output is correct |
37 |
Correct |
16 ms |
26964 KB |
Output is correct |
38 |
Correct |
13 ms |
25812 KB |
Output is correct |
39 |
Correct |
127 ms |
29828 KB |
Output is correct |
40 |
Correct |
156 ms |
30040 KB |
Output is correct |
41 |
Correct |
129 ms |
29860 KB |
Output is correct |
42 |
Correct |
106 ms |
29816 KB |
Output is correct |
43 |
Correct |
99 ms |
30084 KB |
Output is correct |
44 |
Correct |
58 ms |
29968 KB |
Output is correct |
45 |
Correct |
146 ms |
33420 KB |
Output is correct |
46 |
Correct |
90 ms |
36992 KB |
Output is correct |
47 |
Correct |
147 ms |
36064 KB |
Output is correct |
48 |
Correct |
366 ms |
50380 KB |
Output is correct |
49 |
Correct |
367 ms |
49252 KB |
Output is correct |
50 |
Correct |
142 ms |
36052 KB |
Output is correct |
51 |
Correct |
149 ms |
35972 KB |
Output is correct |
52 |
Correct |
184 ms |
36044 KB |
Output is correct |
53 |
Correct |
240 ms |
34892 KB |
Output is correct |
54 |
Correct |
237 ms |
34932 KB |
Output is correct |
55 |
Correct |
19 ms |
24532 KB |
Output is correct |
56 |
Correct |
22 ms |
24492 KB |
Output is correct |
57 |
Correct |
75 ms |
31388 KB |
Output is correct |
58 |
Correct |
38 ms |
31128 KB |
Output is correct |
59 |
Correct |
88 ms |
39120 KB |
Output is correct |
60 |
Correct |
423 ms |
56480 KB |
Output is correct |
61 |
Correct |
157 ms |
39088 KB |
Output is correct |
62 |
Correct |
150 ms |
42832 KB |
Output is correct |
63 |
Correct |
204 ms |
42956 KB |
Output is correct |
64 |
Correct |
383 ms |
41416 KB |
Output is correct |
65 |
Correct |
269 ms |
39760 KB |
Output is correct |
66 |
Correct |
406 ms |
53788 KB |
Output is correct |
67 |
Correct |
103 ms |
43628 KB |
Output is correct |
68 |
Correct |
215 ms |
43596 KB |
Output is correct |
69 |
Correct |
221 ms |
43756 KB |
Output is correct |
70 |
Correct |
235 ms |
42956 KB |
Output is correct |