This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "factories.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <functional>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>
using namespace std;
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long
using ll = long long;
const int MXN = 5e5 + 5, MXK = 20;
const ll INF = 1e18;
vector<pair<int, ll>> g[MXN];
int cd_par[MXN], binlift_par[MXN][MXK], tin[MXN], tout[MXN], subtree[MXN];
ll binlift_dist[MXN][MXK], best[MXN];
bool vis[MXN];
int timer;
void dfs1(int u, int p) {
tin[u] = timer++;
for (int j = 1; j < MXK; j++) {
binlift_par[u][j] = binlift_par[binlift_par[u][j - 1]][j - 1];
binlift_dist[u][j] = binlift_dist[u][j - 1] + binlift_dist[binlift_par[u][j - 1]][j - 1];
}
for (const auto &[v, w] : g[u]) {
if (v == p) continue;
binlift_par[v][0] = u;
binlift_dist[v][0] = w;
dfs1(v, u);
}
tout[u] = timer - 1;
}
bool is_anc(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
int query_lca(int u, int v) {
if (is_anc(u, v)) return u;
if (is_anc(v, u)) return v;
for (int j = MXK - 1; j >= 0; j--) {
int lift = binlift_par[u][j];
if (!is_anc(lift, v))
u = lift;
}
return binlift_par[u][0];
}
int query_dist(int u, int v) {
if (u == v) return 0;
ll res = 0;
for (int j = MXK - 1; j >= 0; j--) {
int lift = binlift_par[u][j];
if (!is_anc(lift, v)) {
res += binlift_dist[u][j];
u = lift;
}
}
return res + binlift_dist[u][0];
}
ll dist(int u, int v) {
if (u == v) return 0;
int cur_lca = query_lca(u, v);
return query_dist(u, cur_lca) + query_dist(v, cur_lca);
}
void dfs_subtree(int u, int p) {
subtree[u] = 1;
for (const auto &[v, w] : g[u]) {
if (v == p || vis[v]) continue;
dfs_subtree(v, u);
subtree[u] += subtree[v];
}
}
int centroid(int u, int p, int vertices) {
for (const auto &[v, w] : g[u]) {
if (v == p || vis[v]) continue;
if (subtree[v] * 2 > vertices)
return centroid(v, u, vertices);
}
return u;
}
void build_cd(int u, int p) {
dfs_subtree(u, -1);
int c = centroid(u, -1, subtree[u]);
if (p == -1)
p = c;
cd_par[c] = p;
vis[c] = true;
for (const auto &[v, w] : g[c]) {
if (vis[v]) continue;
build_cd(v, c);
}
}
void build_factory(int u) {
int v = u;
best[v] = 0;
do {
v = cd_par[v];
best[v] = min(best[v], dist(u, v));
} while (v != cd_par[v]);
}
void remove_factory(int v) {
best[v] = INF;
do {
v = cd_par[v];
best[v] = INF;
} while (v != cd_par[v]);
}
ll query_factory(int u) {
int v = u;
ll ans = best[v];
do {
v = cd_par[v];
ans = min(ans, dist(u, v) + best[v]);
} while (v != cd_par[v]);
return ans;
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N - 1; i++) {
g[A[i]].emplace_back(B[i], D[i]);
g[B[i]].emplace_back(A[i], D[i]);
}
dfs1(0, -1);
build_cd(0, -1);
for (int i = 1; i <= N; i++)
best[i] = INF;
}
ll Query(int S, int X[], int T, int Y[]) {
ll ans = INF;
for (int i = 0; i < S; i++)
build_factory(X[i]);
for (int i = 0; i < T; i++)
ans = min(ans, query_factory(Y[i]));
for (int i = 0; i < S; i++)
remove_factory(X[i]);
return ans;
}
// void solve() {
// int N, Q;
// cin >> N >> Q;
// int A[N - 1], B[N - 1], D[N - 1];
// for (int i = 0; i < N - 1; i++)
// cin >> A[i] >> B[i] >> D[i];
// Init(N, A, B, D);
// while (Q--) {
// int S, T;
// cin >> S >> T;
// int X[S], Y[T];
// for (int i = 0; i < S; i++)
// cin >> X[i];
// for (int i = 0; i < T; i++)
// cin >> Y[i];
// cout << Query(S, X, T, Y) << "\n";
// }
// }
// signed main() {
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr);
// int TC = 1;
// // cin >> TC;
// while (TC--) solve();
// }
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |