#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, m, s, d, a, b;
vector<int> g[maxn], ds(maxn, -1), dd(maxn, -1), da(maxn, -1), db(maxn, -1);
void bfs(int st, vector<int> &d)
{
queue<int> q;
d[st] = 0; q.push(st);
while (!q.empty())
{
int u = q.front(); q.pop();
for (int v : g[u]) if (d[v] == -1) d[v] = d[u]+1, q.push(v);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> s >> d >> a >> b;
s--, d--, a--, b--;
for (int i = 0, u, v; i < m; i++) cin >> u >> v, g[--u].push_back(--v), g[v].push_back(u);
bfs(s, ds), bfs(d, dd), bfs(a, da), bfs(b, db);
int wa = dd[a] - ds[a], wb = dd[b] - ds[b];
int ans;
if (wa < wb) ans = wa;
else if (wa > wb) ans = wb;
else
{
int s2 = 0, d2 = 0;
for (int i = 0; i < n; i++)
if (ds[i]+da[i] == ds[a] && ds[i]+db[i] == ds[b])
s2 = max(s2, ds[i]);
for (int i = 0; i < n; i++)
if (dd[i]+da[i] == dd[a] && dd[i]+db[i] == dd[b])
d2 = max(d2, dd[i]);
if (s2+wa >= d2) ans = wa;
else ans = wa - 1;
}
ans = max(ans, -1);
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8140 KB |
Output is correct |
3 |
Correct |
5 ms |
8140 KB |
Output is correct |
4 |
Correct |
5 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
5 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
6 ms |
8140 KB |
Output is correct |
10 |
Correct |
5 ms |
8140 KB |
Output is correct |
11 |
Correct |
5 ms |
8140 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
5 ms |
8140 KB |
Output is correct |
14 |
Correct |
5 ms |
8140 KB |
Output is correct |
15 |
Correct |
5 ms |
8140 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
5 ms |
8140 KB |
Output is correct |
18 |
Correct |
5 ms |
8140 KB |
Output is correct |
19 |
Correct |
5 ms |
8140 KB |
Output is correct |
20 |
Correct |
5 ms |
8140 KB |
Output is correct |
21 |
Correct |
5 ms |
8140 KB |
Output is correct |
22 |
Correct |
6 ms |
8140 KB |
Output is correct |
23 |
Correct |
6 ms |
8140 KB |
Output is correct |
24 |
Correct |
6 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
9412 KB |
Output is correct |
2 |
Correct |
34 ms |
9920 KB |
Output is correct |
3 |
Correct |
29 ms |
9456 KB |
Output is correct |
4 |
Correct |
35 ms |
10048 KB |
Output is correct |
5 |
Correct |
31 ms |
9308 KB |
Output is correct |
6 |
Correct |
29 ms |
9548 KB |
Output is correct |
7 |
Correct |
43 ms |
9636 KB |
Output is correct |
8 |
Correct |
32 ms |
9576 KB |
Output is correct |
9 |
Correct |
49 ms |
10044 KB |
Output is correct |
10 |
Correct |
160 ms |
14220 KB |
Output is correct |
11 |
Correct |
64 ms |
10948 KB |
Output is correct |
12 |
Correct |
106 ms |
11668 KB |
Output is correct |
13 |
Correct |
68 ms |
11076 KB |
Output is correct |
14 |
Correct |
52 ms |
10672 KB |
Output is correct |
15 |
Correct |
98 ms |
11780 KB |
Output is correct |
16 |
Correct |
88 ms |
11916 KB |
Output is correct |
17 |
Correct |
73 ms |
11560 KB |
Output is correct |
18 |
Correct |
81 ms |
11460 KB |
Output is correct |
19 |
Correct |
148 ms |
12644 KB |
Output is correct |
20 |
Correct |
215 ms |
15504 KB |
Output is correct |
21 |
Correct |
112 ms |
13124 KB |
Output is correct |
22 |
Correct |
108 ms |
12492 KB |
Output is correct |
23 |
Correct |
107 ms |
12996 KB |
Output is correct |
24 |
Correct |
110 ms |
12996 KB |
Output is correct |
25 |
Correct |
115 ms |
12720 KB |
Output is correct |
26 |
Correct |
98 ms |
12464 KB |
Output is correct |
27 |
Correct |
123 ms |
13124 KB |
Output is correct |
28 |
Correct |
155 ms |
13104 KB |
Output is correct |
29 |
Correct |
162 ms |
14024 KB |
Output is correct |
30 |
Correct |
215 ms |
15952 KB |
Output is correct |
31 |
Correct |
140 ms |
13920 KB |
Output is correct |
32 |
Correct |
140 ms |
13128 KB |
Output is correct |
33 |
Correct |
128 ms |
13252 KB |
Output is correct |
34 |
Correct |
142 ms |
13508 KB |
Output is correct |
35 |
Correct |
115 ms |
13240 KB |
Output is correct |
36 |
Correct |
132 ms |
13456 KB |
Output is correct |
37 |
Correct |
175 ms |
14148 KB |
Output is correct |
38 |
Correct |
155 ms |
14008 KB |
Output is correct |
39 |
Correct |
179 ms |
14112 KB |
Output is correct |
40 |
Correct |
218 ms |
15772 KB |
Output is correct |
41 |
Correct |
294 ms |
18024 KB |
Output is correct |