#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)2e5 + 10;
vector<int> T[N];
int c = 0;
int d[4][N];
int n;
void bfs(int node){
for(int i = 1; i <= n; i ++ )
d[c][i] = (int)1e9;
queue<int> bf;
bf.push(node);
d[c][node] = 0;
while(!bf.empty()){
node = bf.front();
bf.pop();
for(auto x : T[node]){
if(d[c][x] > d[c][node] + 1){
d[c][x] = d[c][node] + 1;
bf.push(x);
}
}
}
c ++ ;
}
int main(){
fastIO;
int m;
cin >> n >> m;
int a, b;
cin >> a >> b;
int n0, n1;
cin >> n0 >> n1;
int u, v;
for(int i = 0 ; i < m ; i ++ ){
cin >> u >> v;
T[u].push_back(v);
T[v].push_back(u);
}
bfs(a);
bfs(b);
bfs(n0);
bfs(n1);
int d0 = d[1][n0] - d[0][n0];
int d1 = d[1][n1] - d[0][n1];
if(d0 != d1){
int low = min(d0,d1);
if(low < 0) cout << -1 << "\n";
else cout << low << "\n";
return 0;
}
int cc = 0, dd = 0;
for(int i = 1; i <= n; i ++ ){
if(d[0][i] + d[2][i] == d[0][n0] && d[0][i] + d[3][i] == d[0][n1]){
cc = max(cc, d[0][i]);
}
if(d[1][i] + d[2][i] == d[1][n0] && d[1][i] + d[3][i] == d[1][n1]){
dd = max(dd, d[1][i]);
}
}
if(d0 < 0){
cout << -1 << "\n";
return 0;
}
if(cc + d0 >= dd){
cout << d0 << "\n";
return 0;
}
cout << d0-1 << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
5096 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
3 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
3 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
7168 KB |
Output is correct |
2 |
Correct |
40 ms |
8056 KB |
Output is correct |
3 |
Correct |
28 ms |
7288 KB |
Output is correct |
4 |
Correct |
42 ms |
8056 KB |
Output is correct |
5 |
Correct |
29 ms |
7032 KB |
Output is correct |
6 |
Correct |
30 ms |
7296 KB |
Output is correct |
7 |
Correct |
39 ms |
7544 KB |
Output is correct |
8 |
Correct |
34 ms |
7544 KB |
Output is correct |
9 |
Correct |
47 ms |
7940 KB |
Output is correct |
10 |
Correct |
172 ms |
12664 KB |
Output is correct |
11 |
Correct |
61 ms |
9464 KB |
Output is correct |
12 |
Correct |
129 ms |
10940 KB |
Output is correct |
13 |
Correct |
84 ms |
9976 KB |
Output is correct |
14 |
Correct |
59 ms |
9080 KB |
Output is correct |
15 |
Correct |
92 ms |
11032 KB |
Output is correct |
16 |
Correct |
102 ms |
11340 KB |
Output is correct |
17 |
Correct |
96 ms |
10616 KB |
Output is correct |
18 |
Correct |
88 ms |
10616 KB |
Output is correct |
19 |
Correct |
153 ms |
11880 KB |
Output is correct |
20 |
Correct |
232 ms |
14788 KB |
Output is correct |
21 |
Correct |
139 ms |
13176 KB |
Output is correct |
22 |
Correct |
132 ms |
12152 KB |
Output is correct |
23 |
Correct |
153 ms |
13012 KB |
Output is correct |
24 |
Correct |
112 ms |
13004 KB |
Output is correct |
25 |
Correct |
130 ms |
12536 KB |
Output is correct |
26 |
Correct |
149 ms |
12256 KB |
Output is correct |
27 |
Correct |
186 ms |
13176 KB |
Output is correct |
28 |
Correct |
184 ms |
13072 KB |
Output is correct |
29 |
Correct |
209 ms |
13560 KB |
Output is correct |
30 |
Correct |
252 ms |
15468 KB |
Output is correct |
31 |
Correct |
174 ms |
14200 KB |
Output is correct |
32 |
Correct |
166 ms |
13048 KB |
Output is correct |
33 |
Correct |
148 ms |
13344 KB |
Output is correct |
34 |
Correct |
161 ms |
13688 KB |
Output is correct |
35 |
Correct |
145 ms |
13304 KB |
Output is correct |
36 |
Correct |
136 ms |
13688 KB |
Output is correct |
37 |
Correct |
184 ms |
14776 KB |
Output is correct |
38 |
Correct |
186 ms |
14456 KB |
Output is correct |
39 |
Correct |
206 ms |
14456 KB |
Output is correct |
40 |
Correct |
238 ms |
15992 KB |
Output is correct |
41 |
Correct |
371 ms |
18132 KB |
Output is correct |