#include "race.h"
#include <vector>
#include <map>
struct Graph {
private:
std::vector<std::vector<std::pair<int, int>>> adj;
int size;
std::vector<int> subtree;
std::vector<bool> removed;
std::map<int, int> min_edges;
int answer = -1;
int calc_subtree(int node, int root = -1) {
subtree[node] = 1;
for (const auto &[x, weight]: adj[node]) {
if (x == root || removed[x]) continue;
subtree[node] += calc_subtree(x, node);
}
return subtree[node];
}
int find_centroid(int node, int tree_size, int root = -1) {
for (const auto &[x, weight]: adj[node]) {
if (x == root || removed[x]) continue;
if (subtree[x] > tree_size / 2) return find_centroid(x, tree_size, node);
}
return node;
}
void search(int node, int root, bool assign, int sum, int target, int depth) {
if (sum > target) return;
if (assign) {
if (min_edges[sum] == 0 || depth < min_edges[sum]) min_edges[sum] = depth;
} else {
if (sum == target) {
if (answer == -1 || depth < answer) answer = depth;
} else {
if (min_edges[target - sum] != 0) {
if (answer == -1 || min_edges[target - sum] + depth < answer) {
answer = min_edges[target - sum] + depth;
}
}
}
}
for (const auto &[x, weight]: adj[node]) {
if (x == root || removed[x]) continue;
search(x, node, assign, sum + weight, target, depth + 1);
}
}
void decompose(int node, int target) {
int centroid = find_centroid(node, calc_subtree(node));
removed[centroid] = true;
for (const auto &[x, weight]: adj[centroid]) {
if (removed[x]) continue;
search(x, centroid, false, weight, target, 1);
search(x, centroid, true, weight, target, 1);
}
min_edges.clear();
for (const auto &[x, weight]: adj[centroid]) {
if (removed[x]) continue;
decompose(x, target);
}
}
public:
explicit Graph(int size) : size(size) {
adj.resize(size);
subtree.resize(size, 0);
removed.resize(size, false);
}
void add_edge(int x, int y, int c) {
adj[x].emplace_back(y, c);
adj[y].emplace_back(x, c);
}
int get_answer(int target) {
decompose(0, target);
return answer;
}
};
int best_path(int N, int K, int H[][2], int L[]) {
Graph tree(N);
for (int i = 0; i < N - 1; ++i) {
tree.add_edge(H[i][0], H[i][1], L[i]);
}
return tree.get_answer(K);
}
Compilation message
race.cpp: In member function 'int Graph::calc_subtree(int, int)':
race.cpp:16:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
16 | for (const auto &[x, weight]: adj[node]) {
| ^
race.cpp: In member function 'int Graph::find_centroid(int, int, int)':
race.cpp:24:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
24 | for (const auto &[x, weight]: adj[node]) {
| ^
race.cpp: In member function 'void Graph::search(int, int, bool, int, int, int)':
race.cpp:47:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
47 | for (const auto &[x, weight]: adj[node]) {
| ^
race.cpp: In member function 'void Graph::decompose(int, int)':
race.cpp:59:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | for (const auto &[x, weight]: adj[centroid]) {
| ^
race.cpp:68:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
68 | for (const auto &[x, weight]: adj[centroid]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2636 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
2 ms |
2392 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
2 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2392 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2396 KB |
Output is correct |
38 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
167 ms |
9272 KB |
Output is correct |
20 |
Correct |
165 ms |
9280 KB |
Output is correct |
21 |
Correct |
169 ms |
9292 KB |
Output is correct |
22 |
Correct |
162 ms |
9352 KB |
Output is correct |
23 |
Correct |
63 ms |
9424 KB |
Output is correct |
24 |
Correct |
65 ms |
9416 KB |
Output is correct |
25 |
Correct |
108 ms |
10840 KB |
Output is correct |
26 |
Correct |
84 ms |
15564 KB |
Output is correct |
27 |
Correct |
156 ms |
21228 KB |
Output is correct |
28 |
Correct |
196 ms |
28120 KB |
Output is correct |
29 |
Correct |
170 ms |
27456 KB |
Output is correct |
30 |
Correct |
159 ms |
21228 KB |
Output is correct |
31 |
Correct |
176 ms |
21240 KB |
Output is correct |
32 |
Correct |
154 ms |
21072 KB |
Output is correct |
33 |
Correct |
252 ms |
19792 KB |
Output is correct |
34 |
Correct |
111 ms |
20844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2636 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
2 ms |
2392 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
2 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2392 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2396 KB |
Output is correct |
38 |
Correct |
2 ms |
2396 KB |
Output is correct |
39 |
Correct |
167 ms |
9272 KB |
Output is correct |
40 |
Correct |
165 ms |
9280 KB |
Output is correct |
41 |
Correct |
169 ms |
9292 KB |
Output is correct |
42 |
Correct |
162 ms |
9352 KB |
Output is correct |
43 |
Correct |
63 ms |
9424 KB |
Output is correct |
44 |
Correct |
65 ms |
9416 KB |
Output is correct |
45 |
Correct |
108 ms |
10840 KB |
Output is correct |
46 |
Correct |
84 ms |
15564 KB |
Output is correct |
47 |
Correct |
156 ms |
21228 KB |
Output is correct |
48 |
Correct |
196 ms |
28120 KB |
Output is correct |
49 |
Correct |
170 ms |
27456 KB |
Output is correct |
50 |
Correct |
159 ms |
21228 KB |
Output is correct |
51 |
Correct |
176 ms |
21240 KB |
Output is correct |
52 |
Correct |
154 ms |
21072 KB |
Output is correct |
53 |
Correct |
252 ms |
19792 KB |
Output is correct |
54 |
Correct |
111 ms |
20844 KB |
Output is correct |
55 |
Correct |
23 ms |
3416 KB |
Output is correct |
56 |
Correct |
17 ms |
3164 KB |
Output is correct |
57 |
Correct |
135 ms |
10868 KB |
Output is correct |
58 |
Correct |
49 ms |
10436 KB |
Output is correct |
59 |
Correct |
338 ms |
22228 KB |
Output is correct |
60 |
Correct |
949 ms |
47272 KB |
Output is correct |
61 |
Correct |
267 ms |
21620 KB |
Output is correct |
62 |
Correct |
276 ms |
21552 KB |
Output is correct |
63 |
Correct |
300 ms |
21664 KB |
Output is correct |
64 |
Correct |
1060 ms |
30728 KB |
Output is correct |
65 |
Correct |
141 ms |
21972 KB |
Output is correct |
66 |
Correct |
618 ms |
26724 KB |
Output is correct |
67 |
Correct |
89 ms |
21960 KB |
Output is correct |
68 |
Correct |
385 ms |
29036 KB |
Output is correct |
69 |
Correct |
456 ms |
29216 KB |
Output is correct |
70 |
Correct |
405 ms |
28232 KB |
Output is correct |