#include <stdio.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stack>
#include <ctype.h>
#define p(x,y) pair<x, y>
#define pii pair<int, int>
#define v(x) vector<x>
#define q(x) queue<x>
#define pq(x) priority_queue<x>
#define uppq(x, comp) priority_queue<x, vector<x>, comp>
#define st(x) set<x>
#define m(x, y) map<x, y>
#define fi(s,e) for(int i=s;i<e;i++)
#define fj(s,e) for(int j=s;j<e;j++)
#define fk(s,e) for(int k=s;k<e;k++)
typedef long long int ll;
typedef unsigned long long int ull;
typedef __int128 ulll;
using namespace std;
int n,m;
int s,d,a,b;
v(int) pushvec;
v(v(int)) vec;
int dista[200010], distb[200010];
int dpdiv[200010];
void spreada(int x){
q(int) qu;
qu.push(x);
dista[x]=0;
while(!qu.empty()){
int now = qu.front();
qu.pop();
int sz = vec[now].size();
fi(0,sz){
int next = vec[now][i];
if(dista[next]==-1){
dista[next]=dista[now]+1;
qu.push(next);
}
}
}
return;
}
void spreadb(int x){
q(int) qu;
qu.push(x);
distb[x]=0;
while(!qu.empty()){
int now = qu.front();
qu.pop();
int sz = vec[now].size();
fi(0,sz){
int next = vec[now][i];
if(distb[next]==-1){
distb[next]=distb[now]+1;
qu.push(next);
}
}
}
return;
}
int find(int now){
if(dpdiv[now]) return dpdiv[now];
int sz = vec[now].size();
int ans = dista[now];
fi(0,sz){
int next = vec[now][i];
if(dista[next]==dista[now]-1 && dista[next]==distb[next]){
ans = min(ans, find(next));
}
}
return dpdiv[now]=ans;
}
int main(void){
scanf("%d%d%d%d%d%d",&n,&m,&s,&d,&a,&b);
fi(0,n+1) vec.push_back(pushvec);
fi(0,m){
int s,e;
scanf("%d%d",&s,&e);
vec[s].push_back(e);
vec[e].push_back(s);
}
fi(1,n+1){
dista[i]=-1;
distb[i]=-1;
}
spreada(a);
spreadb(b);
int ans = min(dista[d]-dista[s], distb[d]-distb[s]);
if(dista[s]==distb[s] && distb[d]==distb[d]){
int divds, divdd;
divds = find(s);
divdd = find(d);
if(divds > divdd) ans--;
}
printf("%d\n", max(ans, -1));
return 0;
}
Compilation message
007.cpp: In function 'int main()':
007.cpp:111:35: warning: self-comparison always evaluates to true [-Wtautological-compare]
111 | if(dista[s]==distb[s] && distb[d]==distb[d]){
| ~~~~~~~~^~~~~~~~~~
007.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d%d%d%d%d%d",&n,&m,&s,&d,&a,&b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%d%d",&s,&e);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
276 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2924 KB |
Output is correct |
2 |
Correct |
24 ms |
4424 KB |
Output is correct |
3 |
Correct |
23 ms |
3144 KB |
Output is correct |
4 |
Correct |
28 ms |
4552 KB |
Output is correct |
5 |
Correct |
19 ms |
2652 KB |
Output is correct |
6 |
Correct |
17 ms |
3016 KB |
Output is correct |
7 |
Correct |
21 ms |
3400 KB |
Output is correct |
8 |
Correct |
22 ms |
3436 KB |
Output is correct |
9 |
Correct |
32 ms |
3984 KB |
Output is correct |
10 |
Correct |
130 ms |
13812 KB |
Output is correct |
11 |
Correct |
45 ms |
6352 KB |
Output is correct |
12 |
Correct |
62 ms |
7736 KB |
Output is correct |
13 |
Correct |
52 ms |
6668 KB |
Output is correct |
14 |
Correct |
38 ms |
5716 KB |
Output is correct |
15 |
Correct |
60 ms |
7988 KB |
Output is correct |
16 |
Correct |
62 ms |
7908 KB |
Output is correct |
17 |
Correct |
48 ms |
7152 KB |
Output is correct |
18 |
Correct |
51 ms |
7244 KB |
Output is correct |
19 |
Correct |
101 ms |
8908 KB |
Output is correct |
20 |
Correct |
161 ms |
11740 KB |
Output is correct |
21 |
Correct |
83 ms |
11620 KB |
Output is correct |
22 |
Correct |
80 ms |
9480 KB |
Output is correct |
23 |
Correct |
87 ms |
10216 KB |
Output is correct |
24 |
Correct |
86 ms |
10988 KB |
Output is correct |
25 |
Correct |
88 ms |
10164 KB |
Output is correct |
26 |
Correct |
67 ms |
9116 KB |
Output is correct |
27 |
Correct |
84 ms |
10320 KB |
Output is correct |
28 |
Correct |
88 ms |
10424 KB |
Output is correct |
29 |
Correct |
133 ms |
11180 KB |
Output is correct |
30 |
Correct |
169 ms |
12696 KB |
Output is correct |
31 |
Correct |
90 ms |
13240 KB |
Output is correct |
32 |
Correct |
97 ms |
10756 KB |
Output is correct |
33 |
Correct |
93 ms |
10568 KB |
Output is correct |
34 |
Correct |
103 ms |
11616 KB |
Output is correct |
35 |
Correct |
91 ms |
11948 KB |
Output is correct |
36 |
Correct |
96 ms |
12440 KB |
Output is correct |
37 |
Correct |
114 ms |
12416 KB |
Output is correct |
38 |
Correct |
118 ms |
12140 KB |
Output is correct |
39 |
Correct |
120 ms |
12272 KB |
Output is correct |
40 |
Correct |
182 ms |
14252 KB |
Output is correct |
41 |
Correct |
243 ms |
16236 KB |
Output is correct |