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 ll long long
const int INF=1e9;
const ll LINF=1e18;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define mid ((l+r)/2)
#define sz(a) (int((a).size()))
#define all(a) a.begin(),a.end()
#define endl "\n"
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
#define pb push_back
#define PRINTvec(niz) { cerr<<#niz<<"="; for(auto _i:niz) cerr<<_i<<" "; cerr<<endl; }
void Solve(){
int N,S,Q,E; cin>>N>>S>>Q>>E;
vector<pll> adj[N+1];
vector<pii> edges(N);
for(int i=1;i<=N-1;i++){
int u,v,w; cin>>u>>v>>w;
adj[u].pb({v,w});
adj[v].pb({u,w});
edges[i]={u,v};
}
vector<bool> spec(N+1,false);
for(int i=1;i<=S;i++){
int x; cin>>x; spec[x]=true;
}
struct Kveri{
int idx,I;
};
vector<ll> res(Q+1);
vector<Kveri> qu[N+1];
for(int i=1;i<=Q;i++){
int I,R; cin>>I>>R;
qu[R].pb({i,I});
}
vector<ll> dub_root(N+1),dp(N+1),seg(4*(N+1),LINF);
vector<int> dub(N+1);
function<void(int,int,ll)> Dfs1=[&](int node,int prev,ll w){
dub_root[node]=(node!=E ? dub_root[prev]+w : 0);
dub[node]=(node!=E ? dub[prev]+1 : 1);
dp[node]=(spec[node] ? 0 : LINF);
for(pll p:adj[node]){
if(p.fi!=prev) Dfs1(p.fi,node,p.se);
if(p.fi!=prev) dp[node]=min(dp[node],dp[p.fi]+p.se);
}
}; Dfs1(E,0,0);
function<void(int,int,int,int,ll)> Update=[&](int node,int l,int r,int idx,ll val){
if(l==r){
seg[node]=val;
return;
}
if(idx<=mid) Update(2*node,l,mid,idx,val);
else Update(2*node+1,mid+1,r,idx,val);
seg[node]=min(seg[2*node],seg[2*node+1]);
};
function<ll(int,int,int,int,int)> Query=[&](int node,int l,int r,int L,int R){
if(L>R) return LINF;
if(L<=l && r<=R) return seg[node];
ll ret=LINF;
if(L<=mid) ret=min(ret,Query(2*node,l,mid,L,R));
if(R>mid) ret=min(ret,Query(2*node+1,mid+1,r,L,R));
return ret;
};
set<int> parents;
function<void(int,int)> Dfs2=[&](int node,int prev){
parents.insert(node);
for(Kveri q:qu[node]){
int u=edges[q.I].fi,v=edges[q.I].se;
int idx=q.idx;
if(dub[u]<dub[v]) swap(u,v);
if(!parents.count(u)) res[idx]=-2;
else{
int L=dub[u],R=dub[node];
res[idx]=min(Query(1,1,N,L,R)+dub_root[node],dp[node]);
res[idx]=(res[idx]<LINF ? res[idx] : -1);
}
}
Update(1,1,N,dub[node],dp[node]-dub_root[node]);
for(pll p:adj[node]){
if(p.fi!=prev) Dfs2(p.fi,node);
}
Update(1,1,N,dub[node],LINF);
parents.erase(parents.find(node));
}; Dfs2(E,0);
for(int i=1;i<=Q;i++){
if(res[i]==-2) cout<<"escaped"<<endl;
else if(res[i]==-1) cout<<"oo"<<endl;
else cout<<res[i]<<endl;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
//cin>>t;
t=1;
while(t--){
Solve();
}
return 0;
}
# | 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... |