#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
vector<long long> d, mn;
vector<int> dep, out, sz, par, euler;
vector<bool> centroid;
vector<vector<pair<int, int>>> g;
void dfs(int u, int p) {
out[u] = euler.size();
euler.push_back(u);
for (auto [v, w] : g[u])
if (v ^ p) {
d[v] = d[u] + w;
dep[v] = dep[u] + 1;
dfs(v, u);
out[u] = euler.size();
euler.push_back(u);
}
}
struct RMQ {
vector<vector<int>> sparse;
vector<int> lg;
int comp(const int &a, const int &b) {
return dep[a] < dep[b] ? a : b;
}
void init(const vector<int> &v) {
int n = v.size();
lg.resize(n + 1);
for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
sparse = vector<vector<int>> (lg[n] + 1, vector<int> (n));
for (int i = 0; i < n; i++) sparse[0][i] = v[i];
for (int i = 1; i <= lg[n]; i++)
for (int j = 0; j + (1 << (i - 1)) < n; j++)
sparse[i][j] = comp(sparse[i - 1][j], sparse[i - 1][j + (1 << (i - 1))]);
}
int query(int l, int r) {
return comp(sparse[lg[r - l + 1]][l], sparse[lg[r - l + 1]][r - (1 << lg[r - l + 1]) + 1]);
}
} rmq;
int lca(int u, int v) {
if (out[u] > out[v]) swap(u, v);
return rmq.query(out[u], out[v]);
}
long long dist(int u, int v) {
return d[u] + d[v] - (d[lca(u, v)] << 1);
}
int findcentroid(int u, int p, int root) {
for (auto [v, w] : g[u])
if (v ^ p && !centroid[v] && sz[v] << 1 > sz[root])
return findcentroid(v, u, root);
return u;
}
void calcsz(int u, int p) {
sz[u] = 1;
for (auto [v, w] : g[u])
if (v ^ p && !centroid[v])
calcsz(v, u), sz[u] += sz[v];
}
int centroid_decompose(int u) {
calcsz(u, u);
int x = findcentroid(u, u, u);
centroid[x] = 1;
for (auto [v, w] : g[x])
if (!centroid[v])
par[centroid_decompose(v)] = x;
return x;
}
void update(int u) {
int x = u;
while (u != -1) {
mn[u] = min(mn[u], dist(x, u));
u = par[u];
}
}
void reset(int u) {
while (u != -1) {
mn[u] = INF;
u = par[u];
}
}
long long qry(int u) {
int x = u;
long long ret = INF;
while (u != -1) {
ret = min(ret, mn[u] + dist(x, u));
u = par[u];
}
return ret;
}
void Init(int n, int a[], int b[], int c[]) {
g.resize(n), d.resize(n), dep.resize(n), sz.resize(n), par.resize(n, -1), mn.resize(n, INF), centroid.resize(n), out.resize(n);
for (int i = 0; i < n - 1; i++) {
g[a[i]].push_back({b[i], c[i]});
g[b[i]].push_back({a[i], c[i]});
}
dfs(0, 0);
rmq.init(euler);
centroid_decompose(0);
}
long long Query(int s, int x[], int t, int y[]) {
for (int i = 0; i < s; i++) update(x[i]);
long long ans = INF;
for (int i = 0; i < t; i++) ans = min(ans, qry(y[i]));
for (int i = 0; i < s; i++)
if (x[i] != INF) reset(x[i]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
596 KB |
Output is correct |
2 |
Correct |
358 ms |
9472 KB |
Output is correct |
3 |
Correct |
450 ms |
9484 KB |
Output is correct |
4 |
Correct |
393 ms |
9444 KB |
Output is correct |
5 |
Correct |
496 ms |
9680 KB |
Output is correct |
6 |
Correct |
194 ms |
9472 KB |
Output is correct |
7 |
Correct |
409 ms |
9504 KB |
Output is correct |
8 |
Correct |
419 ms |
9596 KB |
Output is correct |
9 |
Correct |
467 ms |
9804 KB |
Output is correct |
10 |
Correct |
205 ms |
9476 KB |
Output is correct |
11 |
Correct |
401 ms |
9500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
1985 ms |
147664 KB |
Output is correct |
3 |
Correct |
2750 ms |
149992 KB |
Output is correct |
4 |
Correct |
728 ms |
148536 KB |
Output is correct |
5 |
Correct |
3648 ms |
171424 KB |
Output is correct |
6 |
Correct |
3054 ms |
150300 KB |
Output is correct |
7 |
Correct |
1407 ms |
36316 KB |
Output is correct |
8 |
Correct |
402 ms |
37004 KB |
Output is correct |
9 |
Correct |
1652 ms |
39968 KB |
Output is correct |
10 |
Correct |
1477 ms |
37488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
596 KB |
Output is correct |
2 |
Correct |
358 ms |
9472 KB |
Output is correct |
3 |
Correct |
450 ms |
9484 KB |
Output is correct |
4 |
Correct |
393 ms |
9444 KB |
Output is correct |
5 |
Correct |
496 ms |
9680 KB |
Output is correct |
6 |
Correct |
194 ms |
9472 KB |
Output is correct |
7 |
Correct |
409 ms |
9504 KB |
Output is correct |
8 |
Correct |
419 ms |
9596 KB |
Output is correct |
9 |
Correct |
467 ms |
9804 KB |
Output is correct |
10 |
Correct |
205 ms |
9476 KB |
Output is correct |
11 |
Correct |
401 ms |
9500 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1985 ms |
147664 KB |
Output is correct |
14 |
Correct |
2750 ms |
149992 KB |
Output is correct |
15 |
Correct |
728 ms |
148536 KB |
Output is correct |
16 |
Correct |
3648 ms |
171424 KB |
Output is correct |
17 |
Correct |
3054 ms |
150300 KB |
Output is correct |
18 |
Correct |
1407 ms |
36316 KB |
Output is correct |
19 |
Correct |
402 ms |
37004 KB |
Output is correct |
20 |
Correct |
1652 ms |
39968 KB |
Output is correct |
21 |
Correct |
1477 ms |
37488 KB |
Output is correct |
22 |
Correct |
3012 ms |
150364 KB |
Output is correct |
23 |
Correct |
3020 ms |
151132 KB |
Output is correct |
24 |
Correct |
4539 ms |
152476 KB |
Output is correct |
25 |
Correct |
4566 ms |
154012 KB |
Output is correct |
26 |
Correct |
4312 ms |
152864 KB |
Output is correct |
27 |
Correct |
5299 ms |
169104 KB |
Output is correct |
28 |
Correct |
1069 ms |
151996 KB |
Output is correct |
29 |
Correct |
4269 ms |
152720 KB |
Output is correct |
30 |
Correct |
4557 ms |
152036 KB |
Output is correct |
31 |
Correct |
4169 ms |
152764 KB |
Output is correct |
32 |
Correct |
1536 ms |
40808 KB |
Output is correct |
33 |
Correct |
396 ms |
37424 KB |
Output is correct |
34 |
Correct |
1046 ms |
35192 KB |
Output is correct |
35 |
Correct |
1158 ms |
35184 KB |
Output is correct |
36 |
Correct |
1461 ms |
35596 KB |
Output is correct |
37 |
Correct |
1604 ms |
35616 KB |
Output is correct |