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
const int N=200050;
vector<int> E[N];
void BFS(int x, int n, int d[], vector<int> p[])
{
for(int i=1;i<=n;i++) d[i]=-1;
queue<int> q;
q.push(x);
d[x]=0;
while(q.size())
{
int u=q.front();
q.pop();
for(int v:E[u])
{
if(d[v]==-1)
{
d[v]=d[u]+1;
q.push(v);
p[v].pb(u);
}
else if(d[v]==d[u]+1) p[v].pb(u);
}
}
}
int ds[N],dd[N];
vector<int> ps[N],pd[N];
void DFS(int u, int mark, int w[], vector<int> p[])
{
w[u]|=mark;
for(int v:p[u]) if((w[v]&mark)==0) DFS(v,mark,w,p);
}
int sw[N],dw[N];
bool Check(int a, int b, int n)
{
DFS(a,1,sw,ps);
DFS(b,2,sw,ps);
int mns=ds[a];
for(int i=1;i<=n;i++) if(sw[i]==3) mns=min(mns,ds[a]-ds[i]);
DFS(a,1,dw,pd);
DFS(b,2,dw,pd);
int mnd=dd[a];
for(int i=1;i<=n;i++) if(dw[i]==3) mnd=min(mnd,dd[a]-dd[i]);
//printf("%i %i\n",mns,mnd);
return mns>mnd;
}
int main()
{
int n,m,s,d,a,b;
scanf("%i %i",&n,&m);
scanf("%i %i %i %i",&s,&d,&a,&b);
for(int i=1,u,v;i<=m;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
BFS(s,n,ds,ps);
BFS(d,n,dd,pd);
//printf("%i %i %i %i\n",ds[a],ds[b],dd[a],dd[b]);
int ans=min(dd[a]-ds[a],dd[b]-ds[b]);
ans=max(ans,-1);
if(ans!=-1 && dd[a]==dd[b] && ds[a]==ds[b])
{
if(Check(a,b,n)) ans--;
}
printf("%i\n",ans);
return 0;
}
Compilation message (stderr)
007.cpp: In function 'int main()':
007.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&m);
~~~~~^~~~~~~~~~~~~~~
007.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i %i",&s,&d,&a,&b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:54:59: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1,u,v;i<=m;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |