#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,s,d,a,b;
cin >> n >> m >> s >> d >> a >> b;
vector<vector<int> > graph(n+1,vector<int>());
for(int i = 0; i < m; i++) {
int x,y;
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
vector<bool> wass(n+1,false);
vector<int> dists(n+1,0);
vector<bool> wasd(n+1,false);
vector<int> distd(n+1,0);
vector<bool> wasa(n+1,false);
vector<int> dista(n+1,0);
vector<bool> wasb(n+1,false);
vector<int> distb(n+1,0);
wass[s] = true;
queue<int> q;
q.push(s);
while(!q.empty()) {
int v = q.front();
q.pop();
for(int u : graph[v]) {
if(!wass[u]) {
wass[u] = true;
q.push(u);
dists[u] = dists[v]+1;
}
}
}
wasd[d] = true;
q.push(d);
while(!q.empty()) {
int v = q.front();
q.pop();
for(int u : graph[v]) {
if(!wasd[u]) {
wasd[u] = true;
q.push(u);
distd[u] = distd[v]+1;
}
}
}
wasa[a] = true;
q.push(a);
while(!q.empty()) {
int v = q.front();
q.pop();
for(int u : graph[v]) {
if(!wasa[u]) {
wasa[u] = true;
q.push(u);
dista[u] = dista[v]+1;
}
}
}
wasb[b] = true;
q.push(b);
while(!q.empty()) {
int v = q.front();
q.pop();
for(int u : graph[v]) {
if(!wasb[u]) {
wasb[u] = true;
q.push(u);
distb[u] = distb[v]+1;
}
}
}
if(dists[a] == dists[b] && distd[a] == distd[b]) {
int mxdistda = n;
int mxdistdb = n;
int mxdistsa = n;
int mxdistsb = n;
for(int i = 1; i <= n; i++) {
if(dista[i] == distb[i]) {
if(dista[i]+distd[i] == distd[a]) {
mxdistda = min(mxdistda,dista[i]);
}
if(distb[i]+distd[i] == distd[b]) {
mxdistdb = min(mxdistdb,distb[i]);
}
if(dista[i]+dists[i] == dists[a]) {
mxdistsa = min(mxdistsa,dista[i]);
}
if(distb[i]+dists[i] == dists[b]) {
mxdistsb = min(mxdistsb,distb[i]);
}
}
}
if(mxdistsa > mxdistda && mxdistsb > mxdistdb) {
cout << max(min(distd[a]-dists[a],distd[b]-dists[b])-1,-1);
} else {
cout << max(min(distd[a]-dists[a],distd[b]-dists[b]),-1);
}
} else {
cout << max(min(distd[a]-dists[a],distd[b]-dists[b]),-1);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
464 KB |
Output is correct |
3 |
Correct |
3 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
464 KB |
Output is correct |
5 |
Correct |
3 ms |
492 KB |
Output is correct |
6 |
Correct |
3 ms |
564 KB |
Output is correct |
7 |
Correct |
3 ms |
564 KB |
Output is correct |
8 |
Correct |
2 ms |
564 KB |
Output is correct |
9 |
Correct |
3 ms |
564 KB |
Output is correct |
10 |
Correct |
3 ms |
564 KB |
Output is correct |
11 |
Correct |
2 ms |
564 KB |
Output is correct |
12 |
Correct |
3 ms |
572 KB |
Output is correct |
13 |
Correct |
3 ms |
572 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
15 |
Correct |
3 ms |
620 KB |
Output is correct |
16 |
Correct |
3 ms |
620 KB |
Output is correct |
17 |
Correct |
3 ms |
620 KB |
Output is correct |
18 |
Correct |
4 ms |
620 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
3 ms |
624 KB |
Output is correct |
21 |
Correct |
3 ms |
624 KB |
Output is correct |
22 |
Correct |
3 ms |
624 KB |
Output is correct |
23 |
Correct |
3 ms |
624 KB |
Output is correct |
24 |
Correct |
3 ms |
624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
3436 KB |
Output is correct |
2 |
Correct |
42 ms |
4844 KB |
Output is correct |
3 |
Correct |
56 ms |
4844 KB |
Output is correct |
4 |
Correct |
81 ms |
4988 KB |
Output is correct |
5 |
Correct |
43 ms |
4988 KB |
Output is correct |
6 |
Correct |
61 ms |
4988 KB |
Output is correct |
7 |
Correct |
37 ms |
4988 KB |
Output is correct |
8 |
Correct |
50 ms |
4988 KB |
Output is correct |
9 |
Correct |
87 ms |
4988 KB |
Output is correct |
10 |
Correct |
223 ms |
9288 KB |
Output is correct |
11 |
Correct |
90 ms |
9288 KB |
Output is correct |
12 |
Correct |
123 ms |
9288 KB |
Output is correct |
13 |
Correct |
129 ms |
9288 KB |
Output is correct |
14 |
Correct |
96 ms |
9288 KB |
Output is correct |
15 |
Correct |
152 ms |
9288 KB |
Output is correct |
16 |
Correct |
123 ms |
9288 KB |
Output is correct |
17 |
Correct |
105 ms |
9288 KB |
Output is correct |
18 |
Correct |
118 ms |
9288 KB |
Output is correct |
19 |
Correct |
180 ms |
9620 KB |
Output is correct |
20 |
Correct |
366 ms |
12540 KB |
Output is correct |
21 |
Correct |
160 ms |
12540 KB |
Output is correct |
22 |
Correct |
219 ms |
12540 KB |
Output is correct |
23 |
Correct |
151 ms |
12540 KB |
Output is correct |
24 |
Correct |
221 ms |
12540 KB |
Output is correct |
25 |
Correct |
148 ms |
12540 KB |
Output is correct |
26 |
Correct |
170 ms |
12540 KB |
Output is correct |
27 |
Correct |
223 ms |
12540 KB |
Output is correct |
28 |
Correct |
240 ms |
12540 KB |
Output is correct |
29 |
Correct |
224 ms |
12540 KB |
Output is correct |
30 |
Correct |
405 ms |
13692 KB |
Output is correct |
31 |
Correct |
208 ms |
13820 KB |
Output is correct |
32 |
Correct |
180 ms |
13820 KB |
Output is correct |
33 |
Correct |
174 ms |
13820 KB |
Output is correct |
34 |
Correct |
270 ms |
13820 KB |
Output is correct |
35 |
Correct |
185 ms |
13820 KB |
Output is correct |
36 |
Correct |
181 ms |
13820 KB |
Output is correct |
37 |
Correct |
275 ms |
14504 KB |
Output is correct |
38 |
Correct |
208 ms |
14504 KB |
Output is correct |
39 |
Correct |
264 ms |
14504 KB |
Output is correct |
40 |
Correct |
344 ms |
15552 KB |
Output is correct |
41 |
Correct |
550 ms |
17660 KB |
Output is correct |