#include <bits/stdc++.h>
#define all(i) (i).begin(), (i).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
template<typename T1, typename T2>
ostream& operator << (ostream &i, pair<T1, T2> j) {
return i << j.first << ' ' << j.second;
}
template<typename T>
ostream& operator << (ostream &i, vector<T> j) {
i << '{' << j.size() << ':';
for (T ii : j) i << ' ' << ii;
return i << '}';
}
typedef long long ll;
typedef pair<int, int> pi;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, m, s, d, a, b;
cin >> n >> m >> s >> d >> a >> b;
--s, --d, --a, --b;
vector<int> g[n];
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y), g[y].push_back(x);
}
vector<int> disd(n, -1), diss(n, -1), disa(n, -1), disb(n, -1);
queue<int> q;
disd[d] = 0, q.push(d);
while (!q.empty()) {
int i = q.front();
q.pop();
for (int j : g[i]) if (disd[j] == -1)
disd[j] = disd[i] + 1, q.push(j);
}
diss[s] = 0, q.push(s);
while (!q.empty()) {
int i = q.front();
q.pop();
for (int j : g[i]) if (diss[j] == -1)
diss[j] = diss[i] + 1, q.push(j);
}
if (disd[a] != disd[b]) {
if (disd[a] < disd[b]) cout << max(disd[a] - diss[a], -1);
else cout << max(disd[b] - diss[b], -1);
}
else {
int tmp = max(disd[a] - 1 - diss[a], disd[b] - 1 - diss[b]);
vector<int> disa(n, -1), disb(n, -1);
disa[a] = 0, q.push(a);
while (!q.empty()) {
int i = q.front();
q.pop();
for (int j : g[i]) if (disa[j] == -1)
disa[j] = disa[i] + 1, q.push(j);
}
disb[b] = 0, q.push(b);
while (!q.empty()) {
int i = q.front();
q.pop();
for (int j : g[i]) if (disb[j] == -1)
disb[j] = disb[i] + 1, q.push(j);
}
int x = d;
for (int i = 0; i < n; ++i)
if (disa[i] == disb[i] && disd[i] + disa[i] == disd[a] && disa[i] < disa[x])
x = i;
for (int i = 0; i < n; ++i) if (disa[i] <= disa[x] && disb[i] <= disb[x])
tmp = max(tmp, disd[x] - diss[i]);
cout << max(tmp, -1);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
216 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Partially correct |
1 ms |
344 KB |
Partially correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
3472 KB |
Output is correct |
2 |
Correct |
33 ms |
4872 KB |
Output is correct |
3 |
Correct |
31 ms |
3656 KB |
Output is correct |
4 |
Correct |
36 ms |
5044 KB |
Output is correct |
5 |
Correct |
27 ms |
2980 KB |
Output is correct |
6 |
Correct |
29 ms |
3372 KB |
Output is correct |
7 |
Partially correct |
37 ms |
4040 KB |
Partially correct |
8 |
Partially correct |
27 ms |
4148 KB |
Partially correct |
9 |
Correct |
38 ms |
4552 KB |
Output is correct |
10 |
Correct |
142 ms |
9332 KB |
Output is correct |
11 |
Correct |
56 ms |
7368 KB |
Output is correct |
12 |
Correct |
85 ms |
9176 KB |
Output is correct |
13 |
Correct |
77 ms |
7888 KB |
Output is correct |
14 |
Correct |
51 ms |
6892 KB |
Output is correct |
15 |
Correct |
73 ms |
9356 KB |
Output is correct |
16 |
Correct |
62 ms |
8912 KB |
Output is correct |
17 |
Partially correct |
67 ms |
8768 KB |
Partially correct |
18 |
Correct |
77 ms |
8808 KB |
Output is correct |
19 |
Correct |
111 ms |
10032 KB |
Output is correct |
20 |
Correct |
201 ms |
13000 KB |
Output is correct |
21 |
Correct |
111 ms |
12868 KB |
Output is correct |
22 |
Correct |
106 ms |
11128 KB |
Output is correct |
23 |
Correct |
91 ms |
11532 KB |
Output is correct |
24 |
Correct |
105 ms |
12624 KB |
Output is correct |
25 |
Correct |
110 ms |
11880 KB |
Output is correct |
26 |
Correct |
75 ms |
10172 KB |
Output is correct |
27 |
Partially correct |
124 ms |
12952 KB |
Partially correct |
28 |
Partially correct |
142 ms |
12880 KB |
Partially correct |
29 |
Correct |
142 ms |
12560 KB |
Output is correct |
30 |
Correct |
216 ms |
14228 KB |
Output is correct |
31 |
Correct |
157 ms |
14840 KB |
Output is correct |
32 |
Correct |
136 ms |
12612 KB |
Output is correct |
33 |
Correct |
94 ms |
11776 KB |
Output is correct |
34 |
Correct |
145 ms |
13752 KB |
Output is correct |
35 |
Correct |
147 ms |
13272 KB |
Output is correct |
36 |
Correct |
131 ms |
13704 KB |
Output is correct |
37 |
Correct |
122 ms |
13928 KB |
Output is correct |
38 |
Partially correct |
154 ms |
15080 KB |
Partially correct |
39 |
Partially correct |
186 ms |
15100 KB |
Partially correct |
40 |
Correct |
227 ms |
16456 KB |
Output is correct |
41 |
Correct |
267 ms |
18296 KB |
Output is correct |