#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
const int MAXN = 5e5 + 5;
const int LOGN = 20;
const ll INF = 1e17;
vector<pair<int, int> > gr[MAXN];
int par[MAXN][LOGN], lvl[MAXN];
//ll dist[MAXN];
map<int, ll> dist[MAXN];
bool mark[MAXN];
int sz[MAXN], cpar[MAXN], root;
ll ans[MAXN];
void init(int u) {
for (int j = 1; j < LOGN; j++)
if (par[u][j - 1] != -1)
par[u][j] = par[par[u][j - 1]][j - 1];
for (auto v : gr[u])
if (v.fi != par[u][0]) {
//dist[v.fi] = dist[u] + v.se;
par[v.fi][0] = u;
lvl[v.fi] = lvl[u] + 1;
init(v.fi);
}
}
void dfs(int u, int p, const int who) {
sz[u] = 1;
for (auto v : gr[u])
if (v.fi != p && !mark[v.fi]) {
dist[v.fi][who] = dist[u][who] + v.se;
dfs(v.fi, u, who);
sz[u] += sz[v.fi];
}
}
int decompose(int u, int p, const int n) {
int mx = -1, big = -1;
for (auto v : gr[u])
if (v.fi != p && !mark[v.fi] && sz[v.fi] > mx)
mx = sz[v.fi], big = v.fi;
//printf("i am at %d and big child is %d\n", u, big);
if (2 * mx <= n) {
mark[u] = true;
//printf("START OF VISIT OF %d\n", u);
for (auto v : gr[u])
if (!mark[v.fi]) {
dist[v.fi][u] = v.se;
dfs(v.fi, u, u);
int cent = decompose(v.fi, u, sz[v.fi]);
cpar[cent] = u;
}
//printf("END OF VISIT OF %d\n", u);
return u;
} else {
return decompose(big, u, n);
}
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N - 1; i++) {
gr[A[i]].pb(mp(B[i], D[i]));
gr[B[i]].pb(mp(A[i], D[i]));
}
memset(par, -1, sizeof par);
init(0);
memset(cpar, -1, sizeof cpar);
dfs(0, -1, -1);
root = decompose(0, -1, sz[0]);
fill(ans, ans + MAXN, INF);
/*printf("centroid is %d\n", root);
for (int u = 0; u < N; u++)
printf("cpar[%d] = %d\n", u, cpar[u]);*/
}
unordered_map<int, int> memo[MAXN];
int LCA(int u, int v) {
if (lvl[u] > lvl[v]) swap(u, v);
if (memo[u].count(v))
return memo[u][v];
int &ret = memo[u][v];
for (int j = LOGN - 1; j >= 0; j--)
if (lvl[u] + (1 << j) <= lvl[v])
v = par[v][j];
if (u == v) return ret = u;
for (int j = LOGN - 1; j >= 0; j--)
if (par[u][j] != -1 && par[u][j] != par[v][j])
u = par[u][j], v = par[v][j];
return ret = par[u][0];
}
/*
ll far(int u, int v) {
int l = LCA(u, v);
return dist[u] + dist[v] - dist[l] * 2LL;
}
*/
ll Query(int S, int X[], int T, int Y[]) {
for (int i = 0; i < S; i++) {
int u = X[i];
while (u != -1) {
ans[u] = min(ans[u], dist[X[i]][u]);
u = cpar[u];
}
}
ll res = INF;
for (int i = 0; i < T; i++) {
int u = Y[i];
while (u != -1) {
res = min(res, dist[Y[i]][u] + ans[u]);
u = cpar[u];
}
}
for (int i = 0; i < S; i++) {
int u = X[i];
while (u != -1) {
ans[u] = INF;
u = cpar[u];
}
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
136384 KB |
Output is correct |
2 |
Correct |
1006 ms |
138760 KB |
Output is correct |
3 |
Correct |
1353 ms |
139552 KB |
Output is correct |
4 |
Correct |
1386 ms |
139552 KB |
Output is correct |
5 |
Correct |
1819 ms |
140648 KB |
Output is correct |
6 |
Correct |
339 ms |
137344 KB |
Output is correct |
7 |
Correct |
1236 ms |
139552 KB |
Output is correct |
8 |
Correct |
1496 ms |
139684 KB |
Output is correct |
9 |
Correct |
1643 ms |
140644 KB |
Output is correct |
10 |
Correct |
423 ms |
137344 KB |
Output is correct |
11 |
Correct |
1189 ms |
139552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
136384 KB |
Output is correct |
2 |
Execution timed out |
6000 ms |
508228 KB |
Execution timed out |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
6000 ms |
504268 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |