#include <bits/stdc++.h>
using namespace std;
#include "race.h"
vector<set<pair<int64_t, int64_t>>> s;
vector<pair<int64_t, int64_t>> c;
vector<vector<pair<int64_t, int64_t>>> g;
int64_t min_edges_num = INT_MAX;
void find_best(int64_t u, int64_t p, int64_t k)
{
if (g[u].size() == 1 && g[u].front().first == p)
{
s[u].emplace(0, 0);
c[u] = {0, 0};
return;
}
int64_t max_v, max_size = 0, max_w;
for (auto const &[v, w] : g[u])
if (v != p)
{
find_best(v, u, k);
if (s[v].size() >= max_size)
max_v = v, max_size = s[v].size(), max_w = w;
}
swap(s[u], s[max_v]);
swap(c[u], c[max_v]);
c[u].first += max_w;
c[u].second++;
for (auto const &[v, w] : g[u])
if (v != p && v != max_v)
{
for (auto const &[length, num_edges] : s[v])
{
auto it = s[u].lower_bound({k - (length + w + c[v].first) - c[u].first, INT_MIN});
if (it != s[u].end() && it->first + c[u].first + length + w + c[v].first == k)
min_edges_num = min(min_edges_num, it->second + c[u].second + num_edges + 1 + c[v].second);
}
for (auto const &[length, num_edges] : s[v])
{
s[u].emplace(length + w + c[v].first - c[u].first,
num_edges + 1 + c[v].second - c[u].second);
}
}
auto it = s[u].lower_bound({k - c[u].first, INT_MIN});
if (it != s[u].end() && it->first + c[u].first == k)
min_edges_num = min(min_edges_num, it->second + c[u].second);
s[u].emplace(-c[u].first, -c[u].second);
}
int best_path(int N, int K, int H[][2], int L[])
{
g = vector<vector<pair<int64_t, int64_t>>>(N);
s = vector<set<pair<int64_t, int64_t>>>(N);
c = vector<pair<int64_t, int64_t>>(N);
for (int64_t i = 0; i < N - 1; i++)
{
g[H[i][0]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
find_best(0, -1, K);
return min_edges_num == INT_MAX ? -1 : min_edges_num;
}
Compilation message
race.cpp: In function 'void find_best(int64_t, int64_t, int64_t)':
race.cpp:25:29: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} and 'int64_t' {aka 'long int'} [-Wsign-compare]
25 | if (s[v].size() >= max_size)
| ~~~~~~~~~~~~^~~~~~~~~~~
race.cpp:31:16: warning: 'max_w' may be used uninitialized in this function [-Wmaybe-uninitialized]
31 | c[u].first += max_w;
race.cpp:35:20: warning: 'max_v' may be used uninitialized in this function [-Wmaybe-uninitialized]
35 | if (v != p && v != max_v)
| ~~~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
572 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
576 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
124 ms |
32292 KB |
Output is correct |
20 |
Correct |
125 ms |
32148 KB |
Output is correct |
21 |
Correct |
135 ms |
31736 KB |
Output is correct |
22 |
Correct |
120 ms |
30924 KB |
Output is correct |
23 |
Correct |
158 ms |
46200 KB |
Output is correct |
24 |
Correct |
119 ms |
34424 KB |
Output is correct |
25 |
Correct |
106 ms |
36836 KB |
Output is correct |
26 |
Correct |
80 ms |
44304 KB |
Output is correct |
27 |
Correct |
193 ms |
49336 KB |
Output is correct |
28 |
Correct |
235 ms |
88552 KB |
Output is correct |
29 |
Correct |
249 ms |
86252 KB |
Output is correct |
30 |
Correct |
186 ms |
49456 KB |
Output is correct |
31 |
Correct |
176 ms |
49356 KB |
Output is correct |
32 |
Correct |
238 ms |
49364 KB |
Output is correct |
33 |
Correct |
200 ms |
45164 KB |
Output is correct |
34 |
Correct |
295 ms |
77980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
572 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
576 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
124 ms |
32292 KB |
Output is correct |
40 |
Correct |
125 ms |
32148 KB |
Output is correct |
41 |
Correct |
135 ms |
31736 KB |
Output is correct |
42 |
Correct |
120 ms |
30924 KB |
Output is correct |
43 |
Correct |
158 ms |
46200 KB |
Output is correct |
44 |
Correct |
119 ms |
34424 KB |
Output is correct |
45 |
Correct |
106 ms |
36836 KB |
Output is correct |
46 |
Correct |
80 ms |
44304 KB |
Output is correct |
47 |
Correct |
193 ms |
49336 KB |
Output is correct |
48 |
Correct |
235 ms |
88552 KB |
Output is correct |
49 |
Correct |
249 ms |
86252 KB |
Output is correct |
50 |
Correct |
186 ms |
49456 KB |
Output is correct |
51 |
Correct |
176 ms |
49356 KB |
Output is correct |
52 |
Correct |
238 ms |
49364 KB |
Output is correct |
53 |
Correct |
200 ms |
45164 KB |
Output is correct |
54 |
Correct |
295 ms |
77980 KB |
Output is correct |
55 |
Correct |
11 ms |
4308 KB |
Output is correct |
56 |
Correct |
8 ms |
3036 KB |
Output is correct |
57 |
Correct |
71 ms |
28072 KB |
Output is correct |
58 |
Correct |
50 ms |
22076 KB |
Output is correct |
59 |
Correct |
74 ms |
44244 KB |
Output is correct |
60 |
Correct |
222 ms |
86904 KB |
Output is correct |
61 |
Correct |
222 ms |
55160 KB |
Output is correct |
62 |
Correct |
184 ms |
49428 KB |
Output is correct |
63 |
Correct |
210 ms |
49392 KB |
Output is correct |
64 |
Correct |
369 ms |
103092 KB |
Output is correct |
65 |
Correct |
374 ms |
97332 KB |
Output is correct |
66 |
Correct |
257 ms |
81236 KB |
Output is correct |
67 |
Correct |
171 ms |
45292 KB |
Output is correct |
68 |
Correct |
281 ms |
65856 KB |
Output is correct |
69 |
Correct |
284 ms |
66492 KB |
Output is correct |
70 |
Correct |
274 ms |
62968 KB |
Output is correct |