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 <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
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];
ll minn[MAX_N];
vector <int> g[MAX_N], d[MAX_N], pCentroid[MAX_N], dCentroid[MAX_N];
vector <int> is;
bool deleted[MAX_N];
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 dfs(int v, int p, int center, int dist = 0) {
pCentroid[v].pb(center);
dCentroid[v].pb(dist);
for(int i = 0; i < g[v].size(); ++i) {
int to = g[v][i], len = d[v][i];
if(!deleted[to] && to != p)
dfs(to, v, center, dist + len);
}
}
void process(int center) {
deleted[center] = true;
build(center, -1);
dfs(center, -1, center);
for(int to : g[center])
if(!deleted[to])
process(centroid(to, -1, sz[to]));
}
void Init(int N, int A[], int B[], int D[]) {
n = N;
for(int i = 0; i < n; ++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]);
}
build(1);
process(centroid(1, -1, sz[1]));
}
long long Query(int S, int X[], int T, int Y[]) {
ll ans = inf;
for(int i = 0; i < T; ++i)
for(int j = 0; j < pCentroid[Y[i]].size(); ++j)
minn[pCentroid[Y[i]][j]] = inf;
for(int i = 0; i < S; ++i)
for(int j = 0; j < pCentroid[X[i]].size(); ++j)
minn[pCentroid[X[i]][j]] = inf;
for(int i = 0; i < S; ++i)
for(int j = 0; j < pCentroid[X[i]].size(); ++j) {
int s = pCentroid[X[i]][j];
ll d = dCentroid[X[i]][j];
minn[s] = min(minn[s], d);
}
for(int i = 0; i < T; ++i)
for(int j = 0; j < pCentroid[Y[i]].size(); ++j) {
int s = pCentroid[Y[i]][j];
ll d = dCentroid[Y[i]][j];
ans = min(ans, minn[s] + 1LL * d);
}
return ans;
}
Compilation message (stderr)
factories.cpp: In function 'void dfs(int, int, int, int)':
factories.cpp:39:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[v].size(); ++i) {
~~^~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:71:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < pCentroid[Y[i]].size(); ++j)
~~^~~~~~~~~~~~~~~~~~~~~~~~
factories.cpp:74:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < pCentroid[X[i]].size(); ++j)
~~^~~~~~~~~~~~~~~~~~~~~~~~
factories.cpp:77:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < pCentroid[X[i]].size(); ++j) {
~~^~~~~~~~~~~~~~~~~~~~~~~~
factories.cpp:83:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < pCentroid[Y[i]].size(); ++j) {
~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |