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>
#include "factories.h"
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=5e5+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
ll depth[N],parent[N],sz[N],ms[N],dis[N];
pll anc[N][21];
vector<pll> v[N];
vector<ll> lis;
ll dist[N][21],d[N],lv[N];
void DFS(ll id,ll par,ll L){
sz[id]=1;ms[id]=0;
for(auto i:v[id]){
if(i.X==par||lv[i.X]!=-1)continue;
dist[i.X][L]=dist[id][L]+i.Y;
DFS(i.X,id,L);
sz[id]+=sz[i.X];
}
for(auto i:v[id]){
if(i.X==par||lv[i.X]!=-1)continue;
ms[id]=max(ms[id],sz[i.X]);
}
lis.pb(id);
}
void buildc(ll nd,ll pa,ll w){
lis.clear();
dist[nd][lv[pa]]=w;
DFS(nd,0,lv[pa]);
ll cho=lis[0];
for(auto i:lis)if(max(ms[cho],sz[nd]-sz[cho])>max(ms[i],sz[nd]-ms[i]))cho=i;
parent[cho]=pa;
lv[cho]=lv[pa]+1;
for(auto i:v[cho]){
if(lv[i.X]!=-1||i.X==pa)continue;
buildc(i.X,cho,i.Y);
}
}
void Init(int n,int A[],int B[],int D[]){
memset(lv,-1,sizeof(lv));
REP1(i,n)dis[i]=INF;
REP(i,n){
++A[i];++B[i];
v[B[i]].pb(mp(A[i],D[i]));
v[A[i]].pb(mp(B[i],D[i]));
}
lv[0]=0;
buildc(1,0,0);
}
ll Query(int S,int x[],int T,int y[]){
ll ans=INF;
REP(i,S)++x[i];
REP(i,T)++y[i];
REP(i,S){
ll nd=x[i];
while(nd){
dis[nd]=min(dis[nd],(x[i]==nd?0:dist[x[i]][lv[nd]]));
nd=parent[nd];
}
}
REP(i,T){
ll nd=y[i];
while(nd){
ans=min(ans,dis[nd]+(nd==y[i]?0:dist[y[i]][lv[nd]]));
nd=parent[nd];
}
}
REP(i,S){
ll nd=x[i];
while(nd){
dis[nd]=INF;
nd=parent[nd];
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |