#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
const long long INFF = 1e18;
int dep[500000], par[500000], tin[500000], sz[500000], rem[500000], lgg[1000001], pw[20];
long long d[500000], ans[500000];
pair<int, int> sp[20][1000000];
vector<pair<int, int>> g[500000];
void Init(int n, int A[], int B[], int D[]) {
for (int i = 0; i < n - 1; i++) {
g[A[i]].push_back({B[i], D[i]});
g[B[i]].push_back({A[i], D[i]});
}
for (int i = 1; i <= 1000000; i++) {
lgg[i] = __lg(i);
}
for (int j = 0; j < 20; j++) {
pw[j] = 1 << j;
}
int tim = 0;
function<void(int, int)> dfs = [&](int x, int pr) {
sp[0][tim] = {dep[x], x};
tin[x] = tim++;
for (auto [z, c] : g[x]) {
if (z == pr) continue;
dep[z] = dep[x] + 1;
d[z] = d[x] + c;
dfs(z, x);
sp[0][tim++] = {dep[x], x};
}
};
dep[0] = 0;
d[0] = 0;
dfs(0, 0);
for (int i = 0; i < n; i++) ans[i] = INFF;
for (int j = 1; (1 << j) <= tim; j++) {
for (int i = 0; i + (1 << j) - 1 < tim; i++) {
sp[j][i] = min(sp[j - 1][i], sp[j - 1][i + (1 << (j - 1))]);
}
}
function<void(int, int)> compute_subtree = [&](int x, int p) {
sz[x] = 1;
for (auto [z, c] : g[x]) {
if (z == p || rem[z]) continue;
compute_subtree(z, x);
sz[x] += sz[z];
}
};
auto get_centroid = [&](int x) {
compute_subtree(x, -1);
int tree_size = sz[x];
bool found = 0;
while (!found) {
found = 1;
for (auto [z, c] : g[x]) {
if (rem[z] || sz[z] > sz[x]) continue;
if (sz[z] > tree_size / 2) {
x = z;
found = 0;
}
}
}
return x;
};
function<int(int)> build_centroid_tree = [&](int x) {
x = get_centroid(x);
rem[x] = 1;
for (auto [z, c] : g[x]) {
if (rem[z]) continue;
z = build_centroid_tree(z);
par[z] = x;
}
return x;
};
int rt = build_centroid_tree(0);
par[rt] = -1;
}
int lca(int x, int y) {
x = tin[x]; y = tin[y];
if (x > y) swap(x, y);
int j = lgg[y - x + 1];
return min(sp[j][x], sp[j][y - pw[j] + 1]).second;
}
long long dist(int x, int y) {
return d[x] + d[y] - 2 * d[lca(x, y)];
}
void update(int x) {
int z = x;
while (z != -1) {
ans[z] = min(ans[z], dist(z, x));
z = par[z];
}
}
long long query(int x) {
int z = x;
long long res = INFF;
while (z != -1) {
res = min(res, ans[z] + dist(z, x));
z = par[z];
}
return res;
}
void reset(int x) {
int z = x;
while (z != -1) {
ans[z] = INFF;
z = par[z];
}
}
long long Query(int x, int a[], int y, int b[]) {
for (int i = 0; i < x; i++) {
update(a[i]);
}
long long ans = INFF;
for (int i = 0; i < y; i++) {
ans = min(ans, query(b[i]));
}
for (int i = 0; i < x; i++) {
reset(a[i]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
68436 KB |
Output is correct |
2 |
Correct |
338 ms |
92240 KB |
Output is correct |
3 |
Correct |
455 ms |
92500 KB |
Output is correct |
4 |
Correct |
435 ms |
92332 KB |
Output is correct |
5 |
Correct |
438 ms |
92752 KB |
Output is correct |
6 |
Correct |
189 ms |
92280 KB |
Output is correct |
7 |
Correct |
435 ms |
92412 KB |
Output is correct |
8 |
Correct |
463 ms |
92164 KB |
Output is correct |
9 |
Correct |
429 ms |
92752 KB |
Output is correct |
10 |
Correct |
199 ms |
92244 KB |
Output is correct |
11 |
Correct |
431 ms |
92360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
68188 KB |
Output is correct |
2 |
Correct |
1764 ms |
242432 KB |
Output is correct |
3 |
Correct |
2292 ms |
247304 KB |
Output is correct |
4 |
Correct |
627 ms |
242872 KB |
Output is correct |
5 |
Correct |
3145 ms |
287400 KB |
Output is correct |
6 |
Correct |
2418 ms |
247780 KB |
Output is correct |
7 |
Correct |
1145 ms |
138840 KB |
Output is correct |
8 |
Correct |
288 ms |
138336 KB |
Output is correct |
9 |
Correct |
1077 ms |
143544 KB |
Output is correct |
10 |
Correct |
1185 ms |
139492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
68436 KB |
Output is correct |
2 |
Correct |
338 ms |
92240 KB |
Output is correct |
3 |
Correct |
455 ms |
92500 KB |
Output is correct |
4 |
Correct |
435 ms |
92332 KB |
Output is correct |
5 |
Correct |
438 ms |
92752 KB |
Output is correct |
6 |
Correct |
189 ms |
92280 KB |
Output is correct |
7 |
Correct |
435 ms |
92412 KB |
Output is correct |
8 |
Correct |
463 ms |
92164 KB |
Output is correct |
9 |
Correct |
429 ms |
92752 KB |
Output is correct |
10 |
Correct |
199 ms |
92244 KB |
Output is correct |
11 |
Correct |
431 ms |
92360 KB |
Output is correct |
12 |
Correct |
11 ms |
68188 KB |
Output is correct |
13 |
Correct |
1764 ms |
242432 KB |
Output is correct |
14 |
Correct |
2292 ms |
247304 KB |
Output is correct |
15 |
Correct |
627 ms |
242872 KB |
Output is correct |
16 |
Correct |
3145 ms |
287400 KB |
Output is correct |
17 |
Correct |
2418 ms |
247780 KB |
Output is correct |
18 |
Correct |
1145 ms |
138840 KB |
Output is correct |
19 |
Correct |
288 ms |
138336 KB |
Output is correct |
20 |
Correct |
1077 ms |
143544 KB |
Output is correct |
21 |
Correct |
1185 ms |
139492 KB |
Output is correct |
22 |
Correct |
2590 ms |
247148 KB |
Output is correct |
23 |
Correct |
2473 ms |
248976 KB |
Output is correct |
24 |
Correct |
3438 ms |
251624 KB |
Output is correct |
25 |
Correct |
3302 ms |
254496 KB |
Output is correct |
26 |
Correct |
3542 ms |
252152 KB |
Output is correct |
27 |
Correct |
3934 ms |
283036 KB |
Output is correct |
28 |
Correct |
819 ms |
249660 KB |
Output is correct |
29 |
Correct |
3414 ms |
251520 KB |
Output is correct |
30 |
Correct |
3520 ms |
250828 KB |
Output is correct |
31 |
Correct |
3181 ms |
251732 KB |
Output is correct |
32 |
Correct |
1014 ms |
144468 KB |
Output is correct |
33 |
Correct |
312 ms |
138228 KB |
Output is correct |
34 |
Correct |
800 ms |
137092 KB |
Output is correct |
35 |
Correct |
879 ms |
137092 KB |
Output is correct |
36 |
Correct |
1033 ms |
137796 KB |
Output is correct |
37 |
Correct |
1156 ms |
137972 KB |
Output is correct |