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;
using I=int;
using B=bool;
using Lli=long long int;
const I N=100000;
const I LOGN=17;
const I S=N;
const I W=1e9;
const Lli MAX=1e18;
vector<pair<I,I>>adjs[N];
I w_arr[N-1];
I c_arr[S];
Lli lows[N];
Lli diss[N];
I deps[N];
pair<I,Lli>ancs[N][LOGN];
I bots[N-1];
I tbgs[N],teds[N];
I t=0;
void dfs(I a,I p,Lli dis=0,I dep=0){
tbgs[a]=t++;
diss[a]=dis,deps[a]=dep;
for(auto[b,i]:adjs[a])if(b!=p){
I w=w_arr[i];
bots[i]=b;
dfs(b,a,dis+w,dep+1);
lows[a]=min(lows[a],lows[b]+w);
}
for(auto[b,w]:adjs[a])if(b!=p)ancs[b][0]={a,dis-lows[a]};
teds[a]=t;
}
Lli qry(I i,I j){
Lli res=diss[i]-lows[i];
for(I k=0;k<LOGN;k++)if(j>>k&1){
auto[l,d]=ancs[i][k];
i=l,res=max(res,d);
}
return res;
}
B anc(I a,I b){
return tbgs[a]<=tbgs[b]&&teds[a]>=teds[b];
}
I main(){
cin.tie(0)->sync_with_stdio(0);
I n,s,q,e;cin>>n>>s>>q>>e,e--;
for(I i=0;i<n-1;i++){
I a,b,w;cin>>a>>b>>w,a--,b--;
adjs[a].push_back({b,i});
adjs[b].push_back({a,i});
w_arr[i]=w;
}
for(I i=0;i<s;i++){I c;cin>>c,c_arr[i]=c-1;}
fill_n(lows,n,MAX);
for(I i=0;i<s;i++)lows[c_arr[i]]=0;
dfs(e,e);
ancs[e][0]={e,MAX};
for(I i=1;i<LOGN;i++)for(I j=0;j<n;j++){
auto[k,d1]=ancs[j][i-1];auto[l,d2]=ancs[k][i-1];
ancs[j][i]={l,max(d1,d2)};
}
while(q--){
I i,r;cin>>i>>r,i--,r--;
I j=bots[i];
if(!anc(j,r)){printf("escaped\n");continue;}
Lli d=diss[r]-qry(r,deps[r]-deps[j]);
d>(Lli)N*W?printf("oo"):printf("%lli\n",d);
}
}
# | 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... |