#include "race.h"
#include <vector>
#include <array>
using namespace std;
using ll = long long;
struct CentroidDecomposition
{
int n, k;
vector<bool> used;
vector<vector<pair<int, ll>>> adj;
vector<int> sizes, del1, del2;
array<int, 1000001> counter1{}, counter2{};
int res = -1;
int calc(int v, int p)
{
sizes[v] = 1;
for (auto [u, w] : adj[v]) if (u != p && !used[u]) sizes[v] += calc(u, v);
return sizes[v];
}
int findCentroid(int v, int p, int all)
{
for (auto [u, w] : adj[v]) if (u != p && !used[u] && sizes[u] > all / 2) return findCentroid(u, v, all);
return v;
}
void dfs(int v, int p, int l, ll d)
{
if (d <= k)
{
counter2[d] = min(counter2[d], l);
del1.push_back(d);
del2.push_back(d);
}
for (auto [u, w] : adj[v]) if (u != p && !used[u]) dfs(u, v, l + 1, d + w);
}
int build(int v)
{
int all = calc(v, 0);
int centroid = findCentroid(v, 0, all);
//calc res
int res = 1e9;
counter1[0] = 0;
del1.push_back(0);
for (auto [u, w]: adj[centroid])
{
if (!used[u])
{
dfs(u, centroid, 1, w);
for (int i : del2) if (k - i >= 0) res = min(res, counter2[i] + counter1[k - i]);
for (int i : del2)
{
counter1[i] = min(counter1[i], counter2[i]);
counter2[i] = 1e9;
}
del2.clear();
}
}
for (int i : del1) counter1[i] = 1e9;
del1.clear();
used[centroid] = true;
for (auto [u, w] : adj[centroid]) if (!used[u]) res = min(res, build(u));
return res;
}
CentroidDecomposition(vector<vector<pair<int, ll>>> adj, int k) : adj(adj), k(k)
{
int n = (int)adj.size() - 1;
used.resize(n + 1);
sizes.resize(n + 1);
counter1.fill(1e9);
counter2.fill(1e9);
res = build(0);
if (res == 1e9) res = -1;
}
int operator()()
{
return res;
}
};
int best_path(int n, int k, int H[][2], int L[])
{
vector<vector<pair<int, ll>>> adj(n + 1);
for (int i = 0; i + 1 < n; ++i)
{
adj[H[i][0]].emplace_back(H[i][1], L[i]);
adj[H[i][1]].emplace_back(H[i][0], L[i]);
}
return CentroidDecomposition(adj, k)();
}
Compilation message
race.cpp: In constructor 'CentroidDecomposition::CentroidDecomposition(std::vector<std::vector<std::pair<int, long long int> > >, int)':
race.cpp:13:35: warning: 'CentroidDecomposition::adj' will be initialized after [-Wreorder]
13 | vector<vector<pair<int, ll>>> adj;
| ^~~
race.cpp:11:12: warning: 'int CentroidDecomposition::k' [-Wreorder]
11 | int n, k;
| ^
race.cpp:71:5: warning: when initialized here [-Wreorder]
71 | CentroidDecomposition(vector<vector<pair<int, ll>>> adj, int k) : adj(adj), k(k)
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
7 ms |
8152 KB |
Output is correct |
4 |
Correct |
5 ms |
8100 KB |
Output is correct |
5 |
Correct |
5 ms |
8104 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
6 ms |
8104 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
6 ms |
8020 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
6 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8144 KB |
Output is correct |
15 |
Correct |
5 ms |
8104 KB |
Output is correct |
16 |
Correct |
5 ms |
8108 KB |
Output is correct |
17 |
Correct |
5 ms |
8100 KB |
Output is correct |
18 |
Correct |
6 ms |
8104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
7 ms |
8152 KB |
Output is correct |
4 |
Correct |
5 ms |
8100 KB |
Output is correct |
5 |
Correct |
5 ms |
8104 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
6 ms |
8104 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
6 ms |
8020 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
6 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8144 KB |
Output is correct |
15 |
Correct |
5 ms |
8104 KB |
Output is correct |
16 |
Correct |
5 ms |
8108 KB |
Output is correct |
17 |
Correct |
5 ms |
8100 KB |
Output is correct |
18 |
Correct |
6 ms |
8104 KB |
Output is correct |
19 |
Correct |
5 ms |
8020 KB |
Output is correct |
20 |
Correct |
5 ms |
8148 KB |
Output is correct |
21 |
Correct |
6 ms |
8276 KB |
Output is correct |
22 |
Correct |
6 ms |
8276 KB |
Output is correct |
23 |
Correct |
6 ms |
8360 KB |
Output is correct |
24 |
Correct |
6 ms |
8276 KB |
Output is correct |
25 |
Correct |
6 ms |
8364 KB |
Output is correct |
26 |
Correct |
6 ms |
8276 KB |
Output is correct |
27 |
Correct |
6 ms |
8276 KB |
Output is correct |
28 |
Correct |
6 ms |
8276 KB |
Output is correct |
29 |
Correct |
9 ms |
8276 KB |
Output is correct |
30 |
Correct |
6 ms |
8276 KB |
Output is correct |
31 |
Correct |
6 ms |
8276 KB |
Output is correct |
32 |
Correct |
6 ms |
8376 KB |
Output is correct |
33 |
Correct |
7 ms |
8276 KB |
Output is correct |
34 |
Correct |
6 ms |
8244 KB |
Output is correct |
35 |
Correct |
6 ms |
8404 KB |
Output is correct |
36 |
Correct |
6 ms |
8276 KB |
Output is correct |
37 |
Correct |
6 ms |
8236 KB |
Output is correct |
38 |
Correct |
6 ms |
8344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
7 ms |
8152 KB |
Output is correct |
4 |
Correct |
5 ms |
8100 KB |
Output is correct |
5 |
Correct |
5 ms |
8104 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
6 ms |
8104 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
6 ms |
8020 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
6 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8144 KB |
Output is correct |
15 |
Correct |
5 ms |
8104 KB |
Output is correct |
16 |
Correct |
5 ms |
8108 KB |
Output is correct |
17 |
Correct |
5 ms |
8100 KB |
Output is correct |
18 |
Correct |
6 ms |
8104 KB |
Output is correct |
19 |
Correct |
165 ms |
32720 KB |
Output is correct |
20 |
Correct |
175 ms |
32716 KB |
Output is correct |
21 |
Correct |
149 ms |
32972 KB |
Output is correct |
22 |
Correct |
153 ms |
33412 KB |
Output is correct |
23 |
Correct |
155 ms |
33044 KB |
Output is correct |
24 |
Correct |
93 ms |
32268 KB |
Output is correct |
25 |
Correct |
226 ms |
34324 KB |
Output is correct |
26 |
Correct |
114 ms |
37044 KB |
Output is correct |
27 |
Correct |
202 ms |
57228 KB |
Output is correct |
28 |
Correct |
691 ms |
64676 KB |
Output is correct |
29 |
Correct |
735 ms |
64076 KB |
Output is correct |
30 |
Correct |
233 ms |
57276 KB |
Output is correct |
31 |
Correct |
197 ms |
57160 KB |
Output is correct |
32 |
Correct |
323 ms |
57388 KB |
Output is correct |
33 |
Correct |
493 ms |
56360 KB |
Output is correct |
34 |
Correct |
559 ms |
57208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
7 ms |
8152 KB |
Output is correct |
4 |
Correct |
5 ms |
8100 KB |
Output is correct |
5 |
Correct |
5 ms |
8104 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
6 ms |
8104 KB |
Output is correct |
9 |
Correct |
5 ms |
8148 KB |
Output is correct |
10 |
Correct |
5 ms |
8148 KB |
Output is correct |
11 |
Correct |
6 ms |
8020 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
6 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8144 KB |
Output is correct |
15 |
Correct |
5 ms |
8104 KB |
Output is correct |
16 |
Correct |
5 ms |
8108 KB |
Output is correct |
17 |
Correct |
5 ms |
8100 KB |
Output is correct |
18 |
Correct |
6 ms |
8104 KB |
Output is correct |
19 |
Correct |
5 ms |
8020 KB |
Output is correct |
20 |
Correct |
5 ms |
8148 KB |
Output is correct |
21 |
Correct |
6 ms |
8276 KB |
Output is correct |
22 |
Correct |
6 ms |
8276 KB |
Output is correct |
23 |
Correct |
6 ms |
8360 KB |
Output is correct |
24 |
Correct |
6 ms |
8276 KB |
Output is correct |
25 |
Correct |
6 ms |
8364 KB |
Output is correct |
26 |
Correct |
6 ms |
8276 KB |
Output is correct |
27 |
Correct |
6 ms |
8276 KB |
Output is correct |
28 |
Correct |
6 ms |
8276 KB |
Output is correct |
29 |
Correct |
9 ms |
8276 KB |
Output is correct |
30 |
Correct |
6 ms |
8276 KB |
Output is correct |
31 |
Correct |
6 ms |
8276 KB |
Output is correct |
32 |
Correct |
6 ms |
8376 KB |
Output is correct |
33 |
Correct |
7 ms |
8276 KB |
Output is correct |
34 |
Correct |
6 ms |
8244 KB |
Output is correct |
35 |
Correct |
6 ms |
8404 KB |
Output is correct |
36 |
Correct |
6 ms |
8276 KB |
Output is correct |
37 |
Correct |
6 ms |
8236 KB |
Output is correct |
38 |
Correct |
6 ms |
8344 KB |
Output is correct |
39 |
Correct |
165 ms |
32720 KB |
Output is correct |
40 |
Correct |
175 ms |
32716 KB |
Output is correct |
41 |
Correct |
149 ms |
32972 KB |
Output is correct |
42 |
Correct |
153 ms |
33412 KB |
Output is correct |
43 |
Correct |
155 ms |
33044 KB |
Output is correct |
44 |
Correct |
93 ms |
32268 KB |
Output is correct |
45 |
Correct |
226 ms |
34324 KB |
Output is correct |
46 |
Correct |
114 ms |
37044 KB |
Output is correct |
47 |
Correct |
202 ms |
57228 KB |
Output is correct |
48 |
Correct |
691 ms |
64676 KB |
Output is correct |
49 |
Correct |
735 ms |
64076 KB |
Output is correct |
50 |
Correct |
233 ms |
57276 KB |
Output is correct |
51 |
Correct |
197 ms |
57160 KB |
Output is correct |
52 |
Correct |
323 ms |
57388 KB |
Output is correct |
53 |
Correct |
493 ms |
56360 KB |
Output is correct |
54 |
Correct |
559 ms |
57208 KB |
Output is correct |
55 |
Correct |
16 ms |
10656 KB |
Output is correct |
56 |
Correct |
17 ms |
10664 KB |
Output is correct |
57 |
Correct |
99 ms |
33720 KB |
Output is correct |
58 |
Correct |
57 ms |
32668 KB |
Output is correct |
59 |
Correct |
107 ms |
37016 KB |
Output is correct |
60 |
Correct |
850 ms |
65512 KB |
Output is correct |
61 |
Correct |
212 ms |
57676 KB |
Output is correct |
62 |
Correct |
221 ms |
58344 KB |
Output is correct |
63 |
Correct |
306 ms |
58316 KB |
Output is correct |
64 |
Correct |
550 ms |
59288 KB |
Output is correct |
65 |
Correct |
392 ms |
58292 KB |
Output is correct |
66 |
Correct |
805 ms |
62940 KB |
Output is correct |
67 |
Correct |
190 ms |
58752 KB |
Output is correct |
68 |
Correct |
328 ms |
57964 KB |
Output is correct |
69 |
Correct |
351 ms |
58092 KB |
Output is correct |
70 |
Correct |
280 ms |
55656 KB |
Output is correct |