#include "race.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define maxn 200005
using namespace std;
typedef pair<int, int> ii;
int n, k;
vector<ii> adj[maxn];
int ds = INT_MAX;
int sz[maxn], tsz = 0, nho[maxn];
void resz(int u, int dad) {
sz[u] = 1; ++tsz;
for (auto [v, w] : adj[u]) {
if (v == dad || nho[v]) continue;
resz(v, u);
sz[u] += sz[v];
}
}
int find_centroid(int u, int dad) {
for (auto [v, w] : adj[u]) {
if (v != dad && !nho[v] && sz[v] > tsz/2) return find_centroid(v, u);
}
return u;
}
map<int, int> dis;
void dfs(int u, int dad, int l, int depth) {
if (l > k) return;
int h = k-l;
if (dis.count(h)) ds = min(ds, dis[h] + depth);
for (auto [v, w] : adj[u]) {
if (v == dad || nho[v]) continue;
dfs(v, u, l+w, depth+1);
}
}
void refs(int u, int dad, int l, int depth) {
if (l > k) return;
if (dis.count(l)) dis[l] = min(dis[l], depth);
else dis[l] = depth;
for (auto [v, w] : adj[u]) {
if (v == dad || nho[v]) continue;
refs(v, u, l+w, depth+1);
}
}
void dcp(int u) {
tsz = 0;
resz(u, 0);
int C = find_centroid(u, 0); nho[C] = 1;
dis.clear();
dis[0] = 0;
for (auto [v, w] : adj[C]) {
if (nho[v]) continue;
dfs(v, C, w, 1);
refs(v, C, w, 1);
}
for (auto [v, w] : adj[C]) {
if (!nho[v]) dcp(v);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
ds = INT_MAX;
for (int i = 0; i < N-1; i++) {
int u = H[i][0], v = H[i][1], l = L[i];
++u; ++v;
adj[u].emplace_back(v, l);
adj[v].emplace_back(u, l);
}
dcp(1);
for (int i = 1; i <= n; i++) {adj[i].clear(); nho[i] = 0;}
return (ds == INT_MAX ? -1 : ds);
}
Compilation message
race.cpp: In function 'void resz(int, int)':
race.cpp:19:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
19 | for (auto [v, w] : adj[u]) {
| ^
race.cpp: In function 'int find_centroid(int, int)':
race.cpp:27:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
27 | for (auto [v, w] : adj[u]) {
| ^
race.cpp: In function 'void dfs(int, int, int, int)':
race.cpp:39:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | for (auto [v, w] : adj[u]) {
| ^
race.cpp: In function 'void refs(int, int, int, int)':
race.cpp:50:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
50 | for (auto [v, w] : adj[u]) {
| ^
race.cpp: In function 'void dcp(int)':
race.cpp:66:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for (auto [v, w] : adj[C]) {
| ^
race.cpp:72:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | for (auto [v, w] : adj[C]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10320 KB |
Output is correct |
2 |
Correct |
3 ms |
10320 KB |
Output is correct |
3 |
Correct |
3 ms |
10168 KB |
Output is correct |
4 |
Correct |
3 ms |
10488 KB |
Output is correct |
5 |
Correct |
3 ms |
10576 KB |
Output is correct |
6 |
Correct |
3 ms |
10320 KB |
Output is correct |
7 |
Correct |
3 ms |
10320 KB |
Output is correct |
8 |
Correct |
3 ms |
10320 KB |
Output is correct |
9 |
Correct |
3 ms |
10320 KB |
Output is correct |
10 |
Correct |
3 ms |
10320 KB |
Output is correct |
11 |
Correct |
3 ms |
10320 KB |
Output is correct |
12 |
Correct |
4 ms |
10344 KB |
Output is correct |
13 |
Correct |
3 ms |
10320 KB |
Output is correct |
14 |
Correct |
3 ms |
10320 KB |
Output is correct |
15 |
Correct |
4 ms |
10320 KB |
Output is correct |
16 |
Correct |
3 ms |
10320 KB |
Output is correct |
17 |
Correct |
3 ms |
10488 KB |
Output is correct |
18 |
Correct |
3 ms |
10320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10320 KB |
Output is correct |
2 |
Correct |
3 ms |
10320 KB |
Output is correct |
3 |
Correct |
3 ms |
10168 KB |
Output is correct |
4 |
Correct |
3 ms |
10488 KB |
Output is correct |
5 |
Correct |
3 ms |
10576 KB |
Output is correct |
6 |
Correct |
3 ms |
10320 KB |
Output is correct |
7 |
Correct |
3 ms |
10320 KB |
Output is correct |
8 |
Correct |
3 ms |
10320 KB |
Output is correct |
9 |
Correct |
3 ms |
10320 KB |
Output is correct |
10 |
Correct |
3 ms |
10320 KB |
Output is correct |
11 |
Correct |
3 ms |
10320 KB |
Output is correct |
12 |
Correct |
4 ms |
10344 KB |
Output is correct |
13 |
Correct |
3 ms |
10320 KB |
Output is correct |
14 |
Correct |
3 ms |
10320 KB |
Output is correct |
15 |
Correct |
4 ms |
10320 KB |
Output is correct |
16 |
Correct |
3 ms |
10320 KB |
Output is correct |
17 |
Correct |
3 ms |
10488 KB |
Output is correct |
18 |
Correct |
3 ms |
10320 KB |
Output is correct |
19 |
Correct |
4 ms |
10320 KB |
Output is correct |
20 |
Correct |
3 ms |
10320 KB |
Output is correct |
21 |
Correct |
4 ms |
10320 KB |
Output is correct |
22 |
Correct |
4 ms |
10320 KB |
Output is correct |
23 |
Correct |
4 ms |
10320 KB |
Output is correct |
24 |
Correct |
5 ms |
10488 KB |
Output is correct |
25 |
Correct |
4 ms |
10320 KB |
Output is correct |
26 |
Correct |
4 ms |
10488 KB |
Output is correct |
27 |
Correct |
3 ms |
10492 KB |
Output is correct |
28 |
Correct |
4 ms |
10320 KB |
Output is correct |
29 |
Correct |
4 ms |
10320 KB |
Output is correct |
30 |
Correct |
4 ms |
10320 KB |
Output is correct |
31 |
Correct |
4 ms |
10320 KB |
Output is correct |
32 |
Correct |
4 ms |
10424 KB |
Output is correct |
33 |
Correct |
4 ms |
10320 KB |
Output is correct |
34 |
Correct |
4 ms |
10320 KB |
Output is correct |
35 |
Correct |
4 ms |
10320 KB |
Output is correct |
36 |
Correct |
3 ms |
10320 KB |
Output is correct |
37 |
Correct |
4 ms |
10320 KB |
Output is correct |
38 |
Correct |
4 ms |
10320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10320 KB |
Output is correct |
2 |
Correct |
3 ms |
10320 KB |
Output is correct |
3 |
Correct |
3 ms |
10168 KB |
Output is correct |
4 |
Correct |
3 ms |
10488 KB |
Output is correct |
5 |
Correct |
3 ms |
10576 KB |
Output is correct |
6 |
Correct |
3 ms |
10320 KB |
Output is correct |
7 |
Correct |
3 ms |
10320 KB |
Output is correct |
8 |
Correct |
3 ms |
10320 KB |
Output is correct |
9 |
Correct |
3 ms |
10320 KB |
Output is correct |
10 |
Correct |
3 ms |
10320 KB |
Output is correct |
11 |
Correct |
3 ms |
10320 KB |
Output is correct |
12 |
Correct |
4 ms |
10344 KB |
Output is correct |
13 |
Correct |
3 ms |
10320 KB |
Output is correct |
14 |
Correct |
3 ms |
10320 KB |
Output is correct |
15 |
Correct |
4 ms |
10320 KB |
Output is correct |
16 |
Correct |
3 ms |
10320 KB |
Output is correct |
17 |
Correct |
3 ms |
10488 KB |
Output is correct |
18 |
Correct |
3 ms |
10320 KB |
Output is correct |
19 |
Correct |
126 ms |
17472 KB |
Output is correct |
20 |
Correct |
123 ms |
17404 KB |
Output is correct |
21 |
Correct |
140 ms |
17480 KB |
Output is correct |
22 |
Correct |
141 ms |
17604 KB |
Output is correct |
23 |
Correct |
59 ms |
17736 KB |
Output is correct |
24 |
Correct |
59 ms |
17664 KB |
Output is correct |
25 |
Correct |
96 ms |
21072 KB |
Output is correct |
26 |
Correct |
77 ms |
24656 KB |
Output is correct |
27 |
Correct |
153 ms |
22912 KB |
Output is correct |
28 |
Correct |
187 ms |
37448 KB |
Output is correct |
29 |
Correct |
161 ms |
36360 KB |
Output is correct |
30 |
Correct |
129 ms |
22856 KB |
Output is correct |
31 |
Correct |
120 ms |
22864 KB |
Output is correct |
32 |
Correct |
144 ms |
23368 KB |
Output is correct |
33 |
Correct |
173 ms |
21744 KB |
Output is correct |
34 |
Correct |
105 ms |
22588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10320 KB |
Output is correct |
2 |
Correct |
3 ms |
10320 KB |
Output is correct |
3 |
Correct |
3 ms |
10168 KB |
Output is correct |
4 |
Correct |
3 ms |
10488 KB |
Output is correct |
5 |
Correct |
3 ms |
10576 KB |
Output is correct |
6 |
Correct |
3 ms |
10320 KB |
Output is correct |
7 |
Correct |
3 ms |
10320 KB |
Output is correct |
8 |
Correct |
3 ms |
10320 KB |
Output is correct |
9 |
Correct |
3 ms |
10320 KB |
Output is correct |
10 |
Correct |
3 ms |
10320 KB |
Output is correct |
11 |
Correct |
3 ms |
10320 KB |
Output is correct |
12 |
Correct |
4 ms |
10344 KB |
Output is correct |
13 |
Correct |
3 ms |
10320 KB |
Output is correct |
14 |
Correct |
3 ms |
10320 KB |
Output is correct |
15 |
Correct |
4 ms |
10320 KB |
Output is correct |
16 |
Correct |
3 ms |
10320 KB |
Output is correct |
17 |
Correct |
3 ms |
10488 KB |
Output is correct |
18 |
Correct |
3 ms |
10320 KB |
Output is correct |
19 |
Correct |
4 ms |
10320 KB |
Output is correct |
20 |
Correct |
3 ms |
10320 KB |
Output is correct |
21 |
Correct |
4 ms |
10320 KB |
Output is correct |
22 |
Correct |
4 ms |
10320 KB |
Output is correct |
23 |
Correct |
4 ms |
10320 KB |
Output is correct |
24 |
Correct |
5 ms |
10488 KB |
Output is correct |
25 |
Correct |
4 ms |
10320 KB |
Output is correct |
26 |
Correct |
4 ms |
10488 KB |
Output is correct |
27 |
Correct |
3 ms |
10492 KB |
Output is correct |
28 |
Correct |
4 ms |
10320 KB |
Output is correct |
29 |
Correct |
4 ms |
10320 KB |
Output is correct |
30 |
Correct |
4 ms |
10320 KB |
Output is correct |
31 |
Correct |
4 ms |
10320 KB |
Output is correct |
32 |
Correct |
4 ms |
10424 KB |
Output is correct |
33 |
Correct |
4 ms |
10320 KB |
Output is correct |
34 |
Correct |
4 ms |
10320 KB |
Output is correct |
35 |
Correct |
4 ms |
10320 KB |
Output is correct |
36 |
Correct |
3 ms |
10320 KB |
Output is correct |
37 |
Correct |
4 ms |
10320 KB |
Output is correct |
38 |
Correct |
4 ms |
10320 KB |
Output is correct |
39 |
Correct |
126 ms |
17472 KB |
Output is correct |
40 |
Correct |
123 ms |
17404 KB |
Output is correct |
41 |
Correct |
140 ms |
17480 KB |
Output is correct |
42 |
Correct |
141 ms |
17604 KB |
Output is correct |
43 |
Correct |
59 ms |
17736 KB |
Output is correct |
44 |
Correct |
59 ms |
17664 KB |
Output is correct |
45 |
Correct |
96 ms |
21072 KB |
Output is correct |
46 |
Correct |
77 ms |
24656 KB |
Output is correct |
47 |
Correct |
153 ms |
22912 KB |
Output is correct |
48 |
Correct |
187 ms |
37448 KB |
Output is correct |
49 |
Correct |
161 ms |
36360 KB |
Output is correct |
50 |
Correct |
129 ms |
22856 KB |
Output is correct |
51 |
Correct |
120 ms |
22864 KB |
Output is correct |
52 |
Correct |
144 ms |
23368 KB |
Output is correct |
53 |
Correct |
173 ms |
21744 KB |
Output is correct |
54 |
Correct |
105 ms |
22588 KB |
Output is correct |
55 |
Correct |
15 ms |
10832 KB |
Output is correct |
56 |
Correct |
11 ms |
10832 KB |
Output is correct |
57 |
Correct |
90 ms |
17756 KB |
Output is correct |
58 |
Correct |
32 ms |
17664 KB |
Output is correct |
59 |
Correct |
204 ms |
28376 KB |
Output is correct |
60 |
Correct |
545 ms |
45132 KB |
Output is correct |
61 |
Correct |
173 ms |
23172 KB |
Output is correct |
62 |
Correct |
189 ms |
23164 KB |
Output is correct |
63 |
Correct |
213 ms |
23112 KB |
Output is correct |
64 |
Correct |
722 ms |
27764 KB |
Output is correct |
65 |
Correct |
112 ms |
23880 KB |
Output is correct |
66 |
Correct |
392 ms |
33864 KB |
Output is correct |
67 |
Correct |
113 ms |
24248 KB |
Output is correct |
68 |
Correct |
306 ms |
27464 KB |
Output is correct |
69 |
Correct |
286 ms |
27464 KB |
Output is correct |
70 |
Correct |
289 ms |
26952 KB |
Output is correct |