#include <bits/stdc++.h>
using namespace std;
const int mx = 2e5 + 5;
int n, m, s, v, a, b, dA[mx], dB[mx], ans; vector<int> adj[mx];
void bfs(int st, int *D){
queue<int> Q; fill(D, D + n + 1, -1);
Q.push(st); D[st] = 0;
while (!Q.empty()){
int i = Q.front(); Q.pop();
for (int j : adj[i]) if (D[j] == -1) D[j] = D[i] + 1, Q.push(j);
}
}
int same(int pos){
int d = 0;
while (1){
bool ok = 0;
for (int i : adj[pos]) if (dA[i] < dA[pos] and dB[i] < dB[pos]){
pos = i; ok = 1; d++;
break;
}
if (!ok) break;
}
return d;
}
int main(){
cin >> n >> m >> s >> v >> a >> b;
for (int i = 1; i <= m; i++){
int a, b; cin >> a >> b;
adj[a].push_back(b); adj[b].push_back(a);
}
bfs(a, dA); bfs(b, dB);
int wA = dA[v] - dA[s], wB = dB[v] - dB[s];
if (wA == wB) ans = (same(s) + wA < same(v) ? wA - 1 : wA);
else ans = min(wA, wB);
cout<<max(ans, -1)<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 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 |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4904 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
3 ms |
4940 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
6592 KB |
Output is correct |
2 |
Correct |
41 ms |
7204 KB |
Output is correct |
3 |
Correct |
32 ms |
6648 KB |
Output is correct |
4 |
Correct |
43 ms |
7364 KB |
Output is correct |
5 |
Correct |
31 ms |
6472 KB |
Output is correct |
6 |
Correct |
34 ms |
6636 KB |
Output is correct |
7 |
Correct |
36 ms |
6900 KB |
Output is correct |
8 |
Correct |
37 ms |
6856 KB |
Output is correct |
9 |
Correct |
58 ms |
7236 KB |
Output is correct |
10 |
Correct |
254 ms |
11524 KB |
Output is correct |
11 |
Correct |
69 ms |
8436 KB |
Output is correct |
12 |
Correct |
93 ms |
9312 KB |
Output is correct |
13 |
Correct |
78 ms |
8780 KB |
Output is correct |
14 |
Correct |
60 ms |
8084 KB |
Output is correct |
15 |
Correct |
86 ms |
9428 KB |
Output is correct |
16 |
Correct |
98 ms |
9740 KB |
Output is correct |
17 |
Correct |
83 ms |
9216 KB |
Output is correct |
18 |
Correct |
99 ms |
9216 KB |
Output is correct |
19 |
Correct |
153 ms |
10424 KB |
Output is correct |
20 |
Correct |
287 ms |
13180 KB |
Output is correct |
21 |
Correct |
128 ms |
11276 KB |
Output is correct |
22 |
Correct |
119 ms |
10504 KB |
Output is correct |
23 |
Correct |
126 ms |
11060 KB |
Output is correct |
24 |
Correct |
121 ms |
11084 KB |
Output is correct |
25 |
Correct |
125 ms |
10788 KB |
Output is correct |
26 |
Correct |
122 ms |
10412 KB |
Output is correct |
27 |
Correct |
143 ms |
11140 KB |
Output is correct |
28 |
Correct |
132 ms |
11216 KB |
Output is correct |
29 |
Correct |
188 ms |
11844 KB |
Output is correct |
30 |
Correct |
317 ms |
13788 KB |
Output is correct |
31 |
Correct |
147 ms |
12156 KB |
Output is correct |
32 |
Correct |
133 ms |
11140 KB |
Output is correct |
33 |
Correct |
129 ms |
11332 KB |
Output is correct |
34 |
Correct |
145 ms |
11640 KB |
Output is correct |
35 |
Correct |
137 ms |
11512 KB |
Output is correct |
36 |
Correct |
138 ms |
11624 KB |
Output is correct |
37 |
Correct |
160 ms |
12532 KB |
Output is correct |
38 |
Correct |
172 ms |
12420 KB |
Output is correct |
39 |
Correct |
172 ms |
12484 KB |
Output is correct |
40 |
Correct |
275 ms |
13908 KB |
Output is correct |
41 |
Correct |
394 ms |
16196 KB |
Output is correct |