#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int dis[4][N];
vector <int> g[N];
void BFS(int v, int *dis) {
fill(dis, dis + N, N);
dis[v] = 0;
queue <int> q; q.push(v);
while (!q.empty()) {
v = q.front(), q.pop();
for (int u : g[v]) if (dis[v] + 1 < dis[u]) dis[u] = dis[v] + 1, q.push(u);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m; cin >> n >> m;
int t, k, a, b; cin >> k >> t >> a >> b;
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v), g[v].push_back(u);
}
BFS(t, dis[0]), BFS(k, dis[1]), BFS(a, dis[2]), BFS(b, dis[3]);
int ans1 = dis[0][a] - dis[1][a], ans2 = dis[0][b] - dis[1][b];
if (min(ans1, ans2) < 0) return cout << -1 << "\n", 0;
if (ans1 != ans2) return cout << min(ans1, ans2) << "\n", 0;
int mx0 = 0, mx1 = 0;
for (int i = 1; i <= n; i++)
if (dis[0][i] + dis[2][i] == dis[0][a] && dis[0][i] + dis[3][i] == dis[0][b]) mx0 = max(mx0, dis[0][i]);
for (int i = 1; i <= n; i++)
if (dis[1][i] + dis[2][i] == dis[1][a] && dis[1][i] + dis[3][i] == dis[1][b]) mx1 = max(mx1, dis[1][i]);
cout << ans1 - (ans1 + mx1 < mx0) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
10 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
10 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
9 ms |
8192 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
9 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8192 KB |
Output is correct |
10 |
Correct |
9 ms |
8192 KB |
Output is correct |
11 |
Correct |
10 ms |
8192 KB |
Output is correct |
12 |
Correct |
9 ms |
8192 KB |
Output is correct |
13 |
Correct |
9 ms |
8192 KB |
Output is correct |
14 |
Correct |
11 ms |
8192 KB |
Output is correct |
15 |
Correct |
9 ms |
8192 KB |
Output is correct |
16 |
Correct |
9 ms |
8192 KB |
Output is correct |
17 |
Correct |
9 ms |
8192 KB |
Output is correct |
18 |
Correct |
9 ms |
8192 KB |
Output is correct |
19 |
Correct |
9 ms |
8192 KB |
Output is correct |
20 |
Correct |
9 ms |
8192 KB |
Output is correct |
21 |
Correct |
10 ms |
8192 KB |
Output is correct |
22 |
Correct |
10 ms |
8192 KB |
Output is correct |
23 |
Correct |
9 ms |
8192 KB |
Output is correct |
24 |
Correct |
10 ms |
8192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
9852 KB |
Output is correct |
2 |
Correct |
41 ms |
10744 KB |
Output is correct |
3 |
Correct |
35 ms |
9976 KB |
Output is correct |
4 |
Correct |
44 ms |
10744 KB |
Output is correct |
5 |
Correct |
32 ms |
9856 KB |
Output is correct |
6 |
Correct |
32 ms |
10104 KB |
Output is correct |
7 |
Correct |
36 ms |
10236 KB |
Output is correct |
8 |
Correct |
39 ms |
10232 KB |
Output is correct |
9 |
Correct |
61 ms |
11128 KB |
Output is correct |
10 |
Correct |
172 ms |
19576 KB |
Output is correct |
11 |
Correct |
69 ms |
12024 KB |
Output is correct |
12 |
Correct |
92 ms |
13176 KB |
Output is correct |
13 |
Correct |
115 ms |
12452 KB |
Output is correct |
14 |
Correct |
62 ms |
11640 KB |
Output is correct |
15 |
Correct |
95 ms |
13176 KB |
Output is correct |
16 |
Correct |
100 ms |
13408 KB |
Output is correct |
17 |
Correct |
88 ms |
12920 KB |
Output is correct |
18 |
Correct |
88 ms |
12920 KB |
Output is correct |
19 |
Correct |
127 ms |
15224 KB |
Output is correct |
20 |
Correct |
218 ms |
20984 KB |
Output is correct |
21 |
Correct |
134 ms |
15224 KB |
Output is correct |
22 |
Correct |
119 ms |
14328 KB |
Output is correct |
23 |
Correct |
128 ms |
15100 KB |
Output is correct |
24 |
Correct |
115 ms |
14968 KB |
Output is correct |
25 |
Correct |
126 ms |
14712 KB |
Output is correct |
26 |
Correct |
108 ms |
14328 KB |
Output is correct |
27 |
Correct |
226 ms |
15096 KB |
Output is correct |
28 |
Correct |
155 ms |
15224 KB |
Output is correct |
29 |
Correct |
221 ms |
17400 KB |
Output is correct |
30 |
Correct |
230 ms |
21880 KB |
Output is correct |
31 |
Correct |
158 ms |
16248 KB |
Output is correct |
32 |
Correct |
149 ms |
15096 KB |
Output is correct |
33 |
Correct |
229 ms |
15480 KB |
Output is correct |
34 |
Correct |
155 ms |
15736 KB |
Output is correct |
35 |
Correct |
129 ms |
15352 KB |
Output is correct |
36 |
Correct |
139 ms |
15736 KB |
Output is correct |
37 |
Correct |
168 ms |
16760 KB |
Output is correct |
38 |
Correct |
180 ms |
16504 KB |
Output is correct |
39 |
Correct |
266 ms |
16632 KB |
Output is correct |
40 |
Correct |
224 ms |
20344 KB |
Output is correct |
41 |
Correct |
315 ms |
25336 KB |
Output is correct |