#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 |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Partially correct |
2 ms |
432 KB |
Partially correct |
4 |
Correct |
3 ms |
508 KB |
Output is correct |
5 |
Correct |
4 ms |
632 KB |
Output is correct |
6 |
Partially correct |
2 ms |
632 KB |
Partially correct |
7 |
Partially correct |
3 ms |
632 KB |
Partially correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Partially correct |
2 ms |
632 KB |
Partially correct |
10 |
Correct |
3 ms |
632 KB |
Output is correct |
11 |
Correct |
3 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Partially correct |
3 ms |
632 KB |
Partially correct |
14 |
Correct |
3 ms |
632 KB |
Output is correct |
15 |
Partially correct |
3 ms |
632 KB |
Partially correct |
16 |
Correct |
3 ms |
716 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
4 ms |
716 KB |
Output is correct |
19 |
Partially correct |
3 ms |
716 KB |
Partially correct |
20 |
Partially correct |
3 ms |
716 KB |
Partially correct |
21 |
Correct |
3 ms |
716 KB |
Output is correct |
22 |
Partially correct |
4 ms |
716 KB |
Partially correct |
23 |
Partially correct |
4 ms |
716 KB |
Partially correct |
24 |
Correct |
5 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
45 ms |
3436 KB |
Partially correct |
2 |
Correct |
59 ms |
4848 KB |
Output is correct |
3 |
Partially correct |
46 ms |
4848 KB |
Partially correct |
4 |
Correct |
60 ms |
4972 KB |
Output is correct |
5 |
Correct |
37 ms |
4972 KB |
Output is correct |
6 |
Correct |
27 ms |
4972 KB |
Output is correct |
7 |
Partially correct |
37 ms |
4972 KB |
Partially correct |
8 |
Partially correct |
39 ms |
4972 KB |
Partially correct |
9 |
Correct |
80 ms |
4972 KB |
Output is correct |
10 |
Partially correct |
245 ms |
9256 KB |
Partially correct |
11 |
Correct |
119 ms |
9256 KB |
Output is correct |
12 |
Partially correct |
119 ms |
9256 KB |
Partially correct |
13 |
Correct |
135 ms |
9256 KB |
Output is correct |
14 |
Correct |
103 ms |
9256 KB |
Output is correct |
15 |
Partially correct |
143 ms |
9256 KB |
Partially correct |
16 |
Correct |
156 ms |
9264 KB |
Output is correct |
17 |
Partially correct |
132 ms |
9264 KB |
Partially correct |
18 |
Correct |
130 ms |
9264 KB |
Output is correct |
19 |
Partially correct |
207 ms |
9740 KB |
Partially correct |
20 |
Correct |
351 ms |
12632 KB |
Output is correct |
21 |
Correct |
187 ms |
12632 KB |
Output is correct |
22 |
Partially correct |
205 ms |
12632 KB |
Partially correct |
23 |
Correct |
155 ms |
12632 KB |
Output is correct |
24 |
Partially correct |
199 ms |
12632 KB |
Partially correct |
25 |
Correct |
198 ms |
12632 KB |
Output is correct |
26 |
Correct |
163 ms |
12632 KB |
Output is correct |
27 |
Partially correct |
201 ms |
12632 KB |
Partially correct |
28 |
Partially correct |
214 ms |
12632 KB |
Partially correct |
29 |
Partially correct |
251 ms |
12632 KB |
Partially correct |
30 |
Correct |
327 ms |
13648 KB |
Output is correct |
31 |
Correct |
239 ms |
13692 KB |
Output is correct |
32 |
Partially correct |
161 ms |
13692 KB |
Partially correct |
33 |
Correct |
168 ms |
13692 KB |
Output is correct |
34 |
Correct |
245 ms |
13692 KB |
Output is correct |
35 |
Correct |
244 ms |
13692 KB |
Output is correct |
36 |
Correct |
241 ms |
13692 KB |
Output is correct |
37 |
Correct |
249 ms |
14460 KB |
Output is correct |
38 |
Partially correct |
280 ms |
14460 KB |
Partially correct |
39 |
Partially correct |
358 ms |
14460 KB |
Partially correct |
40 |
Correct |
419 ms |
15484 KB |
Output is correct |
41 |
Partially correct |
428 ms |
17404 KB |
Partially correct |