#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 src){
queue<int> Q; int D[n + 1], ret = 0;
fill(D, D + n + 1, -1); D[src] = 0; Q.push(src);
while (!Q.empty()){
int i = Q.front(); Q.pop();
for (int j : adj[i]) if (D[j] == -1 and dA[j] < dA[i] and dB[j] < dB[i]){
D[j] = D[i] + 1; ret = max(ret, D[i]);
Q.push(j);
}
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
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 - 1 : wA;
else ans = min(wA, wB);
cout<<max(ans, -1)<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
2 ms |
5024 KB |
Output is correct |
8 |
Correct |
3 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 |
3 ms |
5020 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 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 |
4952 KB |
Output is correct |
22 |
Correct |
3 ms |
4964 KB |
Output is correct |
23 |
Correct |
3 ms |
4940 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
7244 KB |
Output is correct |
2 |
Correct |
22 ms |
8196 KB |
Output is correct |
3 |
Correct |
18 ms |
7372 KB |
Output is correct |
4 |
Correct |
22 ms |
8268 KB |
Output is correct |
5 |
Correct |
17 ms |
6988 KB |
Output is correct |
6 |
Correct |
17 ms |
7244 KB |
Output is correct |
7 |
Correct |
20 ms |
7620 KB |
Output is correct |
8 |
Correct |
19 ms |
7628 KB |
Output is correct |
9 |
Correct |
35 ms |
8408 KB |
Output is correct |
10 |
Correct |
106 ms |
14368 KB |
Output is correct |
11 |
Correct |
36 ms |
9936 KB |
Output is correct |
12 |
Correct |
50 ms |
11244 KB |
Output is correct |
13 |
Correct |
45 ms |
10388 KB |
Output is correct |
14 |
Correct |
32 ms |
9460 KB |
Output is correct |
15 |
Correct |
46 ms |
11380 KB |
Output is correct |
16 |
Correct |
63 ms |
11184 KB |
Output is correct |
17 |
Correct |
58 ms |
10900 KB |
Output is correct |
18 |
Correct |
44 ms |
10956 KB |
Output is correct |
19 |
Correct |
71 ms |
13348 KB |
Output is correct |
20 |
Correct |
133 ms |
16096 KB |
Output is correct |
21 |
Correct |
67 ms |
13796 KB |
Output is correct |
22 |
Correct |
65 ms |
12628 KB |
Output is correct |
23 |
Correct |
66 ms |
13664 KB |
Output is correct |
24 |
Correct |
64 ms |
13684 KB |
Output is correct |
25 |
Correct |
66 ms |
13208 KB |
Output is correct |
26 |
Correct |
60 ms |
12712 KB |
Output is correct |
27 |
Correct |
72 ms |
13764 KB |
Output is correct |
28 |
Correct |
85 ms |
13812 KB |
Output is correct |
29 |
Correct |
106 ms |
14876 KB |
Output is correct |
30 |
Correct |
143 ms |
16836 KB |
Output is correct |
31 |
Correct |
88 ms |
15232 KB |
Output is correct |
32 |
Correct |
71 ms |
13732 KB |
Output is correct |
33 |
Correct |
71 ms |
14020 KB |
Output is correct |
34 |
Correct |
83 ms |
14496 KB |
Output is correct |
35 |
Correct |
77 ms |
14192 KB |
Output is correct |
36 |
Correct |
71 ms |
14500 KB |
Output is correct |
37 |
Correct |
88 ms |
14964 KB |
Output is correct |
38 |
Correct |
89 ms |
15456 KB |
Output is correct |
39 |
Correct |
94 ms |
15536 KB |
Output is correct |
40 |
Correct |
140 ms |
17172 KB |
Output is correct |
41 |
Correct |
187 ms |
19344 KB |
Output is correct |