#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
int N, lg;
vector<int> p, dep;
vector<int64_t> dis;
vector<vector<int>> par;
vector<vector<pair<int, int>>> adj;
void Init(int n, int A[], int B[], int D[]) {
N = n;
p.resize(N);
dis.resize(N);
dep.resize(N);
adj.resize(N);
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)];
}
constexpr int64_t inf = 1E18;
long long Query(int S, int X[], int T, int Y[]) {
vector<int64_t> best(N, inf);
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];
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
660 KB |
Output is correct |
2 |
Correct |
617 ms |
9264 KB |
Output is correct |
3 |
Correct |
1269 ms |
9408 KB |
Output is correct |
4 |
Correct |
1246 ms |
18736 KB |
Output is correct |
5 |
Correct |
1657 ms |
18844 KB |
Output is correct |
6 |
Correct |
309 ms |
18708 KB |
Output is correct |
7 |
Correct |
1266 ms |
18696 KB |
Output is correct |
8 |
Correct |
1308 ms |
18668 KB |
Output is correct |
9 |
Correct |
1634 ms |
18940 KB |
Output is correct |
10 |
Correct |
296 ms |
18668 KB |
Output is correct |
11 |
Correct |
1295 ms |
18764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Execution timed out |
8100 ms |
112772 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
660 KB |
Output is correct |
2 |
Correct |
617 ms |
9264 KB |
Output is correct |
3 |
Correct |
1269 ms |
9408 KB |
Output is correct |
4 |
Correct |
1246 ms |
18736 KB |
Output is correct |
5 |
Correct |
1657 ms |
18844 KB |
Output is correct |
6 |
Correct |
309 ms |
18708 KB |
Output is correct |
7 |
Correct |
1266 ms |
18696 KB |
Output is correct |
8 |
Correct |
1308 ms |
18668 KB |
Output is correct |
9 |
Correct |
1634 ms |
18940 KB |
Output is correct |
10 |
Correct |
296 ms |
18668 KB |
Output is correct |
11 |
Correct |
1295 ms |
18764 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Execution timed out |
8100 ms |
112772 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |