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<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<cmath>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define pb push_back
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
//#define int long long
#define mod 1000000007
#define int long long
const int mxn=1e5,lg=17,inf=1e18;
vector<pii>adj[mxn+10];
vector<int>shop;
vector<pair<pii,int>>edge;
int up[mxn+10][20],h[mxn+10],sdist[mxn+10],dist[mxn+10];
int dp[mxn+10][20];
void solve(int cur,int p){
for(auto i:adj[cur]){
if(i.f==p)continue;
h[i.f]=h[cur]+1;
up[i.f][0]=cur;
dist[i.f]=dist[cur]+i.s;
solve(i.f,cur);
sdist[cur]=min(sdist[cur],sdist[i.f]+i.s);
}
dp[cur][0]=sdist[cur];
}
int caldist(int a,int b){return abs(dist[a]-dist[b]);}
int cal(int node,int k){
int ans=inf,d=0;
for(int i=0;i<=lg;i++){
if(k&(1<<i)){
ans=min(ans,dp[node][i]+d);
d+=abs(dist[node]-dist[up[node][i]]);
node=up[node][i];
}
}
return ans;
}
int32_t main(){
fastio
int n,m,q,e;cin>>n>>m>>q>>e;
for(int i=0;i<n-1;i++){
int u,v,w;cin>>u>>v>>w;
adj[u].pb({v,w});
adj[v].pb({u,w});
edge.pb({{u,v},w});
}
fill(sdist,sdist+n+1,inf);
for(int i=1;i<=lg;i++)for(int j=1;j<=n;j++)dp[j][i]=inf;
for(int i=0;i<m;i++){
int a;cin>>a;
sdist[a]=0;
}
solve(e,-1);
for(int i=1;i<=lg;i++){
for(int j=1;j<=n;j++){
up[j][i]=up[up[j][i-1]][i-1];
dp[j][i]=min(dp[j][i-1],abs(dist[j]-dist[up[j][i-1]])+dp[up[j][i-1]][i-1]);
}
}
while(q--){
int a,b;cin>>a>>b;
a--;
if(h[edge[a].f.f]>h[edge[a].f.s])a=edge[a].f.f;
else a=edge[a].f.s;
if(h[a]<=h[b]){
int cur=b;
int k=h[b]-h[a];
for(int i=0;i<=lg;i++)if(k&(1<<i))cur=up[cur][i];
if(cur==a){
int k=h[b]-h[a]+1;
int g=cal(b,k);
if(g!=inf)cout<<g<<'\n';
else cout<<"oo"<<'\n';
}
else cout<<"escaped"<<'\n';
}
else cout<<"escaped"<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |