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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define F first
#define S second
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) lli(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef long long int lli;
typedef pair <lli,lli> ii;
typedef pair <ii,lli> iii;
typedef vector <lli> vi;
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int n;
vector<ii> al[500001];
lli dp[500001],par[500001][20],par1[500001],lev[500001],din[500001],dout[500001],t=0,sz[500001],vis[500001],vis1[500001],pre[500001],r;
lli dist[5*100001][20];
lli lev1[5*100001];
void dfs(int node)
{
sz[node]=1;
vis1[node]=1;
for(int i=0;i<al[node].size();++i)
{
if(vis1[al[node][i].F]+vis[al[node][i].F]==0)
{
dfs(al[node][i].F);
sz[node]+=sz[al[node][i].F];
}
}
}
int tell(int node,int p,int to)
{
for(int i=0;i<al[node].size();++i)
{
if(al[node][i].F!=p&&sz[al[node][i].F]>to/2&&vis[al[node][i].F]==0)
return tell(al[node][i].F,node,to);
}
return node;
}
void dfs1(int node,int p,int cen,lli l)
{
pre[node]=cen;
dist[node][lev1[cen]]=l;
for(int i=0;i<al[node].size();++i)
if(al[node][i].F!=p&&vis[al[node][i].F]==0)
dfs1(al[node][i].F,node,cen,l+al[node][i].S);
}
void dfs2(int node,int p,lli l)
{
par[node][0]=p;
for(int i=1;i<20;++i)
par[node][i]=par[par[node][i-1]][i-1];
lev[node]=l;
din[node]=t++;
for(int i=0;i<al[node].size();++i)
{
if(al[node][i].F!=p)
dfs2(al[node][i].F,node,l+al[node][i].S);
}
dout[node]=t++;
}
bool isa(int f,int s)
{
return (din[f]<=din[s])&&(dout[f]>=dout[s]);
}
int lca(int f,int s)
{
if(isa(f,s))
return f;
if(isa(s,f))
return s;
for(int i=19;i>=0;--i)
if(!isa(par[f][i],s))
f=par[f][i];
return par[f][0];
}
void Init(int N, int A[], int B[], int D[])
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
n=N;
for(int i=0;i<n-1;++i)
{
al[A[i]].pb(mp(B[i],D[i]));
al[B[i]].pb(mp(A[i],D[i]));
}
dfs2(0,0,0);
int c=0,xy=0;
while(c!=n)
{
for(int i=0;i<n;++i)
{
if(vis[i]+vis1[i]==0)
{
dfs(i);
int cen=tell(i,i,sz[i]);
par1[cen]=pre[i];
lev1[cen]=xy;
dfs1(cen,cen,cen,0);
c++;
vis[cen]=1;
if(c==1)
r=cen;
}
}
memset(vis1,0,sizeof(vis1));
xy++;
}
for(int i=0;i<n;++i)
dp[i]=1000000000ll*5000000;
}
long long Query(int S, int X[], int T, int Y[])
{
vi v;
for(lli i=0;i<S;++i)
{
lli cur=X[i];
while(1)
{
lli l=lca(X[i],cur);
dp[cur]=min(dp[cur],lev[X[i]]+lev[cur]-2*lev[l]);
v.pb(cur);
if(cur==r)
break;
cur=par1[cur];
}
}
lli ans=1000000000ll*5000000;
for(lli i=0;i<T;++i)
{
lli cur=Y[i];
while(1)
{
ans=min(ans,dp[cur]+dist[Y[i]][lev1[cur]]);
if(cur==r)
break;
cur=par1[cur];
}
}
while(v.size())
{
dp[v.back()]=1000000000ll*5000000;
v.pop_back();
}
return ans;
}
Compilation message (stderr)
factories.cpp: In function 'void dfs(int)':
factories.cpp:32:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i=0;i<al[node].size();++i)
| ~^~~~~~~~~~~~~~~~
factories.cpp: In function 'int tell(int, int, int)':
factories.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i=0;i<al[node].size();++i)
| ~^~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs1(int, int, int, lli)':
factories.cpp:54:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i=0;i<al[node].size();++i)
| ~^~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs2(int, int, lli)':
factories.cpp:65:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i=0;i<al[node].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... |