#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
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);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
15 ms |
14284 KB |
Output is correct |
4 |
Correct |
15 ms |
14456 KB |
Output is correct |
5 |
Correct |
15 ms |
14456 KB |
Output is correct |
6 |
Correct |
15 ms |
14456 KB |
Output is correct |
7 |
Correct |
15 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
15 ms |
14456 KB |
Output is correct |
10 |
Correct |
15 ms |
14584 KB |
Output is correct |
11 |
Correct |
15 ms |
14456 KB |
Output is correct |
12 |
Correct |
15 ms |
14456 KB |
Output is correct |
13 |
Correct |
15 ms |
14456 KB |
Output is correct |
14 |
Correct |
15 ms |
14456 KB |
Output is correct |
15 |
Correct |
15 ms |
14456 KB |
Output is correct |
16 |
Correct |
16 ms |
14456 KB |
Output is correct |
17 |
Correct |
16 ms |
14584 KB |
Output is correct |
18 |
Correct |
15 ms |
14584 KB |
Output is correct |
19 |
Correct |
16 ms |
14456 KB |
Output is correct |
20 |
Correct |
16 ms |
14584 KB |
Output is correct |
21 |
Correct |
15 ms |
14588 KB |
Output is correct |
22 |
Correct |
15 ms |
14456 KB |
Output is correct |
23 |
Correct |
15 ms |
14584 KB |
Output is correct |
24 |
Correct |
16 ms |
14588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
19320 KB |
Output is correct |
2 |
Correct |
75 ms |
21880 KB |
Output is correct |
3 |
Correct |
59 ms |
19636 KB |
Output is correct |
4 |
Correct |
108 ms |
22248 KB |
Output is correct |
5 |
Correct |
53 ms |
18936 KB |
Output is correct |
6 |
Correct |
58 ms |
19576 KB |
Output is correct |
7 |
Correct |
69 ms |
20344 KB |
Output is correct |
8 |
Correct |
64 ms |
20344 KB |
Output is correct |
9 |
Correct |
90 ms |
21032 KB |
Output is correct |
10 |
Correct |
267 ms |
28948 KB |
Output is correct |
11 |
Correct |
126 ms |
25464 KB |
Output is correct |
12 |
Correct |
164 ms |
27784 KB |
Output is correct |
13 |
Correct |
132 ms |
26024 KB |
Output is correct |
14 |
Correct |
105 ms |
23672 KB |
Output is correct |
15 |
Correct |
170 ms |
28152 KB |
Output is correct |
16 |
Correct |
168 ms |
27768 KB |
Output is correct |
17 |
Correct |
151 ms |
27384 KB |
Output is correct |
18 |
Correct |
156 ms |
27228 KB |
Output is correct |
19 |
Correct |
219 ms |
28128 KB |
Output is correct |
20 |
Correct |
363 ms |
32908 KB |
Output is correct |
21 |
Correct |
239 ms |
33808 KB |
Output is correct |
22 |
Correct |
214 ms |
30584 KB |
Output is correct |
23 |
Correct |
220 ms |
31644 KB |
Output is correct |
24 |
Correct |
259 ms |
33272 KB |
Output is correct |
25 |
Correct |
225 ms |
31608 KB |
Output is correct |
26 |
Correct |
191 ms |
29688 KB |
Output is correct |
27 |
Correct |
227 ms |
32572 KB |
Output is correct |
28 |
Correct |
235 ms |
32376 KB |
Output is correct |
29 |
Correct |
312 ms |
31096 KB |
Output is correct |
30 |
Correct |
452 ms |
34132 KB |
Output is correct |
31 |
Correct |
272 ms |
36276 KB |
Output is correct |
32 |
Correct |
236 ms |
32376 KB |
Output is correct |
33 |
Correct |
220 ms |
31736 KB |
Output is correct |
34 |
Correct |
270 ms |
33912 KB |
Output is correct |
35 |
Correct |
246 ms |
34040 KB |
Output is correct |
36 |
Correct |
249 ms |
34988 KB |
Output is correct |
37 |
Correct |
291 ms |
34672 KB |
Output is correct |
38 |
Correct |
281 ms |
35796 KB |
Output is correct |
39 |
Correct |
283 ms |
35448 KB |
Output is correct |
40 |
Correct |
393 ms |
35832 KB |
Output is correct |
41 |
Correct |
511 ms |
39084 KB |
Output is correct |