#include<cstdio>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
using namespace std;
typedef pair <int,int> ii;
int N, M, S, D, A, B;
vector <int> adj[200010];
queue <ii> near;
int dists[2][200010] = {};
bool vis[200010] = {};
void bfs(int X, int n){
near.push(ii(X, 0));
while(!near.empty()){
int node = near.front().first;
int dist = near.front().second;
near.pop();
if(dists[n][node]) continue;
dists[n][node] = dist;
for(int i : adj[node]){
near.push(ii(i, dist + 1));
}
}
}
int both(int X){
int most;
near.push(ii(X, 0));
while(!near.empty()){
int node = near.front().first;
int dist = near.front().second;
near.pop();
if(vis[node] || dists[0][X] - dists[0][node] != dist || dists[1][X] - dists[1][node] != dist) continue;
vis[node] = true;
most = dist;
for(int i : adj[node]){
near.push(ii(i, dist + 1));
}
}
fill(vis, vis + 200010, false);
return most;
}
int main(){
scanf("%d%d%d%d%d%d",&N,&M,&S,&D,&A,&B);
for(int i = 0;i < M;i++){
int a, b;
scanf("%d%d",&a,&b);
adj[a].push_back(b);
adj[b].push_back(a);
}
bfs(A, 0);
bfs(B, 1);
int diff1 = dists[0][D] - dists[0][S];
int diff2 = dists[1][D] - dists[1][S];
int ideal = max(min(diff1, diff2), -1);
if(diff1 != diff2) printf("%d",ideal);
else printf("%d",ideal - (ideal + both(S) < both(D)));
}
Compilation message
007.cpp: In function 'int main()':
007.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d%d%d%d%d%d",&N,&M,&S,&D,&A,&B);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~
007.cpp: In function 'int both(int)':
007.cpp:41:12: warning: 'most' may be used uninitialized in this function [-Wmaybe-uninitialized]
41 | return most;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
23 |
Correct |
3 ms |
5204 KB |
Output is correct |
24 |
Correct |
3 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
6784 KB |
Output is correct |
2 |
Correct |
30 ms |
7496 KB |
Output is correct |
3 |
Correct |
20 ms |
6796 KB |
Output is correct |
4 |
Correct |
24 ms |
7508 KB |
Output is correct |
5 |
Correct |
18 ms |
6436 KB |
Output is correct |
6 |
Correct |
24 ms |
6740 KB |
Output is correct |
7 |
Correct |
24 ms |
7044 KB |
Output is correct |
8 |
Correct |
21 ms |
7012 KB |
Output is correct |
9 |
Correct |
36 ms |
7500 KB |
Output is correct |
10 |
Correct |
159 ms |
11944 KB |
Output is correct |
11 |
Correct |
41 ms |
8644 KB |
Output is correct |
12 |
Correct |
57 ms |
9596 KB |
Output is correct |
13 |
Correct |
44 ms |
8932 KB |
Output is correct |
14 |
Correct |
39 ms |
8396 KB |
Output is correct |
15 |
Correct |
51 ms |
9640 KB |
Output is correct |
16 |
Correct |
54 ms |
9712 KB |
Output is correct |
17 |
Correct |
48 ms |
9424 KB |
Output is correct |
18 |
Correct |
54 ms |
9404 KB |
Output is correct |
19 |
Correct |
112 ms |
10620 KB |
Output is correct |
20 |
Correct |
190 ms |
13408 KB |
Output is correct |
21 |
Correct |
73 ms |
11476 KB |
Output is correct |
22 |
Correct |
75 ms |
10572 KB |
Output is correct |
23 |
Correct |
76 ms |
11304 KB |
Output is correct |
24 |
Correct |
71 ms |
11224 KB |
Output is correct |
25 |
Correct |
72 ms |
10996 KB |
Output is correct |
26 |
Correct |
66 ms |
10576 KB |
Output is correct |
27 |
Correct |
88 ms |
11420 KB |
Output is correct |
28 |
Correct |
78 ms |
11456 KB |
Output is correct |
29 |
Correct |
130 ms |
11980 KB |
Output is correct |
30 |
Correct |
208 ms |
14000 KB |
Output is correct |
31 |
Correct |
111 ms |
12496 KB |
Output is correct |
32 |
Correct |
84 ms |
11336 KB |
Output is correct |
33 |
Correct |
80 ms |
11528 KB |
Output is correct |
34 |
Correct |
86 ms |
11808 KB |
Output is correct |
35 |
Correct |
79 ms |
11596 KB |
Output is correct |
36 |
Correct |
83 ms |
11840 KB |
Output is correct |
37 |
Correct |
99 ms |
12512 KB |
Output is correct |
38 |
Correct |
98 ms |
12500 KB |
Output is correct |
39 |
Correct |
114 ms |
12600 KB |
Output is correct |
40 |
Correct |
174 ms |
14080 KB |
Output is correct |
41 |
Correct |
301 ms |
16444 KB |
Output is correct |