#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 : 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 |
2 ms |
5024 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 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 |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
5024 KB |
Output is correct |
10 |
Correct |
3 ms |
5016 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 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 |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5020 KB |
Output is correct |
22 |
Correct |
2 ms |
5024 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 |
6732 KB |
Output is correct |
2 |
Correct |
24 ms |
7536 KB |
Output is correct |
3 |
Correct |
21 ms |
6940 KB |
Output is correct |
4 |
Correct |
22 ms |
7648 KB |
Output is correct |
5 |
Correct |
16 ms |
6584 KB |
Output is correct |
6 |
Correct |
17 ms |
6780 KB |
Output is correct |
7 |
Correct |
19 ms |
7140 KB |
Output is correct |
8 |
Correct |
19 ms |
7148 KB |
Output is correct |
9 |
Correct |
30 ms |
7492 KB |
Output is correct |
10 |
Correct |
107 ms |
11960 KB |
Output is correct |
11 |
Correct |
34 ms |
8900 KB |
Output is correct |
12 |
Correct |
48 ms |
9788 KB |
Output is correct |
13 |
Correct |
40 ms |
9196 KB |
Output is correct |
14 |
Correct |
32 ms |
8416 KB |
Output is correct |
15 |
Correct |
47 ms |
9884 KB |
Output is correct |
16 |
Correct |
49 ms |
9688 KB |
Output is correct |
17 |
Correct |
47 ms |
9656 KB |
Output is correct |
18 |
Correct |
45 ms |
9684 KB |
Output is correct |
19 |
Correct |
71 ms |
10820 KB |
Output is correct |
20 |
Correct |
143 ms |
13684 KB |
Output is correct |
21 |
Correct |
76 ms |
11904 KB |
Output is correct |
22 |
Correct |
70 ms |
10960 KB |
Output is correct |
23 |
Correct |
67 ms |
11748 KB |
Output is correct |
24 |
Correct |
72 ms |
11724 KB |
Output is correct |
25 |
Correct |
69 ms |
11380 KB |
Output is correct |
26 |
Correct |
59 ms |
11060 KB |
Output is correct |
27 |
Correct |
75 ms |
11804 KB |
Output is correct |
28 |
Correct |
82 ms |
11792 KB |
Output is correct |
29 |
Correct |
98 ms |
12388 KB |
Output is correct |
30 |
Correct |
146 ms |
14424 KB |
Output is correct |
31 |
Correct |
97 ms |
12900 KB |
Output is correct |
32 |
Correct |
75 ms |
11712 KB |
Output is correct |
33 |
Correct |
72 ms |
12000 KB |
Output is correct |
34 |
Correct |
85 ms |
12380 KB |
Output is correct |
35 |
Correct |
68 ms |
12032 KB |
Output is correct |
36 |
Correct |
72 ms |
12340 KB |
Output is correct |
37 |
Correct |
98 ms |
12748 KB |
Output is correct |
38 |
Correct |
89 ms |
13124 KB |
Output is correct |
39 |
Correct |
98 ms |
13160 KB |
Output is correct |
40 |
Correct |
136 ms |
14716 KB |
Output is correct |
41 |
Correct |
195 ms |
16808 KB |
Output is correct |