#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
int N, lg;
vector<int> p, dep;
vector<int64_t> dis, best;
vector<vector<int>> par;
vector<vector<pair<int, int>>> adj;
constexpr int64_t inf = 1E18;
void Init(int n, int A[], int B[], int D[]) {
N = n;
p.resize(N);
dis.resize(N);
dep.resize(N);
adj.resize(N);
best.resize(N, inf);
lg = __lg(N);
par = vector(N, vector<int>(lg + 1, -1));
for (int i = 0; i < N - 1; i++) {
adj[A[i]].emplace_back(B[i], D[i]);
adj[B[i]].emplace_back(A[i], D[i]);
}
auto dfs = [&](auto dfs, int x) -> void {
for (int i = 0; i < lg && par[x][i] != -1; i++) {
par[x][i + 1] = par[par[x][i]][i];
}
for (auto [y, z] : adj[x]) {
if (y == par[x][0]) {
continue;
}
dep[y] = dep[x] + 1;
dis[y] = dis[x] + z;
par[y][0] = x;
dfs(dfs, y);
}
};
dfs(dfs, 0);
vector<bool> vis(N);
vector<int> sz(N);
auto init = [&](auto init, int x, int p) -> void {
sz[x] = 1;
for (auto [y, _] : adj[x]) {
if (y == p || vis[y]) {
continue;
}
init(init, y, x);
sz[x] += sz[y];
}
};
auto find = [&](auto find, int x, int p, int s) -> int {
for (auto [y, _] : adj[x]) {
if (y == p || vis[y] || sz[y] * 2 <= s) {
continue;
}
return find(find, y, x, s);
}
return x;
};
auto cd = [&](auto cd, int x) -> int {
init(init, x, -1);
x = find(find, x, -1, sz[x]);
vis[x] = 1;
for (auto [y, _] : adj[x]) {
if (vis[y]) {
continue;
}
p[cd(cd, y)] = x;
}
return x;
};
p[cd(cd, 0)] = -1;
}
void lift(int &x, int d) {
if (d < 0) {
return;
}
for (int i = 0; i <= lg; i++) {
if (d >> i & 1) {
x = par[x][i];
}
}
}
int lca(int x, int y) {
lift(x, dep[x] - dep[y]);
lift(y, dep[y] - dep[x]);
if (x == y) {
return x;
}
for (int i = lg; i >= 0; i--) {
if (par[x][i] != par[y][i]) {
x = par[x][i];
y = par[y][i];
}
}
return par[x][0];
}
int64_t dist(int x, int y) {
return dis[x] + dis[y] - 2 * dis[lca(x, y)];
}
long long Query(int S, int X[], int T, int Y[]) {
for (int i = 0; i < S; i++) {
int x = X[i];
while (x != -1) {
best[x] = min(best[x], dist(x, X[i]));
x = p[x];
}
}
auto ans = inf;
for (int i = 0; i < T; i++) {
int x = Y[i];
while (x != -1) {
ans = min(ans, best[x] + dist(x, Y[i]));
x = p[x];
}
}
for (int i = 0; i < S; i++) {
int x = X[i];
while (x != -1) {
best[x] = inf;
x = p[x];
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
596 KB |
Output is correct |
2 |
Correct |
654 ms |
9160 KB |
Output is correct |
3 |
Correct |
1352 ms |
9304 KB |
Output is correct |
4 |
Correct |
1303 ms |
9300 KB |
Output is correct |
5 |
Correct |
1742 ms |
9584 KB |
Output is correct |
6 |
Correct |
296 ms |
9296 KB |
Output is correct |
7 |
Correct |
1333 ms |
9316 KB |
Output is correct |
8 |
Correct |
1365 ms |
9412 KB |
Output is correct |
9 |
Correct |
1709 ms |
9344 KB |
Output is correct |
10 |
Correct |
280 ms |
9268 KB |
Output is correct |
11 |
Correct |
1366 ms |
9196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
3594 ms |
114232 KB |
Output is correct |
3 |
Execution timed out |
8045 ms |
133696 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
596 KB |
Output is correct |
2 |
Correct |
654 ms |
9160 KB |
Output is correct |
3 |
Correct |
1352 ms |
9304 KB |
Output is correct |
4 |
Correct |
1303 ms |
9300 KB |
Output is correct |
5 |
Correct |
1742 ms |
9584 KB |
Output is correct |
6 |
Correct |
296 ms |
9296 KB |
Output is correct |
7 |
Correct |
1333 ms |
9316 KB |
Output is correct |
8 |
Correct |
1365 ms |
9412 KB |
Output is correct |
9 |
Correct |
1709 ms |
9344 KB |
Output is correct |
10 |
Correct |
280 ms |
9268 KB |
Output is correct |
11 |
Correct |
1366 ms |
9196 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
3594 ms |
114232 KB |
Output is correct |
14 |
Execution timed out |
8045 ms |
133696 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |