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>
using namespace std;
#define int long long
#define endl "\n"
int n,S,E,q,U,V,x,l,mn,flag,y;
vector <array<int,2>> v[100001],edges;
priority_queue <array<int,2>> qu;
int shops[100001],t[100001][19],level[100001],close[100001],dis[100001];
array <int,2> parent(int i,int last,int cnt){
t[i][0]=last;
level[i]=level[last]+1;
dis[i]=cnt;
int ans=1e15,ind=0;
for(auto [j,w]:v[i]){
if(j!=last){
auto a=parent(j,i,cnt+w);
if(ans>=a[0]+w){
ind=a[1];
ans=a[0]+w;
}
}
}
if(shops[i]==1){
close[i]=i;
return {0,i};
}
close[i]=ind;
return {ans,ind};
}
int lca(int a,int b){
int la=level[a],lb=level[b],k;
if(la<lb){
swap(a,b);
swap(la,lb);
}
k=la-lb;
for(int i=18;i>=0;i--){
if(k-(1<<i)>=0){
k-=(1<<i);
a=t[a][i];
}
}
if(a==b) return a;
for(int i=18;i>=0;i--){
if(t[a][i]!=t[b][i]){
a=t[a][i];
b=t[b][i];
}
}
return t[a][0];
}
void calc2(){
parent(E,0,0);
for(int k=1;k<=18;k++){
for(int i=1;i<=n;i++) t[i][k]=t[t[i][k-1]][k-1];
}
while(q--){
cin>>l>>x;
if(level[edges[l-1][0]]>level[edges[l-1][1]]) y=edges[l-1][0];
else y=edges[l-1][1];
if(lca(y,x)!=y) cout<<"escaped"<<endl;
else{
if(close[y]==0) cout<<"oo"<<endl;
else{
int b=lca(close[y],x);
if(close[y]==b) cout<<dis[x]-dis[close[y]]<<endl;
else if(x==b) cout<<dis[close[y]]-dis[x]<<endl;
else cout<<dis[close[y]]+dis[x]-(dis[y]*2)<<endl;
}
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>S>>q>>E;
for(int i=0;i<n-1;i++){
int a,b,c;
cin>>a>>b>>c;
v[a].push_back({b,c});
v[b].push_back({a,c});
edges.push_back({a,b});
}
for(int i=0;i<S;i++){
int a;
cin>>a;
shops[a]=1;
}
calc2();
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'std::array<long long int, 2> parent(long long int, long long int, long long int)':
valley.cpp:15:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
15 | for(auto [j,w]:v[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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |