// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
#define nl '\n'
using ll = long long;
using E = array<int, 2>;
const int nax = 5e5+5;
const int LG = 20;
vector<E> adj[nax];
bool vis[nax];
int up[nax][LG];
ll DST[nax][LG];
int par[nax], siz[nax], dep[nax];
ll dx[nax], dy[nax], dst[nax];
const ll INFL = ll(1e18) + 10;
// START OF LCA
void dfs(int u = 0, int p = -1) {
up[u][0] = (p == -1 ? u : p);
for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1];
for(auto e : adj[u]) if (e[0] != p) {
int v = e[0], w = e[1];
dst[v] = dst[u] + w;
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
int jmp(int u, int d) {
for(int i = 0; i < LG; i++) if ((d>>i) & 1) u = up[u][i];
return u;
}
int lca(int a, int b) {
if (dep[a] > dep[b]) swap(a, b);
b = jmp(b, dep[b] - dep[a]); if (a == b) return a;
for(int i = LG - 1; i >= 0; i--) if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];
return up[a][0];
}
ll dist(int u, int v) {
return dst[u] + dst[v] - 2LL * dst[lca(u, v)];
}
// END OF LCA
// START OF CENTROID DECOMPOSITION
int find_size(int u, int p = -1) {
if (vis[u]) return 0;
siz[u] = 1;
for(auto e : adj[u]) if (e[0] != p) {
int v = e[0];
siz[u] += find_size(v, u);
}
return siz[u];
}
int find_centroid(int u, int p, int n) {
for(auto e : adj[u]) if (e[0] != p) {
int v = e[0];
if (!vis[v] && siz[v] > n / 2) return find_centroid(v, u, n);
}
return u;
}
vector<int> anc;
void init_centroid(int u = 0, int p = -1) {
find_size(u);
int c = find_centroid(u, -1, siz[u]);
vis[c] = 1;
par[c] = p;
for(int i = 0; i < int(size(anc)); i++) {
int J = int(size(anc)) - i;
// cout << J << " " << anc[i] << " " << dist(c, anc[i]) << endl;
DST[c][J] = dist(c, anc[i]);
}
anc.push_back(c);
for(auto e : adj[c]) {
int v = e[0];
if (vis[v]) continue;
init_centroid(v, c);
}
anc.pop_back();
}
// END OF CENTROID DECOMPOSITION
void Init(int N, int A[], int B[], int D[]) {
for(int i = 0; i < N; i++) {
vis[i] = 0; par[i] = -1;
dx[i] = dy[i] = INFL;
adj[i] = {};
dst[i] = dep[i] = siz[i] = 0;
for(int j = 0; j < LG; j++) up[i][j] = 0;
}
for(int i = 0; i < N-1; i++) {
adj[A[i]].push_back({B[i], D[i]});
adj[B[i]].push_back({A[i], D[i]});
}
dfs();
init_centroid();
}
vector<int> alt;
ll Query(int S, int X[], int T, int Y[]) {
for(int i = 0; i < S; i++) {
int u = X[i];
dx[u] = 0; alt.push_back(u);
int jump = 0;
while(par[u] != -1) {
u = par[u];
alt.push_back(u);
dx[u] = min(dx[u], DST[X[i]][++jump]);
}
}
ll ans = INFL;
for(int i = 0; i < T; i++) {
int u = Y[i];
int jump = 0;
while(u != -1) {
ans = min(ans, dx[u] + DST[Y[i]][jump++]);
u = par[u];
}
}
for(const auto &u : alt) dx[u] = INFL;
alt.clear();
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12524 KB |
Output is correct |
2 |
Correct |
310 ms |
21788 KB |
Output is correct |
3 |
Correct |
286 ms |
21616 KB |
Output is correct |
4 |
Correct |
297 ms |
22060 KB |
Output is correct |
5 |
Correct |
303 ms |
22092 KB |
Output is correct |
6 |
Correct |
199 ms |
21708 KB |
Output is correct |
7 |
Correct |
279 ms |
21656 KB |
Output is correct |
8 |
Correct |
292 ms |
21800 KB |
Output is correct |
9 |
Correct |
309 ms |
22140 KB |
Output is correct |
10 |
Correct |
190 ms |
21676 KB |
Output is correct |
11 |
Correct |
295 ms |
21840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12372 KB |
Output is correct |
2 |
Correct |
1557 ms |
178976 KB |
Output is correct |
3 |
Correct |
2561 ms |
180604 KB |
Output is correct |
4 |
Correct |
695 ms |
179780 KB |
Output is correct |
5 |
Correct |
3508 ms |
203200 KB |
Output is correct |
6 |
Correct |
2536 ms |
181996 KB |
Output is correct |
7 |
Correct |
801 ms |
53828 KB |
Output is correct |
8 |
Correct |
332 ms |
54572 KB |
Output is correct |
9 |
Correct |
942 ms |
57400 KB |
Output is correct |
10 |
Correct |
801 ms |
55164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12524 KB |
Output is correct |
2 |
Correct |
310 ms |
21788 KB |
Output is correct |
3 |
Correct |
286 ms |
21616 KB |
Output is correct |
4 |
Correct |
297 ms |
22060 KB |
Output is correct |
5 |
Correct |
303 ms |
22092 KB |
Output is correct |
6 |
Correct |
199 ms |
21708 KB |
Output is correct |
7 |
Correct |
279 ms |
21656 KB |
Output is correct |
8 |
Correct |
292 ms |
21800 KB |
Output is correct |
9 |
Correct |
309 ms |
22140 KB |
Output is correct |
10 |
Correct |
190 ms |
21676 KB |
Output is correct |
11 |
Correct |
295 ms |
21840 KB |
Output is correct |
12 |
Correct |
7 ms |
12372 KB |
Output is correct |
13 |
Correct |
1557 ms |
178976 KB |
Output is correct |
14 |
Correct |
2561 ms |
180604 KB |
Output is correct |
15 |
Correct |
695 ms |
179780 KB |
Output is correct |
16 |
Correct |
3508 ms |
203200 KB |
Output is correct |
17 |
Correct |
2536 ms |
181996 KB |
Output is correct |
18 |
Correct |
801 ms |
53828 KB |
Output is correct |
19 |
Correct |
332 ms |
54572 KB |
Output is correct |
20 |
Correct |
942 ms |
57400 KB |
Output is correct |
21 |
Correct |
801 ms |
55164 KB |
Output is correct |
22 |
Correct |
1804 ms |
182576 KB |
Output is correct |
23 |
Correct |
1826 ms |
186704 KB |
Output is correct |
24 |
Correct |
2894 ms |
186740 KB |
Output is correct |
25 |
Correct |
3050 ms |
189108 KB |
Output is correct |
26 |
Correct |
3022 ms |
182776 KB |
Output is correct |
27 |
Correct |
3841 ms |
204792 KB |
Output is correct |
28 |
Correct |
824 ms |
184540 KB |
Output is correct |
29 |
Correct |
3030 ms |
182348 KB |
Output is correct |
30 |
Correct |
2938 ms |
181808 KB |
Output is correct |
31 |
Correct |
2877 ms |
182444 KB |
Output is correct |
32 |
Correct |
944 ms |
61404 KB |
Output is correct |
33 |
Correct |
358 ms |
55848 KB |
Output is correct |
34 |
Correct |
586 ms |
52132 KB |
Output is correct |
35 |
Correct |
575 ms |
52224 KB |
Output is correct |
36 |
Correct |
742 ms |
52612 KB |
Output is correct |
37 |
Correct |
818 ms |
52572 KB |
Output is correct |