//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
int n, m, dp[4][MAXN];
vi adj[MAXN];
pii bfs(int src, int x, int y, int p) {
deque<int> pq;
mem(dp[p], 0x3f);
dp[p][src] = 0;
pq.pb(src);
while (!pq.empty()) {
int u = pq.front(); pq.pop_front();
for (int v: adj[u]) {
if (dp[p][v] != 1061109567)
continue;
dp[p][v] = dp[p][u] + 1;
pq.pb(v);
}
}
return mp(dp[p][x], dp[p][y]);
}
int main() {
int s, d, a, b;
scanf("%d %d %d %d %d %d", &n, &m, &s, &d, &a, &b);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
adj[u].pb(v);
adj[v].pb(u);
}
pii a1 = bfs(s, a, b, 0);
pii a2 = bfs(d, a, b, 1);
pii a3 = bfs(a, a, b, 2);
pii a4 = bfs(b, a, b, 3);
//cerr << a1.fi << "/" << a2.fi << " | " << a1.se << "/" << a2.se << "\n";
int ans = min(a2.fi - a1.fi, a2.se - a1.se);
if (a2.fi == a2.se && a1.fi == a1.se) {
int mi1 = INF, mi2 = INF, i1 = 1, i2 = 1;
for (int i = 1; i <= n; i++) {
if (dp[2][i] != dp[3][i])
continue;
if (dp[2][i] + dp[1][i] != a2.fi)
continue;
//cerr << i << " " << dp[2][i] << " " << dp[1][i] << " " << dp[0][i] << "\n";
if (mi1 > dp[2][i])
i1 = i;
mi1 = min(mi1, dp[2][i]);
}
for (int i = 1; i <= n; i++) {
if (dp[2][i] != dp[3][i])
continue;
if (dp[2][i] + dp[0][i] != a1.fi)
continue;
//cerr << i << " " << dp[2][i] << " " << dp[1][i] << " " << dp[0][i] << "\n";
if (mi2 > dp[2][i])
i2 = i;
mi2 = min(mi2, dp[2][i]);
}
if (mi2 > mi1)
ans--;
}
if (ans < 0) ans = -1;
pri(ans);
return 0;
}
Compilation message
007.cpp: In function 'int main()':
007.cpp:73:29: warning: variable 'i1' set but not used [-Wunused-but-set-variable]
int mi1 = INF, mi2 = INF, i1 = 1, i2 = 1;
^~
007.cpp:73:37: warning: variable 'i2' set but not used [-Wunused-but-set-variable]
int mi1 = INF, mi2 = INF, i1 = 1, i2 = 1;
^~
007.cpp:68:6: warning: variable 'a3' set but not used [-Wunused-but-set-variable]
pii a3 = bfs(a, a, b, 2);
^~
007.cpp:69:6: warning: variable 'a4' set but not used [-Wunused-but-set-variable]
pii a4 = bfs(b, a, b, 3);
^~
007.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d %d %d", &n, &m, &s, &d, &a, &b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
39416 KB |
Output is correct |
2 |
Correct |
44 ms |
39472 KB |
Output is correct |
3 |
Correct |
37 ms |
39416 KB |
Output is correct |
4 |
Correct |
37 ms |
39416 KB |
Output is correct |
5 |
Correct |
37 ms |
39416 KB |
Output is correct |
6 |
Correct |
38 ms |
39544 KB |
Output is correct |
7 |
Correct |
37 ms |
39416 KB |
Output is correct |
8 |
Correct |
44 ms |
39416 KB |
Output is correct |
9 |
Correct |
37 ms |
39388 KB |
Output is correct |
10 |
Correct |
37 ms |
39420 KB |
Output is correct |
11 |
Correct |
37 ms |
39388 KB |
Output is correct |
12 |
Correct |
37 ms |
39544 KB |
Output is correct |
13 |
Correct |
37 ms |
39544 KB |
Output is correct |
14 |
Correct |
36 ms |
39416 KB |
Output is correct |
15 |
Correct |
37 ms |
39544 KB |
Output is correct |
16 |
Correct |
37 ms |
39416 KB |
Output is correct |
17 |
Correct |
37 ms |
39516 KB |
Output is correct |
18 |
Correct |
37 ms |
39544 KB |
Output is correct |
19 |
Correct |
37 ms |
39544 KB |
Output is correct |
20 |
Correct |
37 ms |
39544 KB |
Output is correct |
21 |
Correct |
37 ms |
39544 KB |
Output is correct |
22 |
Correct |
37 ms |
39548 KB |
Output is correct |
23 |
Correct |
37 ms |
39444 KB |
Output is correct |
24 |
Correct |
37 ms |
39416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
40688 KB |
Output is correct |
2 |
Correct |
89 ms |
41224 KB |
Output is correct |
3 |
Correct |
72 ms |
40848 KB |
Output is correct |
4 |
Correct |
87 ms |
41464 KB |
Output is correct |
5 |
Correct |
69 ms |
40696 KB |
Output is correct |
6 |
Correct |
72 ms |
40824 KB |
Output is correct |
7 |
Correct |
76 ms |
40952 KB |
Output is correct |
8 |
Correct |
79 ms |
40924 KB |
Output is correct |
9 |
Correct |
91 ms |
41372 KB |
Output is correct |
10 |
Correct |
229 ms |
45560 KB |
Output is correct |
11 |
Correct |
129 ms |
42232 KB |
Output is correct |
12 |
Correct |
144 ms |
43000 KB |
Output is correct |
13 |
Correct |
126 ms |
42488 KB |
Output is correct |
14 |
Correct |
108 ms |
42024 KB |
Output is correct |
15 |
Correct |
152 ms |
43128 KB |
Output is correct |
16 |
Correct |
239 ms |
43256 KB |
Output is correct |
17 |
Correct |
143 ms |
42872 KB |
Output is correct |
18 |
Correct |
152 ms |
42872 KB |
Output is correct |
19 |
Correct |
180 ms |
44176 KB |
Output is correct |
20 |
Correct |
299 ms |
47016 KB |
Output is correct |
21 |
Correct |
202 ms |
44536 KB |
Output is correct |
22 |
Correct |
194 ms |
44024 KB |
Output is correct |
23 |
Correct |
210 ms |
44472 KB |
Output is correct |
24 |
Correct |
202 ms |
44380 KB |
Output is correct |
25 |
Correct |
283 ms |
44152 KB |
Output is correct |
26 |
Correct |
185 ms |
43896 KB |
Output is correct |
27 |
Correct |
212 ms |
44536 KB |
Output is correct |
28 |
Correct |
224 ms |
44536 KB |
Output is correct |
29 |
Correct |
221 ms |
45464 KB |
Output is correct |
30 |
Correct |
310 ms |
47300 KB |
Output is correct |
31 |
Correct |
234 ms |
45304 KB |
Output is correct |
32 |
Correct |
225 ms |
44408 KB |
Output is correct |
33 |
Correct |
212 ms |
44664 KB |
Output is correct |
34 |
Correct |
242 ms |
44764 KB |
Output is correct |
35 |
Correct |
212 ms |
44668 KB |
Output is correct |
36 |
Correct |
218 ms |
44892 KB |
Output is correct |
37 |
Correct |
250 ms |
45688 KB |
Output is correct |
38 |
Correct |
220 ms |
45432 KB |
Output is correct |
39 |
Correct |
273 ms |
45576 KB |
Output is correct |
40 |
Correct |
317 ms |
47056 KB |
Output is correct |
41 |
Correct |
400 ms |
49400 KB |
Output is correct |