Submission #147713

#TimeUsernameProblemLanguageResultExecution timeMemory
147713MvCValley (BOI19_valley)C++11
100 / 100
411 ms77304 KiB
#pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") #include<bits/stdc++.h> //#include "boxes.h" #define rc(x) return cout<<x<<endl,0 #define pb push_back #define mkp make_pair #define in insert #define er erase #define fd find #define fr first #define sc second typedef long long ll; typedef long double ld; const ll INF=0x3f3f3f3f3f3f3f3f; const ll llinf=(1LL<<61); const int inf=(1<<30); const int nmax=1e5+50; const int mod=1e9+7; using namespace std; int n,s,q,e,x,y,ex[nmax],ey[nmax],par[nmax][20],fin[nmax],fout[nmax],b[nmax],i,tin,v,l; ll w,ew[nmax],h[nmax],w1,w2,up[nmax],dw[nmax],u[nmax][20],d[nmax][20],w3,vl[nmax],vd[nmax],vu[nmax]; vector<pair<int,ll> >a[nmax]; vector<pair<ll,int> >vc[nmax],vec[nmax]; void dfsdw(int x,int p) { if(b[x])vc[x].pb(mkp(0,x)); for(int i=0;i<a[x].size();i++) { int y=a[x][i].fr; if(y==p)continue; dfsdw(y,x); if(vc[y].empty())continue; vc[x].pb(mkp(vc[y][0].fr+a[x][i].sc,y)); sort(vc[x].begin(),vc[x].end()); if(vc[x].size()==3)vc[x].pop_back(); } vec[x]=vc[x]; } void dfsup(int x,int p,ll z) { up[x]=dw[x]=llinf; if(!vc[x].empty())dw[x]=vc[x][0].fr; if(b[x])up[x]=0; if(x!=p && up[x]) { for(int i=0;i<vc[p].size();i++) { int y=vc[p][i].sc; if(y!=x) { up[x]=vc[p][i].fr+z; vc[x].pb(mkp(up[x],p)); sort(vc[x].begin(),vc[x].end()); if(vc[x].size()==3)vc[x].pop_back(); break; } } } for(int i=0;i<a[x].size();i++) { int y=a[x][i].fr; if(y==p)continue; dfsup(y,x,a[x][i].sc); } } void build(int x,int p,ll z) { vl[x]=z; par[x][0]=p; h[x]=h[p]+z; d[x][0]=u[x][0]=vd[x]=vu[x]=llinf; if(dw[x]!=llinf)vd[x]=dw[x]-h[x]; if(dw[p]!=llinf)d[x][0]=dw[p]-h[p]; if(up[x]!=llinf) { if(up[x])vu[x]=up[x]-vl[x]+h[x]; else vu[x]=up[x]+h[x]; } if(up[p]!=llinf) { if(up[p])u[x][0]=up[p]-vl[p]+h[p]; else u[x][0]=up[p]+h[p]; } fin[x]=++tin; for(int i=1;i<18;i++) { par[x][i]=par[par[x][i-1]][i-1]; d[x][i]=u[x][i]=llinf; d[x][i]=min(d[x][i-1],d[par[x][i-1]][i-1]); u[x][i]=min(u[x][i-1],u[par[x][i-1]][i-1]); } for(int i=0;i<a[x].size();i++) { int y=a[x][i].fr; if(y==p)continue; build(y,x,a[x][i].sc); } fout[x]=++tin; } int anc(int x,int y) { return fin[x]<=fin[y] && fout[y]<=fout[x]; } ll fnd(int x,int y,int bl) { ll rs=llinf; rs=min(rs,vd[x]); for(int i=17;i>=0;i--) { if(!anc(par[x][i],y)) { rs=min(rs,d[x][i]); x=par[x][i]; rs=min(rs,vd[x]); } } if(bl && x!=y) { rs=min(rs,d[x][0]); } return rs; } ll fnu(int x,int y) { ll rs=llinf; rs=min(rs,vu[x]); for(int i=17;i>=0;i--) { if(!anc(par[x][i],y)) { rs=min(rs,u[x][i]); x=par[x][i]; rs=min(rs,vu[x]); } } return rs; } int lca(int x,int y) { if(anc(x,y))return x; if(anc(y,x))return y; for(int i=17;i>=0;i--)if(!anc(par[x][i],y))x=par[x][i]; return par[x][0]; } int main() { //freopen("sol.in","r",stdin); //freopen("sol.out","w",stdout); //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); cin>>n>>s>>q>>e; for(i=1;i<n;i++) { cin>>x>>y>>w; a[x].pb(mkp(y,w)); a[y].pb(mkp(x,w)); ex[i]=x,ey[i]=y,ew[i]=w; } for(i=1;i<=s;i++) { cin>>x; b[x]=1; } dfsdw(e,e); dfsup(e,e,0); build(e,e,0); while(q--) { cin>>x>>y; if(h[ex[x]]>h[ey[x]])swap(ex[x],ey[x]); v=ex[x]; x=ey[x]; if(!anc(x,y))cout<<"escaped\n"; else { w=fnd(y,x,1); if(w!=llinf)w+=h[y]; if(w==llinf)cout<<"oo\n"; else cout<<w<<'\n'; } } return 0; }

Compilation message (stderr)

valley.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
 #pragma GCC optimization("O3")
 
valley.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
 #pragma GCC optimization("unroll-loops")
 
valley.cpp: In function 'void dfsdw(int, int)':
valley.cpp:29:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<a[x].size();i++)
              ~^~~~~~~~~~~~
valley.cpp: In function 'void dfsup(int, int, ll)':
valley.cpp:48:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<vc[p].size();i++)
               ~^~~~~~~~~~~~~
valley.cpp:61:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<a[x].size();i++)
              ~^~~~~~~~~~~~
valley.cpp: In function 'void build(int, int, ll)':
valley.cpp:94:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<a[x].size();i++)
              ~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...