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 pb push_back
#define fi first
#define se second
#define ll long long
const int MAX=1e5+10, L=19;
const ll MAXV=1e16;
int n, s, q, e, a[MAX], b[MAX], w;
bool sk[MAX];
vector<pair<int,int>>g[MAX];
int czas, pre[MAX], maxiPre[MAX];
ll naj[MAX], gle[MAX], gle2[MAX];
pair<int,ll>jump[MAX][L];
pair<int,int> o[MAX];
void dfs(int v, int oj){
pre[v]=++czas;
naj[v]=MAXV;
if(sk[v])
naj[v]=0;
for(auto s:g[v]){
if(s.fi!=oj){
gle[s.fi]=gle[v]+1;
gle2[s.fi]=gle2[v]+s.se;
o[s.fi]={v, s.se};
dfs(s.fi, v);
naj[v]=min(naj[v], naj[s.fi]+(ll)s.se);
}
}
maxiPre[v]=czas;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>n>>s>>q>>e;
for(int i=1; i<=n-1; i++){
cin>>a[i]>>b[i]>>w;
g[a[i]].pb({b[i], w}), g[b[i]].pb({a[i], w});
}
for(int i=1; i<=s; i++){
int c;
cin>>c;
sk[c]=true;
}
dfs(e, e);
for(int i=1; i<=n; i++)
jump[i][0]={o[i].fi, min(naj[i], o[i].se+naj[o[i].fi])};
for(int i=1; i<L; i++){
for(int j=1; j<=n; j++){
jump[j][i].fi=jump[jump[j][i-1].fi][i-1].fi;
jump[j][i].se=min(jump[j][i-1].se, jump[jump[j][i-1].fi][i-1].se+gle2[j]-gle2[jump[j][i-1].fi]);
}
}
while(q--){
int x, y;
cin>>x>>y;
int u1=a[x], u2=b[x];
if(pre[u1]>pre[u2])
swap(u1, u2);
if(pre[u2]>pre[y] || maxiPre[u2]<pre[y]){
cout<<"escaped\n";
continue;
}
if(naj[u2]==MAXV){
cout<<"oo\n";
continue;
}
int doU2=gle[y]-gle[u2];
//cout<<doU2<<"\n";
int y1=y, licz=0;
ll odl=naj[y];
while(doU2){
if(doU2%2)
odl=min(odl, jump[y1][licz].se), y1=jump[y1][licz].fi;
doU2/=2, licz++;
}
cout<<odl<<"\n";
}
}
# | 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... |