#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ari2 = array<int, 2>;
using arl2 = array<ll, 2>;
#define vt vector
#define size(x) (int((x).size()))
#define all(x) begin(x), end(x)
#define REP(a, b, c, d) for (int a = (b); (d) > 0 ? a <= (c) : a >= (c); a += (d))
#define FOR(a, b, c) REP(a, b, c, 1)
#define ROF(a, b, c) REP(a, b, c, -1)
#define chmax(a, b) a = a > (b) ? a : (b)
#define chmin(a, b) a = a < (b) ? a : (b)
static constexpr int mxN = 500000;
static vt<ari2> adj[mxN];
static vt<arl2> cdal[mxN];
static vt<int> cd[mxN];
static ll best[mxN];
void Init(int N, int A[], int B[], int D[]) {
FOR(i, 0, N-2) {
adj[A[i]].push_back({B[i], D[i]});
adj[B[i]].push_back({A[i], D[i]});
}
int szs[N];
bool dead[N] = {};
function<int(int, int)> gs = [&](int i, int p) {
szs[i] = 1;
for (auto [j, _] : adj[i])
if (!dead[j] && j != p)
szs[i] += gs(j, i);
return szs[i];
};
auto fc = [&](int i, int n) {
bool flag = true;
int p = i;
while (flag) {
flag = false;
for (auto [j, _] : adj[i])
if (!dead[j] && j != p && szs[j] > n >> 1) {
p = i;
i = j;
flag = true;
break;
}
}
return i;
};
int root;
function<void(int, int)> init = [&](int i, int p) {
int c = fc(i, gs(i, i));
dead[c] = true;
if (~p)
cd[p].push_back(c);
else
root = c;
for (auto [j, _] : adj[c])
if (!dead[j])
init(j, c);
};
init(0, -1);
const int L = 32 - __builtin_clz(N);
vt<vt<int>> lift(N, vt<int>(L));
int tin[N], tout[N], timer = 0;
ll depth[N] = {};
function<void(int, int)> pc = [&](int i, int p) {
lift[i][0] = p;
tin[i] = timer++;
for (auto [j, v] : adj[i]) {
if (j == p)
continue;
depth[j] = depth[i] + v;
pc(j, i);
}
tout[i] = timer - 1;
};
pc(0, 0);
FOR(j, 1, L-1)
FOR(i, 0, N-1)
lift[i][j] = lift[lift[i][j-1]][j-1];
auto ia = [&](int a, int b) {
return tin[a] <= tin[b] && tin[b] <= tout[a];
};
auto LCA = [&](int a, int b) {
if (ia(a, b))
return a;
if (ia(b, a))
return b;
ROF(i, L-1, 0)
a = ia(lift[a][i], b) ? a : lift[a][i];
return lift[a][0];
};
auto dist = [&](int a, int b) {
return depth[a] + depth[b] - 2 * depth[LCA(a, b)];
};
function<void(int)> pc_cdal = [&](int i) {
cdal[i].push_back({i, 0});
for (int j : cd[i]) {
for (auto [a, _] : cdal[i])
cdal[j].push_back({a, dist(a, j)});
pc_cdal(j);
}
};
pc_cdal(root);
memset(best, 0x3f, sizeof(best));
}
ll Query(int S, int X[], int T, int Y[]) {
FOR(i, 0, S-1)
for (auto [a, b] : cdal[X[i]])
chmin(best[a], b);
ll ret = LLONG_MAX;
FOR(i, 0, T-1)
for (auto [a, b] : cdal[Y[i]])
chmin(ret, best[a] + b);
FOR(i, 0, S-1)
for (auto [a, _] : cdal[X[i]])
best[a] = 0x3f3f3f3f3f3f3f3f;
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
55912 KB |
Output is correct |
2 |
Correct |
194 ms |
71076 KB |
Output is correct |
3 |
Correct |
211 ms |
71636 KB |
Output is correct |
4 |
Correct |
210 ms |
71516 KB |
Output is correct |
5 |
Correct |
216 ms |
72012 KB |
Output is correct |
6 |
Correct |
151 ms |
70508 KB |
Output is correct |
7 |
Correct |
206 ms |
71452 KB |
Output is correct |
8 |
Correct |
220 ms |
71580 KB |
Output is correct |
9 |
Correct |
222 ms |
72392 KB |
Output is correct |
10 |
Correct |
151 ms |
70516 KB |
Output is correct |
11 |
Correct |
203 ms |
71532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
55900 KB |
Output is correct |
2 |
Correct |
1633 ms |
295516 KB |
Output is correct |
3 |
Correct |
2201 ms |
365356 KB |
Output is correct |
4 |
Correct |
785 ms |
189876 KB |
Output is correct |
5 |
Correct |
2621 ms |
451924 KB |
Output is correct |
6 |
Correct |
2276 ms |
364768 KB |
Output is correct |
7 |
Correct |
692 ms |
117380 KB |
Output is correct |
8 |
Correct |
288 ms |
93848 KB |
Output is correct |
9 |
Correct |
767 ms |
130556 KB |
Output is correct |
10 |
Correct |
716 ms |
117932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
55912 KB |
Output is correct |
2 |
Correct |
194 ms |
71076 KB |
Output is correct |
3 |
Correct |
211 ms |
71636 KB |
Output is correct |
4 |
Correct |
210 ms |
71516 KB |
Output is correct |
5 |
Correct |
216 ms |
72012 KB |
Output is correct |
6 |
Correct |
151 ms |
70508 KB |
Output is correct |
7 |
Correct |
206 ms |
71452 KB |
Output is correct |
8 |
Correct |
220 ms |
71580 KB |
Output is correct |
9 |
Correct |
222 ms |
72392 KB |
Output is correct |
10 |
Correct |
151 ms |
70516 KB |
Output is correct |
11 |
Correct |
203 ms |
71532 KB |
Output is correct |
12 |
Correct |
13 ms |
55900 KB |
Output is correct |
13 |
Correct |
1633 ms |
295516 KB |
Output is correct |
14 |
Correct |
2201 ms |
365356 KB |
Output is correct |
15 |
Correct |
785 ms |
189876 KB |
Output is correct |
16 |
Correct |
2621 ms |
451924 KB |
Output is correct |
17 |
Correct |
2276 ms |
364768 KB |
Output is correct |
18 |
Correct |
692 ms |
117380 KB |
Output is correct |
19 |
Correct |
288 ms |
93848 KB |
Output is correct |
20 |
Correct |
767 ms |
130556 KB |
Output is correct |
21 |
Correct |
716 ms |
117932 KB |
Output is correct |
22 |
Correct |
1920 ms |
299328 KB |
Output is correct |
23 |
Correct |
1891 ms |
301944 KB |
Output is correct |
24 |
Correct |
2645 ms |
369456 KB |
Output is correct |
25 |
Correct |
2618 ms |
371176 KB |
Output is correct |
26 |
Correct |
2466 ms |
371208 KB |
Output is correct |
27 |
Correct |
2985 ms |
448616 KB |
Output is correct |
28 |
Correct |
866 ms |
196408 KB |
Output is correct |
29 |
Correct |
2466 ms |
369996 KB |
Output is correct |
30 |
Correct |
2595 ms |
369292 KB |
Output is correct |
31 |
Correct |
2441 ms |
370392 KB |
Output is correct |
32 |
Correct |
886 ms |
131268 KB |
Output is correct |
33 |
Correct |
271 ms |
93828 KB |
Output is correct |
34 |
Correct |
499 ms |
110840 KB |
Output is correct |
35 |
Correct |
529 ms |
111588 KB |
Output is correct |
36 |
Correct |
664 ms |
116012 KB |
Output is correct |
37 |
Correct |
647 ms |
116240 KB |
Output is correct |