#include <bits/stdc++.h>
using namespace std ;
const int inf = 1e9 ;
const int MAX = 2e5 + 10 ;
int arr[MAX] ;
int n , m ;
int a , b , x , y ;
vector< vector<int> >adj(MAX) ;
int dist[4][MAX] ;
void bfs(int src , int t)
{
for(int i = 1 ; i <= n ; ++i)
dist[t][i] = inf ;
queue<int>q ;
dist[t][src] = 0 ;
q.push(src) ;
while(!q.empty())
{
int node = q.front() ;
q.pop() ;
for(auto &child : adj[node])
{
if(dist[t][node]+1 < dist[t][child])
{
dist[t][child] = dist[t][node]+1 ;
q.push(child) ;
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
cin>>a>>b>>x>>y ;
for(int i = 0 ; i < m ; ++i)
{
int u , v ;
cin>>u>>v ;
adj[u].push_back(v) ;
adj[v].push_back(u) ;
}
bfs(a , 0) ;
bfs(b , 1) ;
bfs(x , 2) ;
bfs(y , 3) ;
if(dist[1][x] < dist[0][x] || dist[1][y] < dist[0][y])
return cout<<-1<<"\n" , 0 ;
if(dist[1][x] - dist[0][x] != dist[1][y] - dist[0][y])
return cout<<min(dist[1][x] - dist[0][x] , dist[1][y] - dist[0][y]) , 0 ;
int Min1 = inf , Min2 = inf ;
for(int i = 1 ; i <= n ; ++i)
{
if(dist[0][i] + dist[2][i] == dist[0][x] && dist[0][i] + dist[3][i] == dist[0][y])
Min1 = min(Min1 , dist[2][i]) ;
if(dist[1][i] + dist[2][i] == dist[1][x] && dist[1][i] + dist[3][i] == dist[1][y])
Min2 = min(Min2 , dist[2][i]) ;
}
if(Min1 <= Min2)
cout<<dist[1][x] - dist[0][x]<<"\n" ;
else
cout<<dist[1][x] - dist[0][x] - 1<<"\n" ;
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4996 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6856 KB |
Output is correct |
2 |
Correct |
33 ms |
7716 KB |
Output is correct |
3 |
Correct |
27 ms |
6980 KB |
Output is correct |
4 |
Correct |
42 ms |
7876 KB |
Output is correct |
5 |
Correct |
25 ms |
6840 KB |
Output is correct |
6 |
Correct |
25 ms |
7108 KB |
Output is correct |
7 |
Correct |
33 ms |
7304 KB |
Output is correct |
8 |
Correct |
33 ms |
7280 KB |
Output is correct |
9 |
Correct |
41 ms |
7748 KB |
Output is correct |
10 |
Correct |
163 ms |
12156 KB |
Output is correct |
11 |
Correct |
75 ms |
9248 KB |
Output is correct |
12 |
Correct |
80 ms |
10308 KB |
Output is correct |
13 |
Correct |
68 ms |
9484 KB |
Output is correct |
14 |
Correct |
52 ms |
8888 KB |
Output is correct |
15 |
Correct |
78 ms |
10440 KB |
Output is correct |
16 |
Correct |
90 ms |
10696 KB |
Output is correct |
17 |
Correct |
68 ms |
10028 KB |
Output is correct |
18 |
Correct |
73 ms |
10140 KB |
Output is correct |
19 |
Correct |
106 ms |
11256 KB |
Output is correct |
20 |
Correct |
203 ms |
14148 KB |
Output is correct |
21 |
Correct |
106 ms |
12460 KB |
Output is correct |
22 |
Correct |
99 ms |
11544 KB |
Output is correct |
23 |
Correct |
128 ms |
12380 KB |
Output is correct |
24 |
Correct |
108 ms |
12356 KB |
Output is correct |
25 |
Correct |
102 ms |
11908 KB |
Output is correct |
26 |
Correct |
100 ms |
11516 KB |
Output is correct |
27 |
Correct |
141 ms |
12432 KB |
Output is correct |
28 |
Correct |
127 ms |
12548 KB |
Output is correct |
29 |
Correct |
153 ms |
12912 KB |
Output is correct |
30 |
Correct |
207 ms |
14848 KB |
Output is correct |
31 |
Correct |
126 ms |
13668 KB |
Output is correct |
32 |
Correct |
124 ms |
12444 KB |
Output is correct |
33 |
Correct |
117 ms |
12628 KB |
Output is correct |
34 |
Correct |
174 ms |
13044 KB |
Output is correct |
35 |
Correct |
127 ms |
12744 KB |
Output is correct |
36 |
Correct |
119 ms |
13048 KB |
Output is correct |
37 |
Correct |
156 ms |
14148 KB |
Output is correct |
38 |
Correct |
188 ms |
13920 KB |
Output is correct |
39 |
Correct |
204 ms |
13888 KB |
Output is correct |
40 |
Correct |
267 ms |
15436 KB |
Output is correct |
41 |
Correct |
280 ms |
17568 KB |
Output is correct |