이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
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++)
{
//if(u==3) cout <<cc[i].fi<<'\n';
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==inf) 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();
}
컴파일 시 표준 에러 (stderr) 메시지
valley.cpp: In function 'void dfs(ll, ll)':
valley.cpp:29:35: warning: iteration 16 invokes undefined behavior [-Waggressive-loop-optimizations]
29 | for(int i=1;i<=17;i++) P[u][i]=P[P[u][i-1]][i-1];
| ~~~~~~~^~~~~~~~~~~~~~~~~~
valley.cpp:29:18: note: within this loop
29 | 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... |