이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const long long maxn=1e5+10;
const long long logn=25;
const long long mod=1e9+7;
long long n,s,q,e;
vector<pair<long long,long long>>g[maxn];
set<long long>ins;
vector<pair<long long,long long>>pr;
long long up_node[maxn][logn];
long long dep[maxn];
struct node
{
long long idx;
long long dis;
node(){}
node(long long _idx,long long _dis)
{
idx=_idx;
dis=_dis;
}
bool operator <(const node &tmp)const
{
return dis>tmp.dis;
}
};
void dfs(long long node)
{
dep[node]=dep[up_node[node][0]]+1;
for(long long i=1;i<logn;i++)
{
up_node[node][i]=up_node[up_node[node][i-1]][i-1];
}
for(auto ax:g[node])
{
dfs(ax.first);
}
}
long long UP(long long node,long long k)
{
for(long long i=0;i<logn;i++)
{
if(k&(1<<i))
{
node=up_node[node][i];
}
}
return node;
}
long long LCA(long long a,long long b)
{
if(dep[a]<dep[b])
{
swap(a,b);
}
long long k=dep[a]-dep[b];
a=UP(a,k);
if(a==b)
{
return a;
}
for(long long i=logn-1;i>=0;i--)
{
if(up_node[a][i]!=up_node[b][i])
{
a=up_node[a][i];
b=up_node[b][i];
}
}
return up_node[a][0];
}
int main()
{
cin>>n>>s>>q>>e;
memset(dep,0,sizeof dep);
for(long long i=0;i<n-1;i++)
{
long long a,b,c;
cin>>a>>b>>c;
g[a].push_back({b,c});
g[b].push_back({a,c});
pr.push_back({a,b});
}
up_node[1][0]=0;
dfs(1);
for(long long i=0;i<s;i++)
{
long long pom;
cin>>pom;
ins.insert(pom);
}
while(q--)
{
long long idxe,st;
cin>>idxe>>st;
long long n1=pr[idxe].first;
long long n2=pr[idxe].second;
///cout<<n1<<" "<<n2<<endl;
if(n<1100)
{
///kolku e odalecen od S
priority_queue<node>Q;
Q.push(node(st,0));
long long d[n+1];
for(long long pi=1;pi<=n;pi++)
{
d[pi]=1e18;
}
d[st]=0;
bool ok=false;
while(!Q.empty())
{
node teme=Q.top();Q.pop();
if(teme.idx==e)
{
cout<<"escaped"<<endl;
ok=true;
break;
}
for(auto ax:g[teme.idx])
{
if((teme.idx==n1 && ax.first==n2) || (teme.idx==n2 && ax.first==n1))
{
continue;
}
if(teme.dis+ax.second<d[ax.first])
{
d[ax.first]=teme.dis+ax.second;
Q.push(node(ax.first,d[ax.first]));
}
}
}
long long ans=1e18;
for(auto pom:ins)
{
ans=min(ans, d[pom]);
}
if(!ok)
{
if(ans!=1e18)
cout<<ans<<endl;
else
{
cout<<"oo"<<endl;
}
}
}
else
{
if((LCA(n1,st)==n1 && LCA(n1,e)==n1) || LCA(n2,st)==n2 && LCA(n2,e)==n2)
{
cout<<"escaped"<<endl;
}
else
{
cout<<0<<endl;
}
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
valley.cpp: In function 'int main()':
valley.cpp:158:68: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
158 | if((LCA(n1,st)==n1 && LCA(n1,e)==n1) || LCA(n2,st)==n2 && LCA(n2,e)==n2)
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
# | 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... |