# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
43412 | Hassoony | 007 (CEOI14_007) | C++14 | 376 ms | 17308 KiB |
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;
typedef long long ll;
const int MX=2e5+9;
int n,m,a,b,s,d,dis[MX],dis1[MX],x,y,vis[MX];
vector<int>v[MX];
void bfs(int x,int y){
for(int i=1;i<=n;i++)dis[i]=MX,vis[i]=0;
dis[x]=0;
queue<int>q;
q.push(x);
while(!q.empty()){
int node=q.front();q.pop();
if(vis[node])continue;
vis[node]=1;
for(auto pp:v[node]){
if(pp==y)continue;
if(dis[pp]==MX){
dis[pp]=dis[node]+1;
q.push(pp);
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
scanf("%d%d%d%d",&s,&d,&a,&b);
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
bfs(s,d);
for(int i=1;i<=n;i++)dis1[i]=dis[i];
bfs(d,s);
if(dis[a]<dis1[a]||dis[b]<dis1[b]){
puts("-1");
return 0;
}
bfs(s,0);
for(int i=1;i<=n;i++)dis1[i]=dis[i];
bfs(d,0);
if(dis[a]<dis[b]){
cout<<dis[a]-dis1[a]<<endl;
}
else cout<<dis[b]-dis1[b]<<endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |