#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
const int len = 2e5+5, inf = 1e9;
int dis[4][len];
int n, m, s1, s2, t1, t2;
queue<int> myq;
vector<int> adj[len];
void bfs(int st, int t){
for (int i = 1; i <= n; i++)
dis[t][i] = -1;
dis[t][st] = 0, myq.push(st);
while (!myq.empty()){
int u = myq.front();
myq.pop();
for (int j = 0; j < adj[u].size(); j++){
int v = adj[u][j];
if (dis[t][v] == -1)
dis[t][v] = dis[t][u]+1, myq.push(v);
}
}
}
int main(){
scanf("%d %d %d %d %d %d", &n, &m, &s1, &s2, &t1, &t2);
for (int i = 0; i < m; i++){
int a, b;
scanf("%d %d", &a, &b);
adj[a].pb(b);
adj[b].pb(a);
}
int a1, a2, b1, b2, w1, w2, c = 0, d = 0;
bfs(t1, 0);
a1 = dis[0][s1], b1 = dis[0][s2], w1 = b1-a1;
bfs(t2, 1);
a2 = dis[1][s1], b2 = dis[1][s2], w2 = b2-a2;
if (w1 > w2)
swap(a1, a2), swap(b1, b2), swap(w1, w2);
bfs(s1, 2), bfs(s2, 3);
for (int i = 1; i <= n; i++){
if (dis[0][i] == dis[1][i] && dis[0][i]+dis[2][i] == dis[0][s1])
c = max(c, dis[2][i]);
if (dis[0][i] == dis[1][i] && dis[0][i]+dis[3][i] == dis[0][s2])
d = max(d, dis[3][i]);
}
if (b1 == b2 && a1 == a2 && c < d-w1)
printf("%d\n", max(w1-1, -1));
else
printf("%d\n", max(w1, -1));
return 0;
}
/*
9 10
9 8 1 2
1 2
2 3
3 1
2 4
4 5
5 2
5 7
7 6
7 8
7 9
6 6
1 2 3 4
1 5
5 6
6 3
6 4
1 2
3 4
6 7
5 6 1 2
6 3
1 2
1 3
2 3
1 5
2 4
5 4
*/
Compilation message
007.cpp: In function 'void bfs(int, int)':
007.cpp:26:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < adj[u].size(); j++){
~~^~~~~~~~~~~~~~~
007.cpp: In function 'int main()':
007.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d %d %d", &n, &m, &s1, &s2, &t1, &t2);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
7 ms |
5224 KB |
Output is correct |
3 |
Correct |
6 ms |
5224 KB |
Output is correct |
4 |
Correct |
7 ms |
5308 KB |
Output is correct |
5 |
Correct |
8 ms |
5356 KB |
Output is correct |
6 |
Correct |
5 ms |
5356 KB |
Output is correct |
7 |
Correct |
5 ms |
5356 KB |
Output is correct |
8 |
Correct |
5 ms |
5356 KB |
Output is correct |
9 |
Correct |
5 ms |
5356 KB |
Output is correct |
10 |
Correct |
6 ms |
5356 KB |
Output is correct |
11 |
Correct |
5 ms |
5356 KB |
Output is correct |
12 |
Correct |
6 ms |
5356 KB |
Output is correct |
13 |
Correct |
8 ms |
5356 KB |
Output is correct |
14 |
Correct |
5 ms |
5356 KB |
Output is correct |
15 |
Correct |
8 ms |
5356 KB |
Output is correct |
16 |
Correct |
8 ms |
5356 KB |
Output is correct |
17 |
Correct |
6 ms |
5356 KB |
Output is correct |
18 |
Correct |
6 ms |
5372 KB |
Output is correct |
19 |
Correct |
7 ms |
5372 KB |
Output is correct |
20 |
Correct |
9 ms |
5372 KB |
Output is correct |
21 |
Correct |
6 ms |
5372 KB |
Output is correct |
22 |
Correct |
10 ms |
5496 KB |
Output is correct |
23 |
Correct |
8 ms |
5496 KB |
Output is correct |
24 |
Correct |
9 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
7164 KB |
Output is correct |
2 |
Correct |
56 ms |
8060 KB |
Output is correct |
3 |
Correct |
45 ms |
8060 KB |
Output is correct |
4 |
Correct |
65 ms |
8316 KB |
Output is correct |
5 |
Correct |
41 ms |
8316 KB |
Output is correct |
6 |
Correct |
44 ms |
8316 KB |
Output is correct |
7 |
Correct |
51 ms |
8316 KB |
Output is correct |
8 |
Correct |
48 ms |
8316 KB |
Output is correct |
9 |
Correct |
73 ms |
8316 KB |
Output is correct |
10 |
Correct |
246 ms |
12400 KB |
Output is correct |
11 |
Correct |
94 ms |
12400 KB |
Output is correct |
12 |
Correct |
149 ms |
12400 KB |
Output is correct |
13 |
Correct |
106 ms |
12400 KB |
Output is correct |
14 |
Correct |
95 ms |
12400 KB |
Output is correct |
15 |
Correct |
171 ms |
12400 KB |
Output is correct |
16 |
Correct |
138 ms |
12400 KB |
Output is correct |
17 |
Correct |
125 ms |
12400 KB |
Output is correct |
18 |
Correct |
132 ms |
12400 KB |
Output is correct |
19 |
Correct |
200 ms |
12400 KB |
Output is correct |
20 |
Correct |
319 ms |
14460 KB |
Output is correct |
21 |
Correct |
247 ms |
14460 KB |
Output is correct |
22 |
Correct |
159 ms |
14460 KB |
Output is correct |
23 |
Correct |
173 ms |
14460 KB |
Output is correct |
24 |
Correct |
161 ms |
14460 KB |
Output is correct |
25 |
Correct |
232 ms |
14460 KB |
Output is correct |
26 |
Correct |
165 ms |
14460 KB |
Output is correct |
27 |
Correct |
179 ms |
14460 KB |
Output is correct |
28 |
Correct |
230 ms |
14460 KB |
Output is correct |
29 |
Correct |
248 ms |
14460 KB |
Output is correct |
30 |
Correct |
389 ms |
15268 KB |
Output is correct |
31 |
Correct |
219 ms |
15268 KB |
Output is correct |
32 |
Correct |
278 ms |
15268 KB |
Output is correct |
33 |
Correct |
204 ms |
15268 KB |
Output is correct |
34 |
Correct |
198 ms |
15268 KB |
Output is correct |
35 |
Correct |
115 ms |
15268 KB |
Output is correct |
36 |
Correct |
190 ms |
15268 KB |
Output is correct |
37 |
Correct |
147 ms |
15268 KB |
Output is correct |
38 |
Correct |
146 ms |
15268 KB |
Output is correct |
39 |
Correct |
192 ms |
15268 KB |
Output is correct |
40 |
Correct |
231 ms |
15832 KB |
Output is correct |
41 |
Correct |
356 ms |
18044 KB |
Output is correct |