#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
const int nax = 5e5 + 3;
const int lg = 20;
vector<pair<int, int>>g[nax];
int CT_par[nax];
int st[nax], en[nax], tt = 0;
int up[nax][lg];
long long d[nax];
int sub[nax];
void dfs(int v, int p) {
st[v] = tt++;
up[v][0] = p;
for(int j=1; j<lg; ++j) {
up[v][j] = up[up[v][j-1]][j-1];
}
for(auto [u, w] : g[v]) if(u!=p) {
d[u] = d[v] + w;
dfs(u, v);
}
en[v] = tt-1;
}
bool ban[nax];
int cdfs(int v, int p = -1) {
sub[v] = 1;
for(auto [u, w] : g[v]) if(u!=p && !ban[u]) {
sub[v] += cdfs(u, v);
}
return sub[v];
}
int get_centroid(int v, int sz, int p = -1) {
for(auto [u, w] : g[v]) if(sub[u]*2>sz && u!=p && !ban[u]) {
return get_centroid(u, sz, v);
}
return v;
}
void centroid(int v, int prevCen = -1) {
int C = get_centroid(v, cdfs(v));
CT_par[C] = prevCen;
ban[C] = 1;
for(auto [u, w] : g[C]) if(!ban[u]) {
centroid(u, C);
}
}
bool is_anc(int u, int v) {
return st[u]<=st[v] && en[u]>=en[v];
}
int getLCA(int u, int v) {
for(int j=lg-1; j>=0; --j) if(!is_anc(up[u][j], v)) {
u = up[u][j];
}
if(!is_anc(u, v)) u = up[u][0];
return u;
}
long long get_dist(int u, int v) {
int lca = getLCA(u, v);
return d[u] + d[v] - 2 * d[lca];
}
long long ans[nax];
long long mem[nax][lg];
void Init(int N, int A[], int B[], int D[]) {
for(int i=0; i<N; ++i) {
g[A[i]].emplace_back(B[i], D[i]);
g[B[i]].emplace_back(A[i], D[i]);
}
dfs(0, 0);
centroid(0);
for(int i=0; i<N; ++i) {
ans[i] = 1e18;
}
memset(mem, -1, sizeof mem);
}
long long Query(int S, int X[], int T, int Y[]) {
for(int i=0; i<S; ++i) {
int v = X[i];
int u = v;
int no = 0;
while(u!=-1) {
long long dis;
if(mem[v][no]!=-1) dis = mem[v][no];
else dis = mem[v][no] = get_dist(u, v);
ans[u] = min(ans[u], dis);
u = CT_par[u];
++no;
}
}
long long ret = 1e18;
for(int i=0; i<T; ++i) {
int v = Y[i];
int u = v;
int no = 0;
while(u!=-1) {
long long dis;
if(mem[v][no]!=-1) dis = mem[v][no];
else dis = mem[v][no] = get_dist(u, v);
ret = min(ret, ans[u] + dis);
u = CT_par[u];
++no;
}
}
for(int i=0; i<S; ++i) {
int v = X[i];
int u = v;
while(u!=-1) {
ans[u] = 1e18;
u = CT_par[u];
}
}
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
90876 KB |
Output is correct |
2 |
Correct |
255 ms |
99736 KB |
Output is correct |
3 |
Correct |
263 ms |
99460 KB |
Output is correct |
4 |
Correct |
261 ms |
99524 KB |
Output is correct |
5 |
Correct |
276 ms |
99820 KB |
Output is correct |
6 |
Correct |
183 ms |
99548 KB |
Output is correct |
7 |
Correct |
260 ms |
99568 KB |
Output is correct |
8 |
Correct |
271 ms |
99628 KB |
Output is correct |
9 |
Correct |
267 ms |
99752 KB |
Output is correct |
10 |
Correct |
184 ms |
99584 KB |
Output is correct |
11 |
Correct |
263 ms |
99504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
90476 KB |
Output is correct |
2 |
Correct |
2346 ms |
177084 KB |
Output is correct |
3 |
Correct |
5237 ms |
178600 KB |
Output is correct |
4 |
Correct |
896 ms |
177840 KB |
Output is correct |
5 |
Correct |
7404 ms |
201180 KB |
Output is correct |
6 |
Correct |
5198 ms |
198652 KB |
Output is correct |
7 |
Correct |
936 ms |
130040 KB |
Output is correct |
8 |
Correct |
361 ms |
130684 KB |
Output is correct |
9 |
Correct |
1191 ms |
133588 KB |
Output is correct |
10 |
Correct |
950 ms |
131324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
90876 KB |
Output is correct |
2 |
Correct |
255 ms |
99736 KB |
Output is correct |
3 |
Correct |
263 ms |
99460 KB |
Output is correct |
4 |
Correct |
261 ms |
99524 KB |
Output is correct |
5 |
Correct |
276 ms |
99820 KB |
Output is correct |
6 |
Correct |
183 ms |
99548 KB |
Output is correct |
7 |
Correct |
260 ms |
99568 KB |
Output is correct |
8 |
Correct |
271 ms |
99628 KB |
Output is correct |
9 |
Correct |
267 ms |
99752 KB |
Output is correct |
10 |
Correct |
184 ms |
99584 KB |
Output is correct |
11 |
Correct |
263 ms |
99504 KB |
Output is correct |
12 |
Correct |
40 ms |
90476 KB |
Output is correct |
13 |
Correct |
2346 ms |
177084 KB |
Output is correct |
14 |
Correct |
5237 ms |
178600 KB |
Output is correct |
15 |
Correct |
896 ms |
177840 KB |
Output is correct |
16 |
Correct |
7404 ms |
201180 KB |
Output is correct |
17 |
Correct |
5198 ms |
198652 KB |
Output is correct |
18 |
Correct |
936 ms |
130040 KB |
Output is correct |
19 |
Correct |
361 ms |
130684 KB |
Output is correct |
20 |
Correct |
1191 ms |
133588 KB |
Output is correct |
21 |
Correct |
950 ms |
131324 KB |
Output is correct |
22 |
Correct |
2675 ms |
202952 KB |
Output is correct |
23 |
Correct |
2807 ms |
205560 KB |
Output is correct |
24 |
Correct |
5444 ms |
204952 KB |
Output is correct |
25 |
Correct |
5572 ms |
208620 KB |
Output is correct |
26 |
Correct |
5619 ms |
204852 KB |
Output is correct |
27 |
Correct |
7534 ms |
224060 KB |
Output is correct |
28 |
Correct |
1031 ms |
206452 KB |
Output is correct |
29 |
Correct |
5801 ms |
204684 KB |
Output is correct |
30 |
Correct |
6723 ms |
204304 KB |
Output is correct |
31 |
Correct |
6026 ms |
204884 KB |
Output is correct |
32 |
Correct |
1084 ms |
134516 KB |
Output is correct |
33 |
Correct |
348 ms |
131284 KB |
Output is correct |
34 |
Correct |
644 ms |
127792 KB |
Output is correct |
35 |
Correct |
621 ms |
127760 KB |
Output is correct |
36 |
Correct |
981 ms |
128320 KB |
Output is correct |
37 |
Correct |
895 ms |
128352 KB |
Output is correct |