#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int BIGNUM = 1'000'000'000'000'000;
struct kun
{
pair<int, int> higher, lower; // depth subtree
kun()
{
higher = {BIGNUM, BIGNUM}, lower = {BIGNUM, BIGNUM};
}
};
vector<vector<pair<int, int>>> adjlist;
vector<bool> node;
vector<int> siz;
int dfs(int cur, int par)
{
siz[cur] = 1;
for (auto a : adjlist[cur])
if (a.first != par && node[a.first])
siz[cur] += dfs(a.first, cur);
return siz[cur];
}
int findcen(int cur, int par, int n)
{
for (auto a : adjlist[cur])
if (a.first != par && node[a.first])
if (siz[a.first] * 2 > n)
return findcen(a.first, cur, n);
return cur;
}
int dfscen(int cur, int par, long long dist, int depth, int head, map<long long, kun> &mii)
{
siz[cur] = 1;
auto *it = &mii[dist];
if (depth < it->higher.first)
{
if (head != it->higher.second)
swap(it->higher, it->lower);
it->higher = {depth, head};
}
else if (depth < it->lower.first && head != it->higher.second)
mii[dist].lower = {depth, head};
for (auto a : adjlist[cur])
if (a.first != par && node[a.first])
siz[cur] += dfscen(a.first, cur, dist + a.second, depth + 1, head, mii);
return siz[cur];
}
int anssubtree(int root, int n, int k)
{
dfs(root, root);
int centroid = findcen(root, root, n);
map<long long, kun> mii; /*length vector<depth, subtree>*/
for (auto a : adjlist[centroid])
if (node[a.first])
dfscen(a.first, centroid, a.second, 1, a.first, mii);
mii[0].lower = {0, 0}, mii[0].higher = {0, 0};
int shortest = BIGNUM;
for (auto &a : mii)
if (a.first <= k / 2)
{
kun tmp = mii[k - a.first];
if (a.second.higher.second != tmp.higher.second)
shortest = min(shortest, a.second.higher.first + tmp.higher.first);
if (a.second.lower.second != tmp.higher.second)
shortest = min(shortest, a.second.lower.first + tmp.higher.first);
if (a.second.higher.second != tmp.lower.second)
shortest = min(shortest, a.second.higher.first + tmp.lower.first);
if (a.second.lower.second != tmp.lower.second)
shortest = min(shortest, a.second.lower.first + tmp.lower.first);
}
node[centroid] = 0;
for (auto a : adjlist[centroid])
if (node[a.first])
shortest = min(shortest, anssubtree(a.first, siz[a.first], k));
return shortest;
}
int32_t best_path(int32_t N, int32_t K, int32_t H[][2], int32_t L[])
{
adjlist.resize(N);
for (int i = 0; i < N - 1; i++)
adjlist[H[i][0]].push_back({H[i][1], L[i]}), adjlist[H[i][1]].push_back({H[i][0], L[i]});
node.resize(N, 1);
siz.resize(N);
int x = anssubtree(0, N, K);
if (x == BIGNUM)
x = -1;
return x;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
3 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
564 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
4 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
3 ms |
468 KB |
Output is correct |
37 |
Correct |
4 ms |
652 KB |
Output is correct |
38 |
Correct |
4 ms |
608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
260 ms |
9884 KB |
Output is correct |
20 |
Correct |
241 ms |
9872 KB |
Output is correct |
21 |
Correct |
266 ms |
9760 KB |
Output is correct |
22 |
Correct |
202 ms |
9876 KB |
Output is correct |
23 |
Correct |
244 ms |
10564 KB |
Output is correct |
24 |
Correct |
123 ms |
9444 KB |
Output is correct |
25 |
Correct |
185 ms |
15688 KB |
Output is correct |
26 |
Correct |
95 ms |
15616 KB |
Output is correct |
27 |
Correct |
318 ms |
22952 KB |
Output is correct |
28 |
Correct |
1006 ms |
64944 KB |
Output is correct |
29 |
Correct |
925 ms |
64076 KB |
Output is correct |
30 |
Correct |
329 ms |
22928 KB |
Output is correct |
31 |
Correct |
328 ms |
22996 KB |
Output is correct |
32 |
Correct |
410 ms |
23044 KB |
Output is correct |
33 |
Correct |
557 ms |
22196 KB |
Output is correct |
34 |
Correct |
939 ms |
49668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
3 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
564 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
4 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
3 ms |
468 KB |
Output is correct |
37 |
Correct |
4 ms |
652 KB |
Output is correct |
38 |
Correct |
4 ms |
608 KB |
Output is correct |
39 |
Correct |
260 ms |
9884 KB |
Output is correct |
40 |
Correct |
241 ms |
9872 KB |
Output is correct |
41 |
Correct |
266 ms |
9760 KB |
Output is correct |
42 |
Correct |
202 ms |
9876 KB |
Output is correct |
43 |
Correct |
244 ms |
10564 KB |
Output is correct |
44 |
Correct |
123 ms |
9444 KB |
Output is correct |
45 |
Correct |
185 ms |
15688 KB |
Output is correct |
46 |
Correct |
95 ms |
15616 KB |
Output is correct |
47 |
Correct |
318 ms |
22952 KB |
Output is correct |
48 |
Correct |
1006 ms |
64944 KB |
Output is correct |
49 |
Correct |
925 ms |
64076 KB |
Output is correct |
50 |
Correct |
329 ms |
22928 KB |
Output is correct |
51 |
Correct |
328 ms |
22996 KB |
Output is correct |
52 |
Correct |
410 ms |
23044 KB |
Output is correct |
53 |
Correct |
557 ms |
22196 KB |
Output is correct |
54 |
Correct |
939 ms |
49668 KB |
Output is correct |
55 |
Correct |
21 ms |
2388 KB |
Output is correct |
56 |
Correct |
16 ms |
1364 KB |
Output is correct |
57 |
Correct |
172 ms |
11468 KB |
Output is correct |
58 |
Correct |
43 ms |
10300 KB |
Output is correct |
59 |
Correct |
479 ms |
39628 KB |
Output is correct |
60 |
Correct |
1468 ms |
87456 KB |
Output is correct |
61 |
Correct |
407 ms |
25396 KB |
Output is correct |
62 |
Correct |
391 ms |
23412 KB |
Output is correct |
63 |
Correct |
447 ms |
23404 KB |
Output is correct |
64 |
Correct |
1330 ms |
59668 KB |
Output is correct |
65 |
Correct |
893 ms |
50412 KB |
Output is correct |
66 |
Correct |
1218 ms |
65008 KB |
Output is correct |
67 |
Correct |
157 ms |
21924 KB |
Output is correct |
68 |
Correct |
625 ms |
47212 KB |
Output is correct |
69 |
Correct |
535 ms |
47540 KB |
Output is correct |
70 |
Correct |
535 ms |
44964 KB |
Output is correct |