#include<bits/stdc++.h>
using namespace std;
#include "race.h"
int best_path(int n, int k, int h[][2], int l[]) {
vector<vector<pair<int, int>>> adj(n);
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]);
}
vector<int> sub(n), del(n);
function<void(int, int)> get_sub = [&](int v, int p) {
sub[v] = 1;
for(auto &[u, w] : adj[v]) {
if(u != p && !del[u]) {
get_sub(u, v);
sub[v] += sub[u];
}
}
};
function<int(int, int, int)> get_centro = [&](int v, int p, int tree) {
bool is_centro = true;
if((tree - sub[v]) * 2 > tree)
is_centro = false;
for(auto &[u, w] : adj[v]) {
if(u != p && !del[u]) {
int r = get_centro(u, v, tree);
if(r != -1) return r;
if(sub[u] * 2 > tree)
is_centro = false;
}
}
if(is_centro) return v;
return -1;
};
int inf = 1e9, ans = inf;
vector<int> opt(k + 1, inf);
function<void(int, int, int, int, int)> get_ans = [&](int v, int p, int dep, int dst, int type) {
if(dst > k) return;
if(type == 1) ans = min(ans, dep + opt[k - dst]);
if(type == 2) opt[dst] = min(opt[dst], dep);
if(type == 3) opt[dst] = inf;
for(auto &[u, w] : adj[v])
if(u != p && !del[u])
get_ans(u, v, dep + 1, dst + w, type);
};
function<void(int)> decomp = [&](int v) {
get_sub(v, -1);
v = get_centro(v, -1, sub[v]);
opt[0] = 0;
for(auto &[u, w] : adj[v]) if(!del[u]) {
get_ans(u, v, 1, w, 1);
get_ans(u, v, 1, w, 2);
}
for(auto &[u, w] : adj[v]) if(!del[u])
get_ans(u, v, 1, w, 3);
del[v] = true;
for(auto &[u, w] : adj[v]) if(!del[u])
decomp(u);
};
decomp(0);
if(ans >= inf) ans = -1;
return ans;
}
Compilation message
race.cpp: In lambda function:
race.cpp:16:18: warning: unused variable 'w' [-Wunused-variable]
for(auto &[u, w] : adj[v]) {
^
race.cpp: In lambda function:
race.cpp:28:18: warning: unused variable 'w' [-Wunused-variable]
for(auto &[u, w] : adj[v]) {
^
race.cpp: In lambda function:
race.cpp:64:18: warning: unused variable 'w' [-Wunused-variable]
for(auto &[u, w] : adj[v]) if(!del[u])
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
7 ms |
3968 KB |
Output is correct |
23 |
Correct |
7 ms |
3328 KB |
Output is correct |
24 |
Correct |
7 ms |
3840 KB |
Output is correct |
25 |
Correct |
8 ms |
3712 KB |
Output is correct |
26 |
Correct |
7 ms |
1792 KB |
Output is correct |
27 |
Correct |
7 ms |
3632 KB |
Output is correct |
28 |
Correct |
6 ms |
1280 KB |
Output is correct |
29 |
Correct |
7 ms |
1664 KB |
Output is correct |
30 |
Correct |
6 ms |
1792 KB |
Output is correct |
31 |
Correct |
7 ms |
2944 KB |
Output is correct |
32 |
Correct |
7 ms |
3200 KB |
Output is correct |
33 |
Correct |
8 ms |
3456 KB |
Output is correct |
34 |
Correct |
6 ms |
2816 KB |
Output is correct |
35 |
Correct |
7 ms |
3584 KB |
Output is correct |
36 |
Correct |
7 ms |
4096 KB |
Output is correct |
37 |
Correct |
7 ms |
3584 KB |
Output is correct |
38 |
Correct |
7 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
237 ms |
8348 KB |
Output is correct |
20 |
Correct |
278 ms |
8356 KB |
Output is correct |
21 |
Correct |
253 ms |
8316 KB |
Output is correct |
22 |
Correct |
226 ms |
8316 KB |
Output is correct |
23 |
Correct |
135 ms |
8568 KB |
Output is correct |
24 |
Correct |
105 ms |
8440 KB |
Output is correct |
25 |
Correct |
213 ms |
13560 KB |
Output is correct |
26 |
Correct |
191 ms |
18772 KB |
Output is correct |
27 |
Correct |
308 ms |
16504 KB |
Output is correct |
28 |
Correct |
444 ms |
37068 KB |
Output is correct |
29 |
Correct |
581 ms |
35576 KB |
Output is correct |
30 |
Correct |
241 ms |
16632 KB |
Output is correct |
31 |
Correct |
241 ms |
16504 KB |
Output is correct |
32 |
Correct |
312 ms |
16632 KB |
Output is correct |
33 |
Correct |
377 ms |
15608 KB |
Output is correct |
34 |
Correct |
316 ms |
15352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
7 ms |
3968 KB |
Output is correct |
23 |
Correct |
7 ms |
3328 KB |
Output is correct |
24 |
Correct |
7 ms |
3840 KB |
Output is correct |
25 |
Correct |
8 ms |
3712 KB |
Output is correct |
26 |
Correct |
7 ms |
1792 KB |
Output is correct |
27 |
Correct |
7 ms |
3632 KB |
Output is correct |
28 |
Correct |
6 ms |
1280 KB |
Output is correct |
29 |
Correct |
7 ms |
1664 KB |
Output is correct |
30 |
Correct |
6 ms |
1792 KB |
Output is correct |
31 |
Correct |
7 ms |
2944 KB |
Output is correct |
32 |
Correct |
7 ms |
3200 KB |
Output is correct |
33 |
Correct |
8 ms |
3456 KB |
Output is correct |
34 |
Correct |
6 ms |
2816 KB |
Output is correct |
35 |
Correct |
7 ms |
3584 KB |
Output is correct |
36 |
Correct |
7 ms |
4096 KB |
Output is correct |
37 |
Correct |
7 ms |
3584 KB |
Output is correct |
38 |
Correct |
7 ms |
2432 KB |
Output is correct |
39 |
Correct |
237 ms |
8348 KB |
Output is correct |
40 |
Correct |
278 ms |
8356 KB |
Output is correct |
41 |
Correct |
253 ms |
8316 KB |
Output is correct |
42 |
Correct |
226 ms |
8316 KB |
Output is correct |
43 |
Correct |
135 ms |
8568 KB |
Output is correct |
44 |
Correct |
105 ms |
8440 KB |
Output is correct |
45 |
Correct |
213 ms |
13560 KB |
Output is correct |
46 |
Correct |
191 ms |
18772 KB |
Output is correct |
47 |
Correct |
308 ms |
16504 KB |
Output is correct |
48 |
Correct |
444 ms |
37068 KB |
Output is correct |
49 |
Correct |
581 ms |
35576 KB |
Output is correct |
50 |
Correct |
241 ms |
16632 KB |
Output is correct |
51 |
Correct |
241 ms |
16504 KB |
Output is correct |
52 |
Correct |
312 ms |
16632 KB |
Output is correct |
53 |
Correct |
377 ms |
15608 KB |
Output is correct |
54 |
Correct |
316 ms |
15352 KB |
Output is correct |
55 |
Correct |
18 ms |
1152 KB |
Output is correct |
56 |
Correct |
20 ms |
1152 KB |
Output is correct |
57 |
Correct |
156 ms |
8568 KB |
Output is correct |
58 |
Correct |
51 ms |
8556 KB |
Output is correct |
59 |
Correct |
179 ms |
19528 KB |
Output is correct |
60 |
Correct |
1138 ms |
39928 KB |
Output is correct |
61 |
Correct |
335 ms |
16504 KB |
Output is correct |
62 |
Correct |
304 ms |
20472 KB |
Output is correct |
63 |
Correct |
441 ms |
20344 KB |
Output is correct |
64 |
Correct |
909 ms |
18452 KB |
Output is correct |
65 |
Correct |
376 ms |
16508 KB |
Output is correct |
66 |
Correct |
741 ms |
35952 KB |
Output is correct |
67 |
Correct |
160 ms |
20584 KB |
Output is correct |
68 |
Correct |
423 ms |
20472 KB |
Output is correct |
69 |
Correct |
425 ms |
20600 KB |
Output is correct |
70 |
Correct |
547 ms |
19832 KB |
Output is correct |