#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 200010, inf = 1e9;
int n, m, s, d, A, B;
vector<int> edge[MAX_N];
int diss[MAX_N], disd[MAX_N], disa[MAX_N], disb[MAX_N];
void get_dis(int s, int *dis) {
fill(dis, dis + n + 1, inf);
queue<int> q;
q.emplace(s); dis[s] = 0;
while (q.size()) {
int x = q.front(); q.pop();
for (int u : edge[x])
if (chmin(dis[u], dis[x] + 1))
q.emplace(u);
}
}
bool valid(int rest) {
auto test = [&](int x) {
int art = diss[x] + rest;
int ga = art + disa[x], gb = art + disb[x];
if (max(disa[x], disb[x]) > 1) return false;
int da = disd[A], db = disd[B];
if (min(disa[x], disb[x]) == 1) {
if (art < min(da, db)) return true;
return false;
}
if (ga <= da && gb <= db) return true;
return false;
};
for (int i = 1;i <= n;++i)
if (test(i)) return true;
return false;
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> s >> d >> A >> B;
for (int a, b, i = 0;i < m;++i) {
cin >> a >> b;
DE(a, b);
edge[a].pb(b), edge[b].pb(a);
}
get_dis(s, diss), get_dis(d, disd);
get_dis(A, disa), get_dis(B, disb);
if (!valid(0)) return puts("-1"), 0;
cout << min(disd[A]-disa[A], disd[B]-disd[B]) << '\n';
return 0;
int l = 0, r = n, mid;
while (l < r) {
mid = l + r >> 1;
if (valid(mid+1))
l = mid + 1;
else
r = mid;
}
cout << l << '\n';
}
Compilation message
007.cpp: In function 'int32_t main()':
007.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
007.cpp:67:3: note: in expansion of macro 'DE'
67 | DE(a, b);
| ^~
007.cpp:81:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
3 |
Partially correct |
3 ms |
4940 KB |
Partially correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
7 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
10 |
Incorrect |
3 ms |
4956 KB |
Output isn't correct |
11 |
Partially correct |
3 ms |
4940 KB |
Partially correct |
12 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
5064 KB |
Output isn't correct |
14 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
15 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
16 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
17 |
Incorrect |
3 ms |
5068 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
4988 KB |
Output isn't correct |
19 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
20 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
21 |
Incorrect |
4 ms |
4940 KB |
Output isn't correct |
22 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
23 |
Incorrect |
4 ms |
5020 KB |
Output isn't correct |
24 |
Correct |
4 ms |
5028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
6884 KB |
Output isn't correct |
2 |
Incorrect |
32 ms |
7804 KB |
Output isn't correct |
3 |
Incorrect |
27 ms |
6988 KB |
Output isn't correct |
4 |
Incorrect |
34 ms |
7804 KB |
Output isn't correct |
5 |
Incorrect |
26 ms |
6836 KB |
Output isn't correct |
6 |
Incorrect |
25 ms |
7036 KB |
Output isn't correct |
7 |
Incorrect |
33 ms |
7276 KB |
Output isn't correct |
8 |
Incorrect |
30 ms |
7240 KB |
Output isn't correct |
9 |
Incorrect |
42 ms |
7636 KB |
Output isn't correct |
10 |
Incorrect |
145 ms |
12056 KB |
Output isn't correct |
11 |
Incorrect |
59 ms |
9152 KB |
Output isn't correct |
12 |
Incorrect |
91 ms |
10320 KB |
Output isn't correct |
13 |
Incorrect |
71 ms |
9540 KB |
Output isn't correct |
14 |
Correct |
66 ms |
8868 KB |
Output is correct |
15 |
Incorrect |
75 ms |
10432 KB |
Output isn't correct |
16 |
Incorrect |
81 ms |
10660 KB |
Output isn't correct |
17 |
Incorrect |
94 ms |
10072 KB |
Output isn't correct |
18 |
Incorrect |
80 ms |
10232 KB |
Output isn't correct |
19 |
Incorrect |
107 ms |
11332 KB |
Output isn't correct |
20 |
Incorrect |
185 ms |
14140 KB |
Output isn't correct |
21 |
Incorrect |
108 ms |
12460 KB |
Output isn't correct |
22 |
Incorrect |
104 ms |
11432 KB |
Output isn't correct |
23 |
Incorrect |
118 ms |
12476 KB |
Output isn't correct |
24 |
Incorrect |
106 ms |
12328 KB |
Output isn't correct |
25 |
Incorrect |
135 ms |
11972 KB |
Output isn't correct |
26 |
Incorrect |
106 ms |
11544 KB |
Output isn't correct |
27 |
Incorrect |
124 ms |
12608 KB |
Output isn't correct |
28 |
Incorrect |
138 ms |
12428 KB |
Output isn't correct |
29 |
Incorrect |
143 ms |
12872 KB |
Output isn't correct |
30 |
Incorrect |
209 ms |
14904 KB |
Output isn't correct |
31 |
Incorrect |
137 ms |
13560 KB |
Output isn't correct |
32 |
Incorrect |
130 ms |
12392 KB |
Output isn't correct |
33 |
Incorrect |
139 ms |
12708 KB |
Output isn't correct |
34 |
Incorrect |
169 ms |
13024 KB |
Output isn't correct |
35 |
Incorrect |
128 ms |
12848 KB |
Output isn't correct |
36 |
Incorrect |
133 ms |
12996 KB |
Output isn't correct |
37 |
Incorrect |
173 ms |
14148 KB |
Output isn't correct |
38 |
Incorrect |
182 ms |
13896 KB |
Output isn't correct |
39 |
Incorrect |
183 ms |
13956 KB |
Output isn't correct |
40 |
Incorrect |
210 ms |
15484 KB |
Output isn't correct |
41 |
Incorrect |
298 ms |
17560 KB |
Output isn't correct |