#include "race.h"
#include <bits/stdc++.h>
#define X first
#define Y second
#define MP make_pair
#define ll long long
using namespace std;
const int MAXN = 2e5 + 12;
int answer, n, k, sz[MAXN], mx[MAXN];
map< int, int > act;
vector< pair< int, int > > graph[MAXN];
bool block[MAXN];
void dfs_sz(int v, int pr){
sz[v] = 1, mx[v] = 0;
for(pair<int, int> to: graph[v]){
if(to.X != pr && !block[to.X]){
dfs_sz(to.X, v), sz[v] += sz[to.X];
if(sz[mx[v]] < sz[to.X])
mx[v] = to.X;
}
}
}
int get_cent(int v){
int u = v;
while(sz[v] < sz[mx[u]] * 2)
u = mx[u];
return u;
}
void dfs_ans(int v, int pr, ll cost, int dist){
if(cost > k)
return;
if(act.count(k - cost))
answer = min(answer, dist + act[k - cost]);
for(pair<int, int> to: graph[v]){
if(to.X != pr && !block[to.X]){
dfs_ans(to.X, v, cost + to.Y, dist + 1);
}
}
}
void dfs_add(int v, int pr, ll cost, int dist){
if(cost > k)
return;
if(!act.count(cost) || act[cost] > dist)
act[cost] = dist;
for(pair<int, int> to: graph[v]){
if(to.X != pr && !block[to.X]){
dfs_add(to.X, v, cost + to.Y, dist + 1);
}
}
}
void cent(int v){
dfs_sz(v, v), v = get_cent(v), block[v] = 1;
act[0] = 0;
for(pair<int, int> to: graph[v]){
if(!block[to.X])
dfs_ans(to.X, v, to.Y, 1), dfs_add(to.X, v, to.Y, 1);
}
act.clear();
for(pair<int, int> to: graph[v]){
if(!block[to.X])
cent(to.X);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K, answer = MAXN;
for(int i = 0;i < n - 1;i++){
graph[H[i][1] + 1].push_back(MP(H[i][0] + 1, L[i]));
graph[H[i][0] + 1].push_back(MP(H[i][1] + 1, L[i]));
}
cent(1);
if(answer == MAXN)
answer = -1;
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
5016 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
3 ms |
4984 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 |
5012 KB |
Output is correct |
15 |
Correct |
3 ms |
4984 KB |
Output is correct |
16 |
Correct |
3 ms |
4988 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
5016 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
3 ms |
4984 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 |
5012 KB |
Output is correct |
15 |
Correct |
3 ms |
4984 KB |
Output is correct |
16 |
Correct |
3 ms |
4988 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5016 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
5 ms |
5020 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5024 KB |
Output is correct |
28 |
Correct |
4 ms |
5020 KB |
Output is correct |
29 |
Correct |
4 ms |
5068 KB |
Output is correct |
30 |
Correct |
5 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
5 ms |
5068 KB |
Output is correct |
33 |
Correct |
6 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5020 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
5016 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
3 ms |
4984 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 |
5012 KB |
Output is correct |
15 |
Correct |
3 ms |
4984 KB |
Output is correct |
16 |
Correct |
3 ms |
4988 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5016 KB |
Output is correct |
19 |
Correct |
223 ms |
11728 KB |
Output is correct |
20 |
Correct |
224 ms |
11832 KB |
Output is correct |
21 |
Correct |
238 ms |
11800 KB |
Output is correct |
22 |
Correct |
221 ms |
11812 KB |
Output is correct |
23 |
Correct |
109 ms |
12108 KB |
Output is correct |
24 |
Correct |
101 ms |
12032 KB |
Output is correct |
25 |
Correct |
170 ms |
15296 KB |
Output is correct |
26 |
Correct |
131 ms |
19272 KB |
Output is correct |
27 |
Correct |
237 ms |
18496 KB |
Output is correct |
28 |
Correct |
293 ms |
33988 KB |
Output is correct |
29 |
Correct |
295 ms |
32812 KB |
Output is correct |
30 |
Correct |
248 ms |
19572 KB |
Output is correct |
31 |
Correct |
229 ms |
19548 KB |
Output is correct |
32 |
Correct |
270 ms |
19676 KB |
Output is correct |
33 |
Correct |
331 ms |
18452 KB |
Output is correct |
34 |
Correct |
206 ms |
19356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
5016 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5012 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
3 ms |
4984 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 |
5012 KB |
Output is correct |
15 |
Correct |
3 ms |
4984 KB |
Output is correct |
16 |
Correct |
3 ms |
4988 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5016 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
5 ms |
5020 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5024 KB |
Output is correct |
28 |
Correct |
4 ms |
5020 KB |
Output is correct |
29 |
Correct |
4 ms |
5068 KB |
Output is correct |
30 |
Correct |
5 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
5 ms |
5068 KB |
Output is correct |
33 |
Correct |
6 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
5 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5020 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
223 ms |
11728 KB |
Output is correct |
40 |
Correct |
224 ms |
11832 KB |
Output is correct |
41 |
Correct |
238 ms |
11800 KB |
Output is correct |
42 |
Correct |
221 ms |
11812 KB |
Output is correct |
43 |
Correct |
109 ms |
12108 KB |
Output is correct |
44 |
Correct |
101 ms |
12032 KB |
Output is correct |
45 |
Correct |
170 ms |
15296 KB |
Output is correct |
46 |
Correct |
131 ms |
19272 KB |
Output is correct |
47 |
Correct |
237 ms |
18496 KB |
Output is correct |
48 |
Correct |
293 ms |
33988 KB |
Output is correct |
49 |
Correct |
295 ms |
32812 KB |
Output is correct |
50 |
Correct |
248 ms |
19572 KB |
Output is correct |
51 |
Correct |
229 ms |
19548 KB |
Output is correct |
52 |
Correct |
270 ms |
19676 KB |
Output is correct |
53 |
Correct |
331 ms |
18452 KB |
Output is correct |
54 |
Correct |
206 ms |
19356 KB |
Output is correct |
55 |
Correct |
23 ms |
5836 KB |
Output is correct |
56 |
Correct |
19 ms |
5788 KB |
Output is correct |
57 |
Correct |
157 ms |
12228 KB |
Output is correct |
58 |
Correct |
56 ms |
12060 KB |
Output is correct |
59 |
Correct |
311 ms |
22852 KB |
Output is correct |
60 |
Correct |
976 ms |
41756 KB |
Output is correct |
61 |
Correct |
312 ms |
19940 KB |
Output is correct |
62 |
Correct |
349 ms |
19688 KB |
Output is correct |
63 |
Correct |
402 ms |
19800 KB |
Output is correct |
64 |
Correct |
1115 ms |
24448 KB |
Output is correct |
65 |
Correct |
279 ms |
20452 KB |
Output is correct |
66 |
Correct |
641 ms |
30552 KB |
Output is correct |
67 |
Correct |
148 ms |
20316 KB |
Output is correct |
68 |
Correct |
463 ms |
23972 KB |
Output is correct |
69 |
Correct |
485 ms |
24068 KB |
Output is correct |
70 |
Correct |
457 ms |
23108 KB |
Output is correct |