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 TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll dp[maxN],in[maxN],out[maxN],tg=0;
bool c[maxN];
ll P[maxN][17];
const ll inf2=1e15;
struct qq
{
ll fi,se,id;
};
vector<qq>g[maxN];
ll h[maxN],d[maxN];
ll val[maxN];
ll pos[maxN];
void dfs(ll u,ll p)
{
dp[u]=inf;
if(c[u]==1) dp[u]=0;
P[u][0]=p;
for(int i=1;i<=17;i++) P[u][i]=P[P[u][i-1]][i-1];
in[u]=++tg;
vector<pli>cc;
for(auto vv:g[u])
{
if(vv.fi!=p)
{
pos[vv.id]=vv.fi;
h[vv.fi]=h[u]+1;
d[vv.fi]=d[u]+vv.se;
dfs(vv.fi,u);
dp[u]=min(dp[u],dp[vv.fi]+vv.se);
cc.pb({dp[vv.fi]+vv.se,vv.fi});
}
}
sort(cc.begin(),cc.end());
if(c[u]==1)
{
for(auto vv:g[u])
{
if(vv.fi!=p)
{
val[vv.fi]=-d[u];
}
}
}
else
{
for(int i=0;i<min(2,(int)cc.size());i++)
{
for(auto vv:g[u])
{
if(vv.fi!=p&&vv.fi!=cc[i].se)
{
val[vv.fi]=min(val[vv.fi],-d[u]+cc[i].fi);
}
}
}
}
out[u]=tg;
}
ll mn[maxN][18];
void dfs2(ll u,ll p)
{
mn[u][0]=val[u];
for(int i=1;i<=17;i++) mn[u][i]=min(mn[u][i-1],mn[P[u][i-1]][i-1]);
for(auto vv:g[u])
{
if(vv.fi!=p)
{
dfs2(vv.fi,u);
}
}
}
ll get(ll u,ll p)
{
ll d=h[u]-h[p];
ll ans=inf;
for(int i=17;i>=0;i--)
{
if(d>>i&1)
{
ans=min(ans,mn[u][i]);
u=P[u][i];
}
}
return ans;
}
ll n,q,s,e;
bool ipar(ll u,ll p)
{
return (in[p]<=in[u]&&out[u]<=out[p]);
}
void solve()
{
cin >> n >> s >> q >> e;
for(int i=1;i<n;i++)
{
ll u,v,w;
cin >> u >> v >> w;
g[u].pb({v,w,i});
g[v].pb({u,w,i});
}
for(int i=1;i<=n;i++) val[i]=inf,c[i]=0;
for(int i=1;i<=s;i++)
{
ll u;
cin >> u;
c[u]=1;
}
dfs(e,e);
dfs2(e,e);
//cout << dp[4]<<'\n';
while(q--)
{
ll id,u;
cin >> id >> u;
ll v=pos[id];
if(ipar(u,v))
{
ll ans=inf;
ans=min(ans,dp[u]);
ans=min(ans,get(u,v)+d[u]);
if(ans>=inf2) cout <<"oo\n";
else cout << ans<<'\n';
}
else
{
cout <<"escaped\n";
}
}
}
int main()
{
fastio
freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
valley.cpp: In function 'int main()':
valley.cpp:146:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | freopen(TASKNAME".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp: In function 'void dfs(ll, ll)':
valley.cpp:30:35: warning: iteration 16 invokes undefined behavior [-Waggressive-loop-optimizations]
30 | for(int i=1;i<=17;i++) P[u][i]=P[P[u][i-1]][i-1];
| ~~~~~~~^~~~~~~~~~~~~~~~~~
valley.cpp:30:18: note: within this loop
30 | for(int i=1;i<=17;i++) P[u][i]=P[P[u][i-1]][i-1];
| ~^~~~
# | 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... |