#include <iostream>
#include <cstdio>
#include <map>
#include <functional>
#include <vector>
//#include "race.h"
using namespace std;
using ll = long long;
using ld = long double;
constexpr int N = 2e5 + 5;
constexpr int Inf = 1e9 + 7;
ll d[N];
vector<pair<int, int>> adj[N];
map<ll, int> cnt[N];
int ranks[N];
int Get(map<ll, int> &cnt, ll x)
{
auto j = cnt.find(x);
return j == cnt.end() ? Inf * 2 : j->second;
}
void Update(map<ll, int> &cnt, ll x, int y)
{
auto j = cnt.find(x);
if (j == cnt.end())
cnt[x] = y;
else
j->second = min(j->second, y);
}
int best_path(int n, int k, int h[][2], int l[])
{
int ans(Inf);
function<void(int, int)> dfs = [&](int v, int p)
{
cnt[v][d[v]] = ranks[v];
for (auto i : adj[v])
if (i.first != p)
{
d[i.first] = d[v] + i.second;
ranks[i.first] = ranks[v] + 1;
dfs(i.first, v);
if (cnt[v].size() < cnt[i.first].size())
swap(cnt[i.first], cnt[v]);
for (auto j : cnt[i.first])
if (j.first - d[v] <= k)
ans = min(ans, Get(cnt[v], k - (j.first - d[v]) + d[v]) + j.second - 2 * ranks[v]);
for (auto j : cnt[i.first])
if (j.first - d[v] <= k)
Update(cnt[v], j.first, j.second);
cnt[i.first].clear();
}
};
for (int i = 0; i < n - 1; ++i)
{
adj[h[i][0] + 1].emplace_back(h[i][1] + 1, l[i]);
adj[h[i][1] + 1].emplace_back(h[i][0] + 1, l[i]);
}
ranks[1] = 1;
dfs(1, -1);
return ans >= Inf ? -1 : ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14392 KB |
Output is correct |
4 |
Correct |
7 ms |
14396 KB |
Output is correct |
5 |
Correct |
7 ms |
14412 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
7 ms |
14372 KB |
Output is correct |
8 |
Correct |
7 ms |
14412 KB |
Output is correct |
9 |
Correct |
7 ms |
14396 KB |
Output is correct |
10 |
Correct |
7 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14396 KB |
Output is correct |
12 |
Correct |
7 ms |
14404 KB |
Output is correct |
13 |
Correct |
7 ms |
14400 KB |
Output is correct |
14 |
Correct |
8 ms |
14392 KB |
Output is correct |
15 |
Correct |
7 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14392 KB |
Output is correct |
4 |
Correct |
7 ms |
14396 KB |
Output is correct |
5 |
Correct |
7 ms |
14412 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
7 ms |
14372 KB |
Output is correct |
8 |
Correct |
7 ms |
14412 KB |
Output is correct |
9 |
Correct |
7 ms |
14396 KB |
Output is correct |
10 |
Correct |
7 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14396 KB |
Output is correct |
12 |
Correct |
7 ms |
14404 KB |
Output is correct |
13 |
Correct |
7 ms |
14400 KB |
Output is correct |
14 |
Correct |
8 ms |
14392 KB |
Output is correct |
15 |
Correct |
7 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14356 KB |
Output is correct |
19 |
Correct |
7 ms |
14392 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
8 ms |
14512 KB |
Output is correct |
22 |
Correct |
8 ms |
14432 KB |
Output is correct |
23 |
Correct |
8 ms |
14412 KB |
Output is correct |
24 |
Correct |
8 ms |
14412 KB |
Output is correct |
25 |
Correct |
8 ms |
14540 KB |
Output is correct |
26 |
Correct |
8 ms |
14412 KB |
Output is correct |
27 |
Correct |
8 ms |
14408 KB |
Output is correct |
28 |
Correct |
9 ms |
14540 KB |
Output is correct |
29 |
Correct |
9 ms |
14540 KB |
Output is correct |
30 |
Correct |
9 ms |
14540 KB |
Output is correct |
31 |
Correct |
9 ms |
14516 KB |
Output is correct |
32 |
Correct |
8 ms |
14536 KB |
Output is correct |
33 |
Correct |
9 ms |
14540 KB |
Output is correct |
34 |
Correct |
8 ms |
14536 KB |
Output is correct |
35 |
Correct |
8 ms |
14540 KB |
Output is correct |
36 |
Correct |
8 ms |
14540 KB |
Output is correct |
37 |
Correct |
8 ms |
14468 KB |
Output is correct |
38 |
Correct |
9 ms |
14532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14392 KB |
Output is correct |
4 |
Correct |
7 ms |
14396 KB |
Output is correct |
5 |
Correct |
7 ms |
14412 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
7 ms |
14372 KB |
Output is correct |
8 |
Correct |
7 ms |
14412 KB |
Output is correct |
9 |
Correct |
7 ms |
14396 KB |
Output is correct |
10 |
Correct |
7 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14396 KB |
Output is correct |
12 |
Correct |
7 ms |
14404 KB |
Output is correct |
13 |
Correct |
7 ms |
14400 KB |
Output is correct |
14 |
Correct |
8 ms |
14392 KB |
Output is correct |
15 |
Correct |
7 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14356 KB |
Output is correct |
19 |
Correct |
91 ms |
21956 KB |
Output is correct |
20 |
Correct |
104 ms |
21888 KB |
Output is correct |
21 |
Correct |
83 ms |
21840 KB |
Output is correct |
22 |
Correct |
91 ms |
21840 KB |
Output is correct |
23 |
Correct |
75 ms |
22080 KB |
Output is correct |
24 |
Correct |
80 ms |
21944 KB |
Output is correct |
25 |
Correct |
84 ms |
34640 KB |
Output is correct |
26 |
Correct |
60 ms |
46148 KB |
Output is correct |
27 |
Correct |
143 ms |
29588 KB |
Output is correct |
28 |
Correct |
335 ms |
78284 KB |
Output is correct |
29 |
Correct |
293 ms |
75592 KB |
Output is correct |
30 |
Correct |
143 ms |
29632 KB |
Output is correct |
31 |
Correct |
147 ms |
29764 KB |
Output is correct |
32 |
Correct |
176 ms |
29788 KB |
Output is correct |
33 |
Correct |
159 ms |
29248 KB |
Output is correct |
34 |
Correct |
128 ms |
29876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14392 KB |
Output is correct |
4 |
Correct |
7 ms |
14396 KB |
Output is correct |
5 |
Correct |
7 ms |
14412 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
7 ms |
14372 KB |
Output is correct |
8 |
Correct |
7 ms |
14412 KB |
Output is correct |
9 |
Correct |
7 ms |
14396 KB |
Output is correct |
10 |
Correct |
7 ms |
14412 KB |
Output is correct |
11 |
Correct |
7 ms |
14396 KB |
Output is correct |
12 |
Correct |
7 ms |
14404 KB |
Output is correct |
13 |
Correct |
7 ms |
14400 KB |
Output is correct |
14 |
Correct |
8 ms |
14392 KB |
Output is correct |
15 |
Correct |
7 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14412 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14356 KB |
Output is correct |
19 |
Correct |
7 ms |
14392 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
8 ms |
14512 KB |
Output is correct |
22 |
Correct |
8 ms |
14432 KB |
Output is correct |
23 |
Correct |
8 ms |
14412 KB |
Output is correct |
24 |
Correct |
8 ms |
14412 KB |
Output is correct |
25 |
Correct |
8 ms |
14540 KB |
Output is correct |
26 |
Correct |
8 ms |
14412 KB |
Output is correct |
27 |
Correct |
8 ms |
14408 KB |
Output is correct |
28 |
Correct |
9 ms |
14540 KB |
Output is correct |
29 |
Correct |
9 ms |
14540 KB |
Output is correct |
30 |
Correct |
9 ms |
14540 KB |
Output is correct |
31 |
Correct |
9 ms |
14516 KB |
Output is correct |
32 |
Correct |
8 ms |
14536 KB |
Output is correct |
33 |
Correct |
9 ms |
14540 KB |
Output is correct |
34 |
Correct |
8 ms |
14536 KB |
Output is correct |
35 |
Correct |
8 ms |
14540 KB |
Output is correct |
36 |
Correct |
8 ms |
14540 KB |
Output is correct |
37 |
Correct |
8 ms |
14468 KB |
Output is correct |
38 |
Correct |
9 ms |
14532 KB |
Output is correct |
39 |
Correct |
91 ms |
21956 KB |
Output is correct |
40 |
Correct |
104 ms |
21888 KB |
Output is correct |
41 |
Correct |
83 ms |
21840 KB |
Output is correct |
42 |
Correct |
91 ms |
21840 KB |
Output is correct |
43 |
Correct |
75 ms |
22080 KB |
Output is correct |
44 |
Correct |
80 ms |
21944 KB |
Output is correct |
45 |
Correct |
84 ms |
34640 KB |
Output is correct |
46 |
Correct |
60 ms |
46148 KB |
Output is correct |
47 |
Correct |
143 ms |
29588 KB |
Output is correct |
48 |
Correct |
335 ms |
78284 KB |
Output is correct |
49 |
Correct |
293 ms |
75592 KB |
Output is correct |
50 |
Correct |
143 ms |
29632 KB |
Output is correct |
51 |
Correct |
147 ms |
29764 KB |
Output is correct |
52 |
Correct |
176 ms |
29788 KB |
Output is correct |
53 |
Correct |
159 ms |
29248 KB |
Output is correct |
54 |
Correct |
128 ms |
29876 KB |
Output is correct |
55 |
Correct |
18 ms |
15432 KB |
Output is correct |
56 |
Correct |
13 ms |
15052 KB |
Output is correct |
57 |
Correct |
54 ms |
21920 KB |
Output is correct |
58 |
Correct |
46 ms |
21620 KB |
Output is correct |
59 |
Correct |
85 ms |
46232 KB |
Output is correct |
60 |
Correct |
270 ms |
77016 KB |
Output is correct |
61 |
Correct |
155 ms |
30020 KB |
Output is correct |
62 |
Correct |
147 ms |
29892 KB |
Output is correct |
63 |
Correct |
182 ms |
30004 KB |
Output is correct |
64 |
Correct |
376 ms |
38464 KB |
Output is correct |
65 |
Correct |
160 ms |
30404 KB |
Output is correct |
66 |
Correct |
270 ms |
69572 KB |
Output is correct |
67 |
Correct |
146 ms |
30544 KB |
Output is correct |
68 |
Correct |
284 ms |
39616 KB |
Output is correct |
69 |
Correct |
301 ms |
43072 KB |
Output is correct |
70 |
Correct |
271 ms |
38724 KB |
Output is correct |