#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 2e5 + 10;
vector<array<ll, 2>> ad[N];
ll mark[N], sz[N], ans = 1e18, k;
array<ll, 2> nodes[N];
int t, tin[N], tout[N], lv[N];
map <ll, ll> we;
ll size(ll node, ll par) {
sz[node] = 1;
for (auto [v, w] : ad[node]) {
if (v != par and !mark[v]) {
sz[node] += size(v, node);
}
}
return sz[node];
}
ll centroid(ll node, ll par, ll s) {
for (auto [v, w] : ad[node]) {
if (v != par and !mark[v]) {
if (sz[v] > s / 2) return centroid(v, node, s);
}
}
return node;
}
void dfs(ll node, ll par, ll wt, int l = 0) {
nodes[t] = {node, wt};
tin[node] = t++;
lv[node] = l;
for (auto [v, w] : ad[node]) {
if (v != par and !mark[v]) {
dfs(v, node, wt + w, l + 1);
}
}
tout[node] = t - 1;
}
void go(ll node) {
ll s = size(node, node);
ll c = centroid(node, node, s);
mark[c] = 1;
t = 0;
dfs(c, c, 0);
for (int i = 0; i < t; i++) {
auto [a, b] = nodes[i];
we[b] = 1e18;
}
we[0] = 0;
for (auto [v, w] : ad[c]) {
if (!mark[v]) {
for (ll i = tin[v]; i <= tout[v]; i++) {
auto [a, b] = nodes[i];
if (k - b >= 0 and we.find(k - b) != we.end())
ans = min(ans, we[k - b] + lv[a]);
}
for (ll i = tin[v]; i <= tout[v]; i++) {
auto [a, b] = nodes[i];
we[b] = min(we[b], 1ll * lv[a]);
}
}
}
for (int i = 0; i < t; i++) {
auto [a, b] = nodes[i];
we[b] = 1e18;
}
for (auto [v, w] : ad[c]) {
if (!mark[v]) {
go(v);
}
}
}
int best_path(int n, int d, int H[][2], int dis[]) {
k = d;
for (int i = 0; i + 1 < n; i++) {
ad[H[i][0]].push_back({H[i][1], dis[i]});
ad[H[i][1]].push_back({H[i][0], dis[i]});
}
go(0);
if (ans == 1e18) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5152 KB |
Output is correct |
2 |
Correct |
2 ms |
5172 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
4 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
2 ms |
5152 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
5152 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
5200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5152 KB |
Output is correct |
2 |
Correct |
2 ms |
5172 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
4 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
2 ms |
5152 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
5152 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
5200 KB |
Output is correct |
19 |
Correct |
2 ms |
5160 KB |
Output is correct |
20 |
Correct |
4 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
5 ms |
5468 KB |
Output is correct |
23 |
Correct |
5 ms |
5468 KB |
Output is correct |
24 |
Correct |
5 ms |
5468 KB |
Output is correct |
25 |
Correct |
4 ms |
5468 KB |
Output is correct |
26 |
Correct |
4 ms |
5424 KB |
Output is correct |
27 |
Correct |
3 ms |
5164 KB |
Output is correct |
28 |
Correct |
4 ms |
5428 KB |
Output is correct |
29 |
Correct |
4 ms |
5468 KB |
Output is correct |
30 |
Correct |
5 ms |
5464 KB |
Output is correct |
31 |
Correct |
6 ms |
5468 KB |
Output is correct |
32 |
Correct |
5 ms |
5588 KB |
Output is correct |
33 |
Correct |
5 ms |
5612 KB |
Output is correct |
34 |
Correct |
6 ms |
5680 KB |
Output is correct |
35 |
Correct |
5 ms |
5468 KB |
Output is correct |
36 |
Correct |
6 ms |
5468 KB |
Output is correct |
37 |
Correct |
4 ms |
5468 KB |
Output is correct |
38 |
Correct |
8 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5152 KB |
Output is correct |
2 |
Correct |
2 ms |
5172 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
4 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
2 ms |
5152 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
5152 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
5200 KB |
Output is correct |
19 |
Correct |
219 ms |
17100 KB |
Output is correct |
20 |
Correct |
200 ms |
17000 KB |
Output is correct |
21 |
Correct |
200 ms |
17236 KB |
Output is correct |
22 |
Correct |
194 ms |
16976 KB |
Output is correct |
23 |
Correct |
308 ms |
17492 KB |
Output is correct |
24 |
Correct |
126 ms |
16728 KB |
Output is correct |
25 |
Correct |
202 ms |
20304 KB |
Output is correct |
26 |
Correct |
106 ms |
22864 KB |
Output is correct |
27 |
Correct |
300 ms |
29776 KB |
Output is correct |
28 |
Correct |
2017 ms |
91272 KB |
Output is correct |
29 |
Correct |
1689 ms |
83396 KB |
Output is correct |
30 |
Correct |
307 ms |
29776 KB |
Output is correct |
31 |
Correct |
299 ms |
29872 KB |
Output is correct |
32 |
Correct |
350 ms |
29992 KB |
Output is correct |
33 |
Correct |
386 ms |
28496 KB |
Output is correct |
34 |
Execution timed out |
3045 ms |
169056 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5152 KB |
Output is correct |
2 |
Correct |
2 ms |
5172 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
4 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
5208 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
2 ms |
5152 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
5152 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
5200 KB |
Output is correct |
19 |
Correct |
2 ms |
5160 KB |
Output is correct |
20 |
Correct |
4 ms |
5208 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
5 ms |
5468 KB |
Output is correct |
23 |
Correct |
5 ms |
5468 KB |
Output is correct |
24 |
Correct |
5 ms |
5468 KB |
Output is correct |
25 |
Correct |
4 ms |
5468 KB |
Output is correct |
26 |
Correct |
4 ms |
5424 KB |
Output is correct |
27 |
Correct |
3 ms |
5164 KB |
Output is correct |
28 |
Correct |
4 ms |
5428 KB |
Output is correct |
29 |
Correct |
4 ms |
5468 KB |
Output is correct |
30 |
Correct |
5 ms |
5464 KB |
Output is correct |
31 |
Correct |
6 ms |
5468 KB |
Output is correct |
32 |
Correct |
5 ms |
5588 KB |
Output is correct |
33 |
Correct |
5 ms |
5612 KB |
Output is correct |
34 |
Correct |
6 ms |
5680 KB |
Output is correct |
35 |
Correct |
5 ms |
5468 KB |
Output is correct |
36 |
Correct |
6 ms |
5468 KB |
Output is correct |
37 |
Correct |
4 ms |
5468 KB |
Output is correct |
38 |
Correct |
8 ms |
5468 KB |
Output is correct |
39 |
Correct |
219 ms |
17100 KB |
Output is correct |
40 |
Correct |
200 ms |
17000 KB |
Output is correct |
41 |
Correct |
200 ms |
17236 KB |
Output is correct |
42 |
Correct |
194 ms |
16976 KB |
Output is correct |
43 |
Correct |
308 ms |
17492 KB |
Output is correct |
44 |
Correct |
126 ms |
16728 KB |
Output is correct |
45 |
Correct |
202 ms |
20304 KB |
Output is correct |
46 |
Correct |
106 ms |
22864 KB |
Output is correct |
47 |
Correct |
300 ms |
29776 KB |
Output is correct |
48 |
Correct |
2017 ms |
91272 KB |
Output is correct |
49 |
Correct |
1689 ms |
83396 KB |
Output is correct |
50 |
Correct |
307 ms |
29776 KB |
Output is correct |
51 |
Correct |
299 ms |
29872 KB |
Output is correct |
52 |
Correct |
350 ms |
29992 KB |
Output is correct |
53 |
Correct |
386 ms |
28496 KB |
Output is correct |
54 |
Execution timed out |
3045 ms |
169056 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |