#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cassert>
#include <climits>
#include <fstream>
#include <stack>
#include <queue>
#include <map>
using namespace std;
const string FILE_NAME = "spy";
const string IN_FILE = FILE_NAME + ".inp";
const string OUT_FILE = FILE_NAME + ".out";
int readInt()
{
char ch = getchar(); int ans = 0, sgn = 1;
while (ch == ' ' || ch == '\n') ch = getchar();
if (ch == '-') sgn = -1, ch = getchar();
while (47 < ch && ch < 58) ans = ans * 10 + ch - 48, ch = getchar();
return ans * sgn;
}
const int INF = 1e8;
const int MAX = 200010;
int n, m;
int s, d, a, b;
vector< vector<int> > dist(3, vector<int>(MAX, INF));
vector<int> deci(MAX, INF);
vector<int> gr[MAX];
int32_t main()
{
// freopen(IN_FILE.c_str(), "r", stdin);
// freopen(OUT_FILE.c_str(), "w", stdout);
n = readInt(); m = readInt();
s = readInt(); d = readInt(); a = readInt(); b = readInt();
for (int i = 0; i < m; ++i) {
int u = readInt(), v = readInt();
gr[u].push_back(v);
gr[v].push_back(u);
}
dist[0][a] = 0;
queue<int> qu; qu.push(a);
while (!qu.empty()) {
int u = qu.front(); qu.pop();
for (int v : gr[u]) {
if (dist[0][v] == INF) {
dist[0][v] = dist[0][u] + 1;
qu.push(v);
}
}
}
dist[1][b] = 0;
assert(qu.empty());
qu.push(b);
while (!qu.empty()) {
int u = qu.front(); qu.pop();
for (int v : gr[u]) {
if (dist[1][v] == INF) {
dist[1][v] = dist[1][u] + 1;
qu.push(v);
}
}
deci[u] = min(dist[0][u], dist[1][u]);
for (int v : gr[u]) {
if (dist[0][v] + 1 == dist[0][u] && dist[1][v] + 1 == dist[1][u]) {
deci[u] = min(deci[u], deci[v]);
}
}
}
dist[2][d] = 0;
assert(qu.empty());
qu.push(d);
while (!qu.empty()) {
int u = qu.front(); qu.pop();
for (int v : gr[u]) {
if (dist[2][v] == INF) {
dist[2][v] = dist[2][u] + 1;
qu.push(v);
}
}
}
int ans = INF;
for (int u = 1; u <= n; ++u) {
bool block;
if (dist[0][s] > dist[0][u]) block = 0;
else if (dist[0][s] < dist[0][u]) block = 1;
else if (dist[1][s] > dist[1][u]) block = 0;
else if (dist[1][s] < dist[1][u]) block = 1;
else if (deci[s] <= deci[u]) block = 1;
else block = 0;
if (u == d && (!block)) {
cout << -1;
return 0;
}
if (!block) {
ans = min(ans, dist[2][u] - 1);
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8168 KB |
Output is correct |
2 |
Correct |
9 ms |
8296 KB |
Output is correct |
3 |
Correct |
9 ms |
8296 KB |
Output is correct |
4 |
Correct |
9 ms |
8296 KB |
Output is correct |
5 |
Correct |
9 ms |
8296 KB |
Output is correct |
6 |
Correct |
9 ms |
8304 KB |
Output is correct |
7 |
Correct |
9 ms |
8316 KB |
Output is correct |
8 |
Correct |
10 ms |
8340 KB |
Output is correct |
9 |
Correct |
9 ms |
8368 KB |
Output is correct |
10 |
Correct |
9 ms |
8540 KB |
Output is correct |
11 |
Correct |
9 ms |
8540 KB |
Output is correct |
12 |
Correct |
9 ms |
8540 KB |
Output is correct |
13 |
Correct |
9 ms |
8540 KB |
Output is correct |
14 |
Correct |
9 ms |
8540 KB |
Output is correct |
15 |
Correct |
9 ms |
8540 KB |
Output is correct |
16 |
Correct |
9 ms |
8540 KB |
Output is correct |
17 |
Correct |
9 ms |
8540 KB |
Output is correct |
18 |
Correct |
10 ms |
8540 KB |
Output is correct |
19 |
Correct |
9 ms |
8540 KB |
Output is correct |
20 |
Correct |
10 ms |
8540 KB |
Output is correct |
21 |
Incorrect |
9 ms |
8540 KB |
Output isn't correct |
22 |
Correct |
11 ms |
8540 KB |
Output is correct |
23 |
Correct |
9 ms |
8540 KB |
Output is correct |
24 |
Correct |
9 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
9704 KB |
Output is correct |
2 |
Correct |
31 ms |
10220 KB |
Output is correct |
3 |
Correct |
34 ms |
10220 KB |
Output is correct |
4 |
Correct |
33 ms |
10312 KB |
Output is correct |
5 |
Correct |
31 ms |
10312 KB |
Output is correct |
6 |
Correct |
26 ms |
10312 KB |
Output is correct |
7 |
Correct |
28 ms |
10312 KB |
Output is correct |
8 |
Correct |
43 ms |
10312 KB |
Output is correct |
9 |
Correct |
44 ms |
10344 KB |
Output is correct |
10 |
Correct |
135 ms |
14568 KB |
Output is correct |
11 |
Correct |
57 ms |
14568 KB |
Output is correct |
12 |
Correct |
71 ms |
14568 KB |
Output is correct |
13 |
Correct |
62 ms |
14568 KB |
Output is correct |
14 |
Correct |
48 ms |
14568 KB |
Output is correct |
15 |
Correct |
63 ms |
14568 KB |
Output is correct |
16 |
Correct |
70 ms |
14568 KB |
Output is correct |
17 |
Correct |
62 ms |
14568 KB |
Output is correct |
18 |
Correct |
75 ms |
14568 KB |
Output is correct |
19 |
Correct |
97 ms |
14568 KB |
Output is correct |
20 |
Correct |
197 ms |
15980 KB |
Output is correct |
21 |
Correct |
103 ms |
15980 KB |
Output is correct |
22 |
Correct |
103 ms |
15980 KB |
Output is correct |
23 |
Correct |
93 ms |
15980 KB |
Output is correct |
24 |
Correct |
93 ms |
15980 KB |
Output is correct |
25 |
Correct |
107 ms |
15980 KB |
Output is correct |
26 |
Correct |
78 ms |
15980 KB |
Output is correct |
27 |
Correct |
96 ms |
15980 KB |
Output is correct |
28 |
Correct |
141 ms |
15980 KB |
Output is correct |
29 |
Correct |
157 ms |
15980 KB |
Output is correct |
30 |
Correct |
207 ms |
16232 KB |
Output is correct |
31 |
Correct |
117 ms |
16232 KB |
Output is correct |
32 |
Correct |
114 ms |
16232 KB |
Output is correct |
33 |
Correct |
131 ms |
16232 KB |
Output is correct |
34 |
Correct |
141 ms |
16232 KB |
Output is correct |
35 |
Correct |
101 ms |
16232 KB |
Output is correct |
36 |
Correct |
126 ms |
16232 KB |
Output is correct |
37 |
Correct |
118 ms |
16232 KB |
Output is correct |
38 |
Correct |
129 ms |
16232 KB |
Output is correct |
39 |
Correct |
162 ms |
16232 KB |
Output is correct |
40 |
Correct |
193 ms |
16232 KB |
Output is correct |
41 |
Correct |
286 ms |
18268 KB |
Output is correct |