Submission #923073

#TimeUsernameProblemLanguageResultExecution timeMemory
923073AiperiiiValley (BOI19_valley)C++14
100 / 100
326 ms54148 KiB
#include <bits/stdc++.h> #define int long long #define ff first #define ss second #define pb push_back #define all(x) x.begin(),x.end() using namespace std; const int N=1e5+5; vector <pair <int,int > > g[N]; int d[N],par[N],jmp[N][20],dis[N],shop[N],dis2[N],jmp_mn[N][20]; void dfs(int v,int p){ par[v]=p; for(auto to : g[v]){ if(to.ff!=p){ dis[to.ff]=dis[v]+to.ss; d[to.ff]=d[v]+1; dfs(to.ff,v); } } } void dfs2(int v,int p){ for(auto to : g[v]){ if(to.ff!=p)dfs2(to.ff,v); } if(shop[v])dis2[v]=dis[v]; dis2[p]=min(dis2[p],dis2[v]); } int lca(int a,int b){ if(d[a]<d[b])swap(a,b); for(int i=19;i>=0;i--){ if(d[jmp[a][i]]>=d[b]){ a=jmp[a][i]; } } if(a==b)return a; for(int i=19;i>=0;i--){ if(jmp[a][i]!=jmp[b][i]){ a=jmp[a][i]; b=jmp[b][i]; } } return par[a]; } signed main(){ ios_base::sync_with_stdio(); cin.tie(0);cout.tie(0); int n,s,q,e; cin>>n>>s>>q>>e; vector <int> mag(s),a(n),b(n),c(n); for(int i=0;i<n-1;i++){ cin>>a[i]>>b[i]>>c[i]; g[a[i]].pb({b[i],c[i]}); g[b[i]].pb({a[i],c[i]}); } dfs(e,0); for(int i=0;i<s;i++){ cin>>mag[i]; shop[mag[i]]=1; } for(int i=1;i<=n;i++){ dis2[i]=1e18; } dfs2(e,0); for(int i=1;i<=n;i++){ if(dis2[i]!=1e18)dis2[i]-=2*dis[i]; } par[e]=e; for(int i=1;i<=n;i++){ jmp[i][0]=par[i]; jmp_mn[i][0]=dis2[par[i]]; } for(int j=1;j<20;j++){ for(int i=1;i<=n;i++){ jmp[i][j]=jmp[jmp[i][j-1]][j-1]; jmp_mn[i][j]=min(jmp_mn[i][j-1],jmp_mn[jmp[i][j-1]][j-1]); } } while(q--){ int ind,x; cin>>ind>>x; ind--; int u=a[ind]; int v=b[ind]; if(d[u]<d[v])swap(u,v); if(lca(u,x)!=u)cout<<"escaped\n"; else{ int ans=dis[x],mn=dis2[x]; for(int i=19;i>=0;i--){ if(d[jmp[x][i]]>=d[u]){ mn=min(mn,jmp_mn[x][i]); x=jmp[x][i]; } } mn=min(mn,dis2[x]); if(mn==1e18)cout<<"oo\n"; else cout<<ans+mn<<"\n"; } } } /* */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...