#include <bits/stdc++.h>
using namespace std;
const int mxN=2e5;
int n, m, s, d, a, b, da[mxN], db[mxN], qu[mxN], qh, qt, dp[mxN];
vector<int> adj[mxN];
inline void bfs(int s, int dist[mxN]) {
qh=qt=0;
memset(dist, 0x3F, 4*n);
qu[qt++]=s;
dist[s]=0;
while(qh<qt) {
int u=qu[qh++];
for(int v : adj[u]) {
if(dist[v]>n) {
qu[qt++]=v;
dist[v]=dist[u]+1;
}
}
}
}
int cdp(int u) {
if(!dp[u]) {
dp[u]=da[u];
for(int v : adj[u])
if(da[v]==da[u]-1&&da[v]==db[v])
dp[u]=min(cdp(v), dp[u]);
}
return dp[u];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> s >> d >> a >> b, --s, --d, --a, --b;
while(m--) {
int u, v;
cin >> u >> v, --u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
bfs(a, da);
bfs(b, db);
if(da[s]!=db[s]||da[d]!=db[d])
cout << max(min(da[d]-da[s], db[d]-db[s]), -1);
else
cout << max(da[d]-da[s]-(cdp(d)<cdp(s)), -1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5216 KB |
Output is correct |
3 |
Correct |
8 ms |
5216 KB |
Output is correct |
4 |
Correct |
8 ms |
5216 KB |
Output is correct |
5 |
Correct |
8 ms |
5216 KB |
Output is correct |
6 |
Correct |
8 ms |
5216 KB |
Output is correct |
7 |
Correct |
7 ms |
5232 KB |
Output is correct |
8 |
Correct |
7 ms |
5288 KB |
Output is correct |
9 |
Correct |
8 ms |
5460 KB |
Output is correct |
10 |
Correct |
7 ms |
5460 KB |
Output is correct |
11 |
Correct |
6 ms |
5460 KB |
Output is correct |
12 |
Correct |
7 ms |
5460 KB |
Output is correct |
13 |
Correct |
9 ms |
5460 KB |
Output is correct |
14 |
Correct |
8 ms |
5460 KB |
Output is correct |
15 |
Correct |
10 ms |
5460 KB |
Output is correct |
16 |
Correct |
8 ms |
5460 KB |
Output is correct |
17 |
Correct |
9 ms |
5460 KB |
Output is correct |
18 |
Correct |
7 ms |
5460 KB |
Output is correct |
19 |
Correct |
8 ms |
5460 KB |
Output is correct |
20 |
Correct |
8 ms |
5460 KB |
Output is correct |
21 |
Correct |
8 ms |
5460 KB |
Output is correct |
22 |
Correct |
9 ms |
5460 KB |
Output is correct |
23 |
Correct |
8 ms |
5460 KB |
Output is correct |
24 |
Correct |
9 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
7164 KB |
Output is correct |
2 |
Correct |
63 ms |
8184 KB |
Output is correct |
3 |
Correct |
32 ms |
8184 KB |
Output is correct |
4 |
Correct |
43 ms |
8312 KB |
Output is correct |
5 |
Correct |
52 ms |
8312 KB |
Output is correct |
6 |
Correct |
36 ms |
8312 KB |
Output is correct |
7 |
Correct |
37 ms |
8312 KB |
Output is correct |
8 |
Correct |
37 ms |
8312 KB |
Output is correct |
9 |
Correct |
60 ms |
8312 KB |
Output is correct |
10 |
Correct |
190 ms |
12412 KB |
Output is correct |
11 |
Correct |
87 ms |
12412 KB |
Output is correct |
12 |
Correct |
133 ms |
12412 KB |
Output is correct |
13 |
Correct |
117 ms |
12412 KB |
Output is correct |
14 |
Correct |
78 ms |
12412 KB |
Output is correct |
15 |
Correct |
119 ms |
12412 KB |
Output is correct |
16 |
Correct |
127 ms |
12412 KB |
Output is correct |
17 |
Correct |
88 ms |
12412 KB |
Output is correct |
18 |
Correct |
90 ms |
12412 KB |
Output is correct |
19 |
Correct |
185 ms |
12412 KB |
Output is correct |
20 |
Correct |
294 ms |
14636 KB |
Output is correct |
21 |
Correct |
139 ms |
14636 KB |
Output is correct |
22 |
Correct |
157 ms |
14636 KB |
Output is correct |
23 |
Correct |
135 ms |
14636 KB |
Output is correct |
24 |
Correct |
143 ms |
14636 KB |
Output is correct |
25 |
Correct |
134 ms |
14636 KB |
Output is correct |
26 |
Correct |
109 ms |
14636 KB |
Output is correct |
27 |
Correct |
171 ms |
14636 KB |
Output is correct |
28 |
Correct |
169 ms |
14636 KB |
Output is correct |
29 |
Correct |
204 ms |
14636 KB |
Output is correct |
30 |
Correct |
367 ms |
15368 KB |
Output is correct |
31 |
Correct |
182 ms |
15368 KB |
Output is correct |
32 |
Correct |
171 ms |
15368 KB |
Output is correct |
33 |
Correct |
123 ms |
15368 KB |
Output is correct |
34 |
Correct |
207 ms |
15368 KB |
Output is correct |
35 |
Correct |
159 ms |
15368 KB |
Output is correct |
36 |
Correct |
172 ms |
15368 KB |
Output is correct |
37 |
Correct |
202 ms |
15368 KB |
Output is correct |
38 |
Correct |
192 ms |
15368 KB |
Output is correct |
39 |
Correct |
266 ms |
15368 KB |
Output is correct |
40 |
Correct |
358 ms |
15764 KB |
Output is correct |
41 |
Correct |
385 ms |
18016 KB |
Output is correct |