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][20],level[100001];
void dfs(int i,int last,int sum){
if(i==E){
flag=1;
return;
}
if(shops[i]==1) mn=min(mn,sum);
for(auto [j,c]:v[i]){
if((i==U&&j==V)||(i==V&&j==U)||j==last) continue;
dfs(j,i,sum+c);
}
return;
}
void calc1(){
while(q--){
flag=0;
mn=100000000000000000;
cin>>l>>x;
U=edges[l-1][0];
V=edges[l-1][1];
dfs(x,0,0);
if(flag==1) cout<<"escaped"<<endl;
else if(mn!=100000000000000000) cout<<mn<<endl;
else cout<<"oo"<<endl;
}
}
void parent(int i,int last){
t[i][0]=last;
level[i]=level[last]+1;
for(auto [j,c]:v[i]) if(j!=last) parent(j,i);
return;
}
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=20;i>=0;i--){
if(k-(1<<i)>=0){
k-=(1<<i);
a=t[a][i];
}
}
if(a==b) return a;
for(int i=20;i>=0;i--){
if(t[a][i]!=t[b][i]){
a=t[a][i];
b=t[b][i];
}
}
return t[a][0];
}
void calc2(){
for(int k=1;k<=20;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(lca(E,x),y)!=lca(E,x)) cout<<"escaped"<<endl;
else cout<<0<<endl;
}
}
signed main()
{
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;
}
if(S!=n) calc1();
else calc2();
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void dfs(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,c]:v[i]){
| ^
valley.cpp: In function 'void parent(long long int, long long int)':
valley.cpp:37:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for(auto [j,c]:v[i]) if(j!=last) parent(j,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... |