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;
const int MAX = 5e5, LOG = 20;
const long long int INF = 1e18;
int n,q;
vector<int> g[MAX], p[MAX];
vector<int> rList;
int troidProf[MAX], troidPai[LOG][MAX], sub[MAX];
long long int troidDist[LOG][MAX], mnDist[MAX];
bool isTroid[MAX], active[MAX];
void CalcSub(int v, int pai)
{
sub[v] = 1;
for(int i=0;i<g[v].size();i++)
{
int prox = g[v][i];
if(prox == pai || isTroid[prox]) continue;
CalcSub(prox,v);
sub[v] += sub[prox];
}
}
int FindTroid(int v, int pai, int sz)
{
for(int i=0;i<g[v].size();i++)
{
int prox = g[v][i];
if( prox == pai || isTroid[prox] ) continue;
if( sub[prox] > sz/2 ) return FindTroid(prox,v,sz);
}
return v;
}
void CalcDist(int v, int pai, int prof, int troid)
{
troidPai[prof][v] = troid;
for(int i=0;i<g[v].size();i++)
{
int prox = g[v][i], peso = p[v][i];
if(prox == pai || isTroid[prox]) continue;
troidDist[prof][prox] = troidDist[prof][v] + peso;
CalcDist(prox,v,prof,troid);
}
return;
}
void Decompose(int v, int prof)
{
CalcSub(v,v);
int tempN = sub[v];
int troid = FindTroid(v,v,tempN);
isTroid[v] = 1;
troidProf[troid] = troid;
CalcDist(troid,troid,prof,troid);
for(int i=0;i<g[troid].size();i++)
{
int prox = g[troid][i];
if(isTroid[prox]) continue;
Decompose(prox,prof+1);
}
}
void Init(int N, int A[], int B[], int D[])
{
for(int i=0;i<n-1;i++)
{
g[ A[i] ].push_back( B[i] ); g[ B[i] ].push_back( A[i] );
p[ A[i] ].push_back( D[i] ); p[ B[i] ].push_back( D[i] );
}
Decompose(0,0);
for(int i=0;i<N;i++) sub[i] = 0;
return;
}
long long int Query(int S, int X[], int T, int Y[])
{
long long int resp = INF;
for(int i=0;i<S;i++)
{
int v = X[i];
for(int j=0;j<=troidProf[v];j++)
{
int troid = troidPai[j][v];
if(active[troid]) mnDist[troid] = min( mnDist[troid] , troidDist[j][v] );
else active[troid] = 1, mnDist[troid] = troidDist[j][v];
rList.push_back(troid);
}
}
for(int i=0;i<T;i++)
{
int v = Y[i];
for(int j=0;j<=troidProf[v];j++)
{
int troid = troidPai[j][v];
if(!active[troid]) continue;
resp = min( resp , mnDist[troid] + troidDist[j][v] );
}
}
for(int i=0;i<rList.size();i++) active[rList[i]] = 0;
return resp;
}
Compilation message (stderr)
factories.cpp: In function 'void CalcSub(int, int)':
factories.cpp:15:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++)
~^~~~~~~~~~~~
factories.cpp: In function 'int FindTroid(int, int, int)':
factories.cpp:25:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++)
~^~~~~~~~~~~~
factories.cpp: In function 'void CalcDist(int, int, int, int)':
factories.cpp:36:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++)
~^~~~~~~~~~~~
factories.cpp: In function 'void Decompose(int, int)':
factories.cpp:53:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[troid].size();i++)
~^~~~~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:96:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<rList.size();i++) active[rList[i]] = 0;
~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |