#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];
unordered_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];
auto it = we.find(k - b);
if (k - b >= 0 and it != we.end())
ans = min(ans, (*it).second + 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 |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5156 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 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 |
5156 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5156 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 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 |
5156 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
5156 KB |
Output is correct |
20 |
Correct |
2 ms |
4952 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
4 ms |
5468 KB |
Output is correct |
23 |
Correct |
6 ms |
5488 KB |
Output is correct |
24 |
Correct |
4 ms |
5288 KB |
Output is correct |
25 |
Correct |
3 ms |
5468 KB |
Output is correct |
26 |
Correct |
3 ms |
5468 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5468 KB |
Output is correct |
29 |
Correct |
4 ms |
5464 KB |
Output is correct |
30 |
Correct |
3 ms |
5468 KB |
Output is correct |
31 |
Correct |
3 ms |
5468 KB |
Output is correct |
32 |
Correct |
3 ms |
5468 KB |
Output is correct |
33 |
Correct |
4 ms |
5468 KB |
Output is correct |
34 |
Correct |
4 ms |
5468 KB |
Output is correct |
35 |
Correct |
4 ms |
5420 KB |
Output is correct |
36 |
Correct |
3 ms |
5468 KB |
Output is correct |
37 |
Correct |
4 ms |
5480 KB |
Output is correct |
38 |
Correct |
3 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5156 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 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 |
5156 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
117 ms |
16980 KB |
Output is correct |
20 |
Correct |
108 ms |
16980 KB |
Output is correct |
21 |
Correct |
106 ms |
16976 KB |
Output is correct |
22 |
Correct |
92 ms |
17176 KB |
Output is correct |
23 |
Correct |
103 ms |
17496 KB |
Output is correct |
24 |
Correct |
61 ms |
16724 KB |
Output is correct |
25 |
Correct |
132 ms |
20052 KB |
Output is correct |
26 |
Correct |
93 ms |
22804 KB |
Output is correct |
27 |
Correct |
147 ms |
29520 KB |
Output is correct |
28 |
Correct |
763 ms |
80056 KB |
Output is correct |
29 |
Correct |
721 ms |
67260 KB |
Output is correct |
30 |
Correct |
142 ms |
29520 KB |
Output is correct |
31 |
Correct |
150 ms |
29656 KB |
Output is correct |
32 |
Correct |
214 ms |
29780 KB |
Output is correct |
33 |
Correct |
253 ms |
28448 KB |
Output is correct |
34 |
Correct |
1541 ms |
130480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5156 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 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 |
5156 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
5156 KB |
Output is correct |
20 |
Correct |
2 ms |
4952 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
4 ms |
5468 KB |
Output is correct |
23 |
Correct |
6 ms |
5488 KB |
Output is correct |
24 |
Correct |
4 ms |
5288 KB |
Output is correct |
25 |
Correct |
3 ms |
5468 KB |
Output is correct |
26 |
Correct |
3 ms |
5468 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5468 KB |
Output is correct |
29 |
Correct |
4 ms |
5464 KB |
Output is correct |
30 |
Correct |
3 ms |
5468 KB |
Output is correct |
31 |
Correct |
3 ms |
5468 KB |
Output is correct |
32 |
Correct |
3 ms |
5468 KB |
Output is correct |
33 |
Correct |
4 ms |
5468 KB |
Output is correct |
34 |
Correct |
4 ms |
5468 KB |
Output is correct |
35 |
Correct |
4 ms |
5420 KB |
Output is correct |
36 |
Correct |
3 ms |
5468 KB |
Output is correct |
37 |
Correct |
4 ms |
5480 KB |
Output is correct |
38 |
Correct |
3 ms |
5468 KB |
Output is correct |
39 |
Correct |
117 ms |
16980 KB |
Output is correct |
40 |
Correct |
108 ms |
16980 KB |
Output is correct |
41 |
Correct |
106 ms |
16976 KB |
Output is correct |
42 |
Correct |
92 ms |
17176 KB |
Output is correct |
43 |
Correct |
103 ms |
17496 KB |
Output is correct |
44 |
Correct |
61 ms |
16724 KB |
Output is correct |
45 |
Correct |
132 ms |
20052 KB |
Output is correct |
46 |
Correct |
93 ms |
22804 KB |
Output is correct |
47 |
Correct |
147 ms |
29520 KB |
Output is correct |
48 |
Correct |
763 ms |
80056 KB |
Output is correct |
49 |
Correct |
721 ms |
67260 KB |
Output is correct |
50 |
Correct |
142 ms |
29520 KB |
Output is correct |
51 |
Correct |
150 ms |
29656 KB |
Output is correct |
52 |
Correct |
214 ms |
29780 KB |
Output is correct |
53 |
Correct |
253 ms |
28448 KB |
Output is correct |
54 |
Correct |
1541 ms |
130480 KB |
Output is correct |
55 |
Correct |
11 ms |
6492 KB |
Output is correct |
56 |
Correct |
10 ms |
6232 KB |
Output is correct |
57 |
Correct |
74 ms |
17236 KB |
Output is correct |
58 |
Correct |
30 ms |
16324 KB |
Output is correct |
59 |
Correct |
104 ms |
27032 KB |
Output is correct |
60 |
Correct |
522 ms |
53388 KB |
Output is correct |
61 |
Correct |
179 ms |
30608 KB |
Output is correct |
62 |
Correct |
140 ms |
29776 KB |
Output is correct |
63 |
Correct |
181 ms |
29780 KB |
Output is correct |
64 |
Correct |
418 ms |
39316 KB |
Output is correct |
65 |
Correct |
993 ms |
110068 KB |
Output is correct |
66 |
Correct |
869 ms |
77440 KB |
Output is correct |
67 |
Correct |
93 ms |
28864 KB |
Output is correct |
68 |
Correct |
326 ms |
42364 KB |
Output is correct |
69 |
Correct |
317 ms |
42364 KB |
Output is correct |
70 |
Correct |
297 ms |
40628 KB |
Output is correct |