#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 nearer(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 = (nearer(s) >= nearer(v) - wA) ? wA : wA - 1;
else ans = min(wA, wB);
cout<<max(ans, -1)<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5000 KB |
Output is correct |
4 |
Correct |
3 ms |
4996 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 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 |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5000 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4996 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
3 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
6596 KB |
Output is correct |
2 |
Correct |
53 ms |
7376 KB |
Output is correct |
3 |
Correct |
34 ms |
6768 KB |
Output is correct |
4 |
Correct |
46 ms |
7512 KB |
Output is correct |
5 |
Correct |
31 ms |
6548 KB |
Output is correct |
6 |
Correct |
33 ms |
6732 KB |
Output is correct |
7 |
Correct |
36 ms |
6940 KB |
Output is correct |
8 |
Correct |
38 ms |
6952 KB |
Output is correct |
9 |
Correct |
57 ms |
7364 KB |
Output is correct |
10 |
Correct |
279 ms |
11652 KB |
Output is correct |
11 |
Correct |
69 ms |
8572 KB |
Output is correct |
12 |
Correct |
93 ms |
9436 KB |
Output is correct |
13 |
Correct |
80 ms |
8832 KB |
Output is correct |
14 |
Correct |
64 ms |
8232 KB |
Output is correct |
15 |
Correct |
93 ms |
9592 KB |
Output is correct |
16 |
Correct |
93 ms |
9796 KB |
Output is correct |
17 |
Correct |
83 ms |
9280 KB |
Output is correct |
18 |
Correct |
91 ms |
9224 KB |
Output is correct |
19 |
Correct |
145 ms |
10472 KB |
Output is correct |
20 |
Correct |
311 ms |
13428 KB |
Output is correct |
21 |
Correct |
136 ms |
11240 KB |
Output is correct |
22 |
Correct |
115 ms |
10412 KB |
Output is correct |
23 |
Correct |
127 ms |
11140 KB |
Output is correct |
24 |
Correct |
137 ms |
11304 KB |
Output is correct |
25 |
Correct |
120 ms |
10836 KB |
Output is correct |
26 |
Correct |
111 ms |
10560 KB |
Output is correct |
27 |
Correct |
147 ms |
11204 KB |
Output is correct |
28 |
Correct |
141 ms |
11292 KB |
Output is correct |
29 |
Correct |
193 ms |
11948 KB |
Output is correct |
30 |
Correct |
321 ms |
13932 KB |
Output is correct |
31 |
Correct |
149 ms |
12220 KB |
Output is correct |
32 |
Correct |
144 ms |
11376 KB |
Output is correct |
33 |
Correct |
140 ms |
11440 KB |
Output is correct |
34 |
Correct |
146 ms |
11760 KB |
Output is correct |
35 |
Correct |
155 ms |
11420 KB |
Output is correct |
36 |
Correct |
146 ms |
11852 KB |
Output is correct |
37 |
Correct |
160 ms |
12636 KB |
Output is correct |
38 |
Correct |
176 ms |
12496 KB |
Output is correct |
39 |
Correct |
198 ms |
12484 KB |
Output is correct |
40 |
Correct |
293 ms |
14012 KB |
Output is correct |
41 |
Correct |
407 ms |
16208 KB |
Output is correct |