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 <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include <time.h>
#endif
#define all(a) (a).begin(), (a).end()
#define rev(a) (a).rbegin(), (a).rend()
#define F first
#define S second
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) \
{ \
++recur_depth; \
auto x_ = x; \
--recur_depth; \
cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":" \
<< __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl; \
}
#else
#define dbg(x)
#endif
using namespace std;
using namespace __gnu_pbds;
typedef pair<int, int> ii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> llll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int>> vii;
typedef vector<vii> vvii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pair<ll, ll>> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
template <class type1>
using ordered_set = tree<type1, null_type, less<type1>, rb_tree_tag,
tree_order_statistics_node_update>;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
!is_same<T_container, string>::value,
typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
os << '{';
string sep;
for (const T &x : v)
os << sep << x, sep = ", ";
return os << '}';
}
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e18;
const ld EPS = 1e-9;
const int MAXLG = 22;
const int MAX_EULER_N = 1e6 + 10;
const int MAXN = 5e5 + 10;
struct ST {
int n;
ii st[MAXLG][MAX_EULER_N];
int lg2[MAX_EULER_N];
void init(int n_, vii &a) {
n = n_;
for (int i = 0; i < n; i++)
st[0][i] = a[i];
for (int j = 1; j < MAXLG; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
lg2[1] = 0;
for (int i = 2; i < MAX_EULER_N; i++)
lg2[i] = lg2[i / 2] + 1;
}
llll query(int l, int r) {
if (l > r)
swap(l, r);
int k = lg2[r - l + 1];
return min(st[k][l], st[k][r - (1 << k) + 1]);
}
};
struct CD {
int n;
vll adj[MAXN];
int sub_sz[MAXN];
bool rm[MAXN];
int cpar[MAXN];
ll dist_0[MAXN];
int depth[MAXN];
vii euler;
int first_occ[MAXN];
ST st;
ll val[MAXN];
void init(int n_, vvll &a) {
n = n_;
for (int i = 0; i < n; i++)
adj[i] = a[i];
for (int i = 0; i < n; i++)
sub_sz[i] = first_occ[i] = 0, rm[i] = 0, cpar[i] = depth[i] = -1,
dist_0[i] = val[i] = INF;
dfs(0);
decompose();
st.init(euler.size(), euler);
}
void dfs(int u, int p = -1, ll d = 0, int de = 0) {
dist_0[u] = d;
depth[u] = de;
first_occ[u] = euler.size();
euler.push_back({de, u});
for (auto [v, w] : adj[u])
if (v != p) {
dfs(v, u, d + w, de + 1);
euler.push_back({de, u});
}
}
int lca(int u, int v) { return st.query(first_occ[u], first_occ[v]).second; }
ll dist(int u, int v) {
return dist_0[u] + dist_0[v] - 2 * dist_0[lca(u, v)];
}
int get_sub_sz(int u, int p = -1) {
sub_sz[u] = 1;
for (auto [v, _] : adj[u])
if (v != p && !rm[v])
sub_sz[u] += get_sub_sz(v, u);
return sub_sz[u];
}
int get_centroid(int u, int ssz, int p = -1) {
for (auto [v, _] : adj[u])
if (v != p && !rm[v] && sub_sz[v] * 2 > ssz)
return get_centroid(v, ssz, u);
return u;
}
void decompose(int u = 0, int par = -1) {
int c = get_centroid(u, get_sub_sz(u));
rm[c] = 1;
if (par != -1)
cpar[c] = par;
for (auto [v, _] : adj[c])
if (!rm[v])
decompose(v, c);
}
void upd(int u, bool flag) {
for (int v = u; v != -1; v = cpar[v]) {
if (!flag)
val[v] = min(val[v], dist(u, v));
else
val[v] = INF;
}
}
ll query(int u) {
ll res = INF;
for (int v = u; v != -1; v = cpar[v])
res = min(res, val[v] + dist(u, v));
return res;
}
} cd;
void Init(int N, int A[], int B[], int D[]) {
vvll adj(N);
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]});
cd.init(N, adj);
}
long long Query(int S, int X[], int T, int Y[]) {
ll res = INF;
for (int i = 0; i < S; i++)
cd.upd(X[i], 0);
for (int i = 0; i < T; i++)
res = min(res, cd.query(Y[i]));
for (int i = 0; i < S; i++)
cd.upd(X[i], 1);
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |