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 <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define mp make_pair
const int l = 20;
const ll inf = 2e15;
const int MAX_N = 5e5 + 15;
const int MAX_Q = 1e6 + 15;
const int MAX_SUM_ST = 1e6 + 15;
const int MAX_VALUE = 1e9;
int n, m, sz[MAX_N], tin[MAX_N], tout[MAX_N], last, loglog[MAX_N];
pair <int, int> st[MAX_N][l];
vector <pair <ll, int>> cur;
ll minn[MAX_N];
vector <int> g[MAX_N], d[MAX_N];
int pCentroid[MAX_N];
ll dist[MAX_N], dd[MAX_N];
bool deleted[MAX_N];
void start(int v, int p) {
cur.pb(mp(dd[v], v));
for(int i = 0; i < g[v].size(); ++i)
if(g[v][i] != p) {
dist[g[v][i]] = dist[v] + d[v][i];
dd[g[v][i]] = dd[v] + 1;
start(g[v][i], v);
cur.pb(mp(dd[v], v));
}
}
void buildlca() {
for(int i = 0; i < cur.size(); ++i)
if(!tin[cur[i].second])
tin[cur[i].second] = i;
for(int i = 2; i < MAX_N; ++i)
loglog[i] = loglog[i >> 1] + 1;
for(int i = 0; i + 1 < cur.size(); ++i)
st[i][0] = min(cur[i], cur[i+1]);
for(int j = 1; (1 << j) < cur.size(); ++j)
for(int i = 0; i + (1 << (j - 1)) < cur.size(); ++i)
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int lca(int a, int b) {
int l = min(tin[a], tin[b]), r = max(tin[a], tin[b]);
int lg = loglog[tin[b] - tin[a] + 1];
return min(st[l][lg], st[r - (1 << lg) + 1][lg]).second;
}
void build(int v, int p = -1) {
sz[v] = 1;
for(auto to : g[v])
if(to != p && !deleted[to]) {
build(to, v);
sz[v] += sz[to];
}
}
int centroid(int v, int p, int n) {
for(auto to : g[v])
if(!deleted[to] && to != p && sz[to] > n / 2)
return centroid(to, v, n);
return v;
}
void process(int node, int p = -1) {
build(node, -1);
int center = centroid(node, node, sz[node]);
deleted[center] = true;
if(p == -1)
p = center;
pCentroid[center] = p;
for(int to : g[center])
if(!deleted[to])
process(to, center);
}
void Init(int N, int A[], int B[], int D[]) {
n = N;
for(int i = 0; i < n; ++i) {
++A[i];
++B[i];
g[A[i]].pb(B[i]);
d[A[i]].pb(D[i]);
g[B[i]].pb(A[i]);
d[B[i]].pb(D[i]);
}
fill(minn, minn + MAX_N, inf);
start(1, 1);
buildlca();
process(1);
}
long long Query(int S, int X[], int T, int Y[]) {
ll ans = inf;
for(int i = 0; i < S; ++i)
++X[i];
for(int i = 0; i < T; ++i)
++Y[i];
for(int i = 0; i < S; ++i) {
int v = X[i];
while(1) {
minn[v] = min(minn[v], dist[X[i]] + dist[v] - 2 * dist[lca(X[i], v)]);
if(v == pCentroid[v])
break;
v = pCentroid[v];
}
}
for(int i = 0; i < T; ++i) {
int v = Y[i];
while(1) {
ans = min(ans, dist[Y[i]] + dist[v] - 2 * dist[lca(Y[i], v)] + minn[v]);
if(v == pCentroid[v])
break;
v = pCentroid[v];
}
}
for(int i = 0; i < S; ++i) {
int v = X[i];
while(1) {
minn[v] = inf;
if(v == pCentroid[v])
break;
v = pCentroid[v];
}
}
return ans;
}
Compilation message (stderr)
factories.cpp: In function 'void start(int, int)':
factories.cpp:26:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[v].size(); ++i)
~~^~~~~~~~~~~~~
factories.cpp: In function 'void buildlca()':
factories.cpp:36:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < cur.size(); ++i)
~~^~~~~~~~~~~~
factories.cpp:41:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i + 1 < cur.size(); ++i)
~~~~~~^~~~~~~~~~~~
factories.cpp:43:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1; (1 << j) < cur.size(); ++j)
~~~~~~~~~^~~~~~~~~~~~
factories.cpp:44:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i + (1 << (j - 1)) < cur.size(); ++i)
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |