#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const int N = 2e5 + 1;
int n, k, max_depth;
int sz[N];
bool vis[N];
vector<pair<int, int>> adj[N];
int res = INT_MAX;
map<int, int> cnt;
int get_subtree_size(int u, int p)
{
sz[u] = 1;
for (auto [v, w] : adj[u])
{
if (v != p and !vis[v])
sz[u] += get_subtree_size(v, u);
}
return sz[u];
}
int get_centroid(int u, int p, int tree_size)
{
for (auto [v, w] : adj[u])
{
if (v != p and !vis[v] and (sz[v] * 2 > tree_size))
return get_centroid(v, u, tree_size);
}
return u;
}
void calc(int u, int p, int fill, int weight, int depth = 1)
{
if (fill == 1)
{
if (cnt.count(weight))
{
cnt[weight] = min(cnt[weight], depth);
}
else
{
cnt[weight] = depth;
}
}
else if ((weight <= k) and (cnt.count(k - weight)))
{
res = min(res, depth + cnt[k - weight]);
}
for (auto [v, w] : adj[u])
{
if (!vis[v] and (v != p))
calc(v, u, fill, weight + w, depth + 1);
}
}
void decompose(int u, int p)
{
int tree_size = get_subtree_size(u, p);
int centroid = get_centroid(u, p, tree_size);
vis[centroid] = true;
cnt.clear();
cnt[0] = 0;
for (auto [v, w] : adj[centroid])
{
if (!vis[v] and v != p)
calc(v, centroid, 0, w), calc(v, centroid, 1, w);
}
for (auto [v, w] : adj[centroid])
{
if (!vis[v] and v != p)
decompose(v, centroid);
}
}
int best_path(int N, int K, int edges[][2], int weight[2])
{
n = N;
k = K;
for (int i = 0; i < n - 1; ++i)
{
int u, v, w;
u = edges[i][0];
v = edges[i][1];
w = weight[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
decompose(0, -1);
if (res == INT_MAX)
return -1;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
8784 KB |
Output is correct |
3 |
Correct |
2 ms |
8784 KB |
Output is correct |
4 |
Correct |
2 ms |
8784 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
2 ms |
8784 KB |
Output is correct |
7 |
Correct |
2 ms |
8784 KB |
Output is correct |
8 |
Correct |
2 ms |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8784 KB |
Output is correct |
10 |
Correct |
2 ms |
8788 KB |
Output is correct |
11 |
Correct |
2 ms |
8784 KB |
Output is correct |
12 |
Correct |
2 ms |
8784 KB |
Output is correct |
13 |
Correct |
2 ms |
8788 KB |
Output is correct |
14 |
Correct |
2 ms |
8784 KB |
Output is correct |
15 |
Correct |
2 ms |
8784 KB |
Output is correct |
16 |
Correct |
2 ms |
8784 KB |
Output is correct |
17 |
Correct |
2 ms |
8952 KB |
Output is correct |
18 |
Correct |
2 ms |
8784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
8784 KB |
Output is correct |
3 |
Correct |
2 ms |
8784 KB |
Output is correct |
4 |
Correct |
2 ms |
8784 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
2 ms |
8784 KB |
Output is correct |
7 |
Correct |
2 ms |
8784 KB |
Output is correct |
8 |
Correct |
2 ms |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8784 KB |
Output is correct |
10 |
Correct |
2 ms |
8788 KB |
Output is correct |
11 |
Correct |
2 ms |
8784 KB |
Output is correct |
12 |
Correct |
2 ms |
8784 KB |
Output is correct |
13 |
Correct |
2 ms |
8788 KB |
Output is correct |
14 |
Correct |
2 ms |
8784 KB |
Output is correct |
15 |
Correct |
2 ms |
8784 KB |
Output is correct |
16 |
Correct |
2 ms |
8784 KB |
Output is correct |
17 |
Correct |
2 ms |
8952 KB |
Output is correct |
18 |
Correct |
2 ms |
8784 KB |
Output is correct |
19 |
Correct |
2 ms |
8784 KB |
Output is correct |
20 |
Correct |
2 ms |
8784 KB |
Output is correct |
21 |
Correct |
3 ms |
8784 KB |
Output is correct |
22 |
Correct |
4 ms |
6736 KB |
Output is correct |
23 |
Correct |
4 ms |
6736 KB |
Output is correct |
24 |
Correct |
4 ms |
6864 KB |
Output is correct |
25 |
Correct |
5 ms |
6736 KB |
Output is correct |
26 |
Correct |
4 ms |
6736 KB |
Output is correct |
27 |
Correct |
4 ms |
6908 KB |
Output is correct |
28 |
Correct |
4 ms |
6736 KB |
Output is correct |
29 |
Correct |
4 ms |
6752 KB |
Output is correct |
30 |
Correct |
3 ms |
6736 KB |
Output is correct |
31 |
Correct |
4 ms |
6736 KB |
Output is correct |
32 |
Correct |
4 ms |
6652 KB |
Output is correct |
33 |
Correct |
4 ms |
6736 KB |
Output is correct |
34 |
Correct |
4 ms |
6736 KB |
Output is correct |
35 |
Correct |
4 ms |
6848 KB |
Output is correct |
36 |
Correct |
4 ms |
6736 KB |
Output is correct |
37 |
Correct |
4 ms |
6868 KB |
Output is correct |
38 |
Correct |
4 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
8784 KB |
Output is correct |
3 |
Correct |
2 ms |
8784 KB |
Output is correct |
4 |
Correct |
2 ms |
8784 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
2 ms |
8784 KB |
Output is correct |
7 |
Correct |
2 ms |
8784 KB |
Output is correct |
8 |
Correct |
2 ms |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8784 KB |
Output is correct |
10 |
Correct |
2 ms |
8788 KB |
Output is correct |
11 |
Correct |
2 ms |
8784 KB |
Output is correct |
12 |
Correct |
2 ms |
8784 KB |
Output is correct |
13 |
Correct |
2 ms |
8788 KB |
Output is correct |
14 |
Correct |
2 ms |
8784 KB |
Output is correct |
15 |
Correct |
2 ms |
8784 KB |
Output is correct |
16 |
Correct |
2 ms |
8784 KB |
Output is correct |
17 |
Correct |
2 ms |
8952 KB |
Output is correct |
18 |
Correct |
2 ms |
8784 KB |
Output is correct |
19 |
Correct |
164 ms |
14620 KB |
Output is correct |
20 |
Correct |
160 ms |
16464 KB |
Output is correct |
21 |
Correct |
144 ms |
15176 KB |
Output is correct |
22 |
Correct |
129 ms |
16200 KB |
Output is correct |
23 |
Correct |
209 ms |
15432 KB |
Output is correct |
24 |
Correct |
88 ms |
16272 KB |
Output is correct |
25 |
Correct |
134 ms |
22344 KB |
Output is correct |
26 |
Correct |
72 ms |
20812 KB |
Output is correct |
27 |
Correct |
221 ms |
19732 KB |
Output is correct |
28 |
Correct |
549 ms |
40008 KB |
Output is correct |
29 |
Correct |
579 ms |
42232 KB |
Output is correct |
30 |
Correct |
202 ms |
19528 KB |
Output is correct |
31 |
Correct |
206 ms |
21824 KB |
Output is correct |
32 |
Correct |
233 ms |
22512 KB |
Output is correct |
33 |
Correct |
321 ms |
18504 KB |
Output is correct |
34 |
Correct |
556 ms |
29768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
8784 KB |
Output is correct |
3 |
Correct |
2 ms |
8784 KB |
Output is correct |
4 |
Correct |
2 ms |
8784 KB |
Output is correct |
5 |
Correct |
2 ms |
8784 KB |
Output is correct |
6 |
Correct |
2 ms |
8784 KB |
Output is correct |
7 |
Correct |
2 ms |
8784 KB |
Output is correct |
8 |
Correct |
2 ms |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8784 KB |
Output is correct |
10 |
Correct |
2 ms |
8788 KB |
Output is correct |
11 |
Correct |
2 ms |
8784 KB |
Output is correct |
12 |
Correct |
2 ms |
8784 KB |
Output is correct |
13 |
Correct |
2 ms |
8788 KB |
Output is correct |
14 |
Correct |
2 ms |
8784 KB |
Output is correct |
15 |
Correct |
2 ms |
8784 KB |
Output is correct |
16 |
Correct |
2 ms |
8784 KB |
Output is correct |
17 |
Correct |
2 ms |
8952 KB |
Output is correct |
18 |
Correct |
2 ms |
8784 KB |
Output is correct |
19 |
Correct |
2 ms |
8784 KB |
Output is correct |
20 |
Correct |
2 ms |
8784 KB |
Output is correct |
21 |
Correct |
3 ms |
8784 KB |
Output is correct |
22 |
Correct |
4 ms |
6736 KB |
Output is correct |
23 |
Correct |
4 ms |
6736 KB |
Output is correct |
24 |
Correct |
4 ms |
6864 KB |
Output is correct |
25 |
Correct |
5 ms |
6736 KB |
Output is correct |
26 |
Correct |
4 ms |
6736 KB |
Output is correct |
27 |
Correct |
4 ms |
6908 KB |
Output is correct |
28 |
Correct |
4 ms |
6736 KB |
Output is correct |
29 |
Correct |
4 ms |
6752 KB |
Output is correct |
30 |
Correct |
3 ms |
6736 KB |
Output is correct |
31 |
Correct |
4 ms |
6736 KB |
Output is correct |
32 |
Correct |
4 ms |
6652 KB |
Output is correct |
33 |
Correct |
4 ms |
6736 KB |
Output is correct |
34 |
Correct |
4 ms |
6736 KB |
Output is correct |
35 |
Correct |
4 ms |
6848 KB |
Output is correct |
36 |
Correct |
4 ms |
6736 KB |
Output is correct |
37 |
Correct |
4 ms |
6868 KB |
Output is correct |
38 |
Correct |
4 ms |
6736 KB |
Output is correct |
39 |
Correct |
164 ms |
14620 KB |
Output is correct |
40 |
Correct |
160 ms |
16464 KB |
Output is correct |
41 |
Correct |
144 ms |
15176 KB |
Output is correct |
42 |
Correct |
129 ms |
16200 KB |
Output is correct |
43 |
Correct |
209 ms |
15432 KB |
Output is correct |
44 |
Correct |
88 ms |
16272 KB |
Output is correct |
45 |
Correct |
134 ms |
22344 KB |
Output is correct |
46 |
Correct |
72 ms |
20812 KB |
Output is correct |
47 |
Correct |
221 ms |
19732 KB |
Output is correct |
48 |
Correct |
549 ms |
40008 KB |
Output is correct |
49 |
Correct |
579 ms |
42232 KB |
Output is correct |
50 |
Correct |
202 ms |
19528 KB |
Output is correct |
51 |
Correct |
206 ms |
21824 KB |
Output is correct |
52 |
Correct |
233 ms |
22512 KB |
Output is correct |
53 |
Correct |
321 ms |
18504 KB |
Output is correct |
54 |
Correct |
556 ms |
29768 KB |
Output is correct |
55 |
Correct |
16 ms |
9552 KB |
Output is correct |
56 |
Correct |
10 ms |
9208 KB |
Output is correct |
57 |
Correct |
90 ms |
15072 KB |
Output is correct |
58 |
Correct |
32 ms |
15360 KB |
Output is correct |
59 |
Correct |
211 ms |
25672 KB |
Output is correct |
60 |
Correct |
551 ms |
39160 KB |
Output is correct |
61 |
Correct |
236 ms |
21064 KB |
Output is correct |
62 |
Correct |
180 ms |
18732 KB |
Output is correct |
63 |
Correct |
198 ms |
20540 KB |
Output is correct |
64 |
Correct |
630 ms |
26276 KB |
Output is correct |
65 |
Correct |
540 ms |
32036 KB |
Output is correct |
66 |
Correct |
558 ms |
42128 KB |
Output is correct |
67 |
Correct |
78 ms |
19640 KB |
Output is correct |
68 |
Correct |
333 ms |
31052 KB |
Output is correct |
69 |
Correct |
290 ms |
27644 KB |
Output is correct |
70 |
Correct |
284 ms |
30084 KB |
Output is correct |