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>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define sz size()
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define bc back()
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const long long inf=1e18+7;
const ll mod=998244353;
const ll N=1e5+7;
const ld eps=1e-9;
vector <vector <pair<ll,ll>> > g(N);
ll pref[N],mn[N];
bool mp[N];
ll p[N][30],up[N][30];
ll d[N],tin[N],tout[N],tiktak;
void calc(ll v){
for(ll i=2;i<20;i++){
p[v][i]=p[p[v][i-1]][i-1];
up[v][i]=min(up[v][i-1],up[p[v][i-1]][i-1]+pref[v]-pref[p[v][i-1]]);
}
}
void prep(ll v,ll pr){
if(pr>0)calc(v);
for(auto i : g[v]){
if(i.fr==pr) continue;
p[i.fr][1]=v;
up[i.fr][1]=min(mn[i.fr],i.sc+mn[v]);
prep(i.fr,v);
}
}
void dfs(ll v,ll p){
mn[v]=inf;
tin[v]=tiktak++;
if(mp[v]) mn[v]=0;
for(auto i : g[v]){
if(i.fr==p) continue;
d[i.fr]=d[v]+1;
pref[i.fr]=pref[v]+i.sc;
dfs(i.fr,v);
mn[v]=min(mn[v],mn[i.fr]+i.sc);
}
tout[v]=tiktak++;
}
void solve(){
ll i,j,q;
ll a,b,c=0;
ll n,r,k;
cin>>n>>k>>q>>r;
vector <pair<ll,ll>> edges;
for(i=1;i<n;i++){
cin>>a>>b>>c;
edges.pb({a,b});
g[a].pb({b,c});
g[b].pb({a,c});
}
for(i=0;i<k;i++){
cin>>b;
mp[b]=true;
}
for(i=0;i<25;i++){
for(j=1;j<=n;j++){
up[j][i]=inf;
}
}
dfs(r,-1);
prep(r,-1);
for(j=0;j<q;j++){
cin>>a>>b;
if(d[edges[a-1].fr]<d[edges[a-1].sc]){
a=edges[a-1].sc;
}else{
a=edges[a-1].fr;
}
if(tin[a]>tin[b] || tout[a]<tout[b]){
cout<<"escaped"<<endl;
}
else{
ll sum=0,ans=inf,v=b;
for(i=19;i>0;i--){
if(d[v]-(1ll<<(i-1))>=d[a]){
ans=min(ans,up[v][i]+sum);
sum+=pref[v]-pref[p[v][i]];
v=up[v][i];
}
}
if(ans==inf){
cout<<"oo"<<endl;
}else{
cout<<ans<<endl;
}
}
}
}
signed main(){
start();
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ll t=1;
//cin>>t;
while(t--) solve();
}
/*
*/
Compilation message (stderr)
valley.cpp: In function 'void fre(std::string)':
valley.cpp:24:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:24:64: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |