#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;
}
// void solve() {
// int n, k; cin >> n >> k;
// int arr[n - 1][2];
// for (int i = 0; i + 1 < n; i++) {
// cin >> arr[i][0] >> arr[i][1];
// }
// int dis[n - 1];
// for (int i = 0; i + 1 < n; i++) {
// cin >> dis[i];
// }
// cout << best_path(n, k, arr, dis) << "\n";
// }
// int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// ll tt = 1;
// // cin >> tt;
// while (tt--) {
// solve();
// }
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4996 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 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 |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5152 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4952 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4996 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 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 |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5152 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4952 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5468 KB |
Output is correct |
23 |
Correct |
3 ms |
5592 KB |
Output is correct |
24 |
Correct |
3 ms |
5468 KB |
Output is correct |
25 |
Correct |
3 ms |
5468 KB |
Output is correct |
26 |
Correct |
3 ms |
5468 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5468 KB |
Output is correct |
29 |
Correct |
3 ms |
5492 KB |
Output is correct |
30 |
Correct |
5 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 |
6 ms |
5416 KB |
Output is correct |
35 |
Correct |
4 ms |
5468 KB |
Output is correct |
36 |
Correct |
3 ms |
5468 KB |
Output is correct |
37 |
Correct |
4 ms |
5468 KB |
Output is correct |
38 |
Correct |
4 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4996 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 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 |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5152 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4952 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
107 ms |
16980 KB |
Output is correct |
20 |
Correct |
114 ms |
16980 KB |
Output is correct |
21 |
Correct |
106 ms |
16892 KB |
Output is correct |
22 |
Correct |
92 ms |
17148 KB |
Output is correct |
23 |
Correct |
105 ms |
17488 KB |
Output is correct |
24 |
Correct |
59 ms |
16728 KB |
Output is correct |
25 |
Correct |
130 ms |
19920 KB |
Output is correct |
26 |
Correct |
84 ms |
22608 KB |
Output is correct |
27 |
Correct |
141 ms |
28576 KB |
Output is correct |
28 |
Correct |
779 ms |
78944 KB |
Output is correct |
29 |
Correct |
727 ms |
65904 KB |
Output is correct |
30 |
Correct |
148 ms |
28500 KB |
Output is correct |
31 |
Correct |
157 ms |
28584 KB |
Output is correct |
32 |
Correct |
203 ms |
28636 KB |
Output is correct |
33 |
Correct |
258 ms |
27476 KB |
Output is correct |
34 |
Correct |
1552 ms |
128732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
4 ms |
4996 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 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 |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5152 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
4952 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
3 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5468 KB |
Output is correct |
23 |
Correct |
3 ms |
5592 KB |
Output is correct |
24 |
Correct |
3 ms |
5468 KB |
Output is correct |
25 |
Correct |
3 ms |
5468 KB |
Output is correct |
26 |
Correct |
3 ms |
5468 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5468 KB |
Output is correct |
29 |
Correct |
3 ms |
5492 KB |
Output is correct |
30 |
Correct |
5 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 |
6 ms |
5416 KB |
Output is correct |
35 |
Correct |
4 ms |
5468 KB |
Output is correct |
36 |
Correct |
3 ms |
5468 KB |
Output is correct |
37 |
Correct |
4 ms |
5468 KB |
Output is correct |
38 |
Correct |
4 ms |
5468 KB |
Output is correct |
39 |
Correct |
107 ms |
16980 KB |
Output is correct |
40 |
Correct |
114 ms |
16980 KB |
Output is correct |
41 |
Correct |
106 ms |
16892 KB |
Output is correct |
42 |
Correct |
92 ms |
17148 KB |
Output is correct |
43 |
Correct |
105 ms |
17488 KB |
Output is correct |
44 |
Correct |
59 ms |
16728 KB |
Output is correct |
45 |
Correct |
130 ms |
19920 KB |
Output is correct |
46 |
Correct |
84 ms |
22608 KB |
Output is correct |
47 |
Correct |
141 ms |
28576 KB |
Output is correct |
48 |
Correct |
779 ms |
78944 KB |
Output is correct |
49 |
Correct |
727 ms |
65904 KB |
Output is correct |
50 |
Correct |
148 ms |
28500 KB |
Output is correct |
51 |
Correct |
157 ms |
28584 KB |
Output is correct |
52 |
Correct |
203 ms |
28636 KB |
Output is correct |
53 |
Correct |
258 ms |
27476 KB |
Output is correct |
54 |
Correct |
1552 ms |
128732 KB |
Output is correct |
55 |
Correct |
14 ms |
6488 KB |
Output is correct |
56 |
Correct |
9 ms |
6364 KB |
Output is correct |
57 |
Correct |
71 ms |
17140 KB |
Output is correct |
58 |
Correct |
30 ms |
16064 KB |
Output is correct |
59 |
Correct |
102 ms |
26480 KB |
Output is correct |
60 |
Correct |
462 ms |
52424 KB |
Output is correct |
61 |
Correct |
175 ms |
29292 KB |
Output is correct |
62 |
Correct |
178 ms |
28392 KB |
Output is correct |
63 |
Correct |
181 ms |
28432 KB |
Output is correct |
64 |
Correct |
424 ms |
37600 KB |
Output is correct |
65 |
Correct |
990 ms |
108200 KB |
Output is correct |
66 |
Correct |
812 ms |
76012 KB |
Output is correct |
67 |
Correct |
85 ms |
27580 KB |
Output is correct |
68 |
Correct |
325 ms |
40668 KB |
Output is correct |
69 |
Correct |
342 ms |
40712 KB |
Output is correct |
70 |
Correct |
300 ms |
39284 KB |
Output is correct |