#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int read(){
char p;
while((p = getchar_unlocked())){
if(p < '0' || p > '9') continue;
break;
}
int ret = p - '0';
while((p = getchar_unlocked())){
if(p >= '0' && p <= '9'){
ret *= 10;
ret += p - '0';
}
else break;
}
return ret;
}
int n, m, u, v;
int s, d, a, b, disa[200007], disb[200007], dist[200007];
bool vis[200007];
queue <int> q;
vector <int> edge[200007];
int go(int st){
memset(dist, 0, sizeof(dist));
memset(vis, false, sizeof(vis));
int res = 0;
q.push(st);
vis[st] = true;
while(!q.empty()){
u = q.front();
q.pop();
res = max(res, dist[u]);
for(int v : edge[u]){
if(vis[v]) continue;
if(disa[u] == disa[v] + 1 && disb[u] == disb[v] + 1){
vis[v] = true;
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
return res;
}
int main(){
n = read(), m = read();
s = read(), d = read(), a = read(), b = read();
for(int i = 1; i <= m; i++){
u = read();
v = read();
edge[u].pb(v);
edge[v].pb(u);
}
vis[a] = true;
q.push(a);
while(!q.empty()){
u = q.front();
q.pop();
for(int v : edge[u]){
if(vis[v]) continue;
vis[v] = true;
disa[v] = disa[u] + 1;
q.push(v);
}
}
memset(vis, false, sizeof(vis));
vis[b] = true;
q.push(b);
while(!q.empty()){
u = q.front();
q.pop();
for(int v : edge[u]){
if(vis[v]) continue;
vis[v] = true;
disb[v] = disb[u] + 1;
q.push(v);
}
}
int cal_a = disa[d] - disa[s];
int cal_b = disb[d] - disb[s];
//cout << cal_a << " " << cal_b << endl;
if(min(cal_a, cal_b) < 0){
printf("-1");
return 0;
}
if(cal_a != cal_b){
cal_a = min(cal_a, cal_b);
cout << cal_a;
return 0;
}
int down_s = go(s), down_d = go(d);
if(cal_a + down_s < down_d) cal_a--;
if(cal_a < 0) cout << -1;
else cout << cal_a;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
7 ms |
6012 KB |
Output is correct |
3 |
Correct |
7 ms |
6056 KB |
Output is correct |
4 |
Correct |
7 ms |
6072 KB |
Output is correct |
5 |
Correct |
6 ms |
6244 KB |
Output is correct |
6 |
Correct |
7 ms |
6244 KB |
Output is correct |
7 |
Correct |
7 ms |
6244 KB |
Output is correct |
8 |
Correct |
7 ms |
6244 KB |
Output is correct |
9 |
Correct |
6 ms |
6244 KB |
Output is correct |
10 |
Correct |
6 ms |
6244 KB |
Output is correct |
11 |
Correct |
6 ms |
6244 KB |
Output is correct |
12 |
Correct |
7 ms |
6244 KB |
Output is correct |
13 |
Correct |
7 ms |
6244 KB |
Output is correct |
14 |
Correct |
7 ms |
6244 KB |
Output is correct |
15 |
Correct |
7 ms |
6268 KB |
Output is correct |
16 |
Correct |
7 ms |
6268 KB |
Output is correct |
17 |
Correct |
7 ms |
6268 KB |
Output is correct |
18 |
Correct |
7 ms |
6268 KB |
Output is correct |
19 |
Correct |
7 ms |
6268 KB |
Output is correct |
20 |
Correct |
9 ms |
6268 KB |
Output is correct |
21 |
Correct |
6 ms |
6268 KB |
Output is correct |
22 |
Correct |
7 ms |
6268 KB |
Output is correct |
23 |
Correct |
7 ms |
6268 KB |
Output is correct |
24 |
Correct |
7 ms |
6268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7804 KB |
Output is correct |
2 |
Correct |
22 ms |
8632 KB |
Output is correct |
3 |
Correct |
21 ms |
8632 KB |
Output is correct |
4 |
Correct |
22 ms |
8700 KB |
Output is correct |
5 |
Correct |
18 ms |
8700 KB |
Output is correct |
6 |
Correct |
18 ms |
8700 KB |
Output is correct |
7 |
Correct |
20 ms |
8700 KB |
Output is correct |
8 |
Correct |
20 ms |
8700 KB |
Output is correct |
9 |
Correct |
29 ms |
8700 KB |
Output is correct |
10 |
Correct |
84 ms |
12944 KB |
Output is correct |
11 |
Correct |
33 ms |
12944 KB |
Output is correct |
12 |
Correct |
50 ms |
12944 KB |
Output is correct |
13 |
Correct |
44 ms |
12944 KB |
Output is correct |
14 |
Correct |
29 ms |
12944 KB |
Output is correct |
15 |
Correct |
41 ms |
12944 KB |
Output is correct |
16 |
Correct |
47 ms |
12944 KB |
Output is correct |
17 |
Correct |
39 ms |
12944 KB |
Output is correct |
18 |
Correct |
41 ms |
12944 KB |
Output is correct |
19 |
Correct |
68 ms |
12944 KB |
Output is correct |
20 |
Correct |
125 ms |
14556 KB |
Output is correct |
21 |
Correct |
66 ms |
14556 KB |
Output is correct |
22 |
Correct |
61 ms |
14556 KB |
Output is correct |
23 |
Correct |
70 ms |
14556 KB |
Output is correct |
24 |
Correct |
61 ms |
14556 KB |
Output is correct |
25 |
Correct |
66 ms |
14556 KB |
Output is correct |
26 |
Correct |
54 ms |
14556 KB |
Output is correct |
27 |
Correct |
68 ms |
14556 KB |
Output is correct |
28 |
Correct |
83 ms |
14556 KB |
Output is correct |
29 |
Correct |
102 ms |
14556 KB |
Output is correct |
30 |
Correct |
172 ms |
15100 KB |
Output is correct |
31 |
Correct |
77 ms |
15100 KB |
Output is correct |
32 |
Correct |
95 ms |
15100 KB |
Output is correct |
33 |
Correct |
69 ms |
15100 KB |
Output is correct |
34 |
Correct |
98 ms |
15100 KB |
Output is correct |
35 |
Correct |
70 ms |
15100 KB |
Output is correct |
36 |
Correct |
80 ms |
15100 KB |
Output is correct |
37 |
Correct |
86 ms |
15100 KB |
Output is correct |
38 |
Correct |
86 ms |
15100 KB |
Output is correct |
39 |
Correct |
110 ms |
15100 KB |
Output is correct |
40 |
Correct |
145 ms |
15360 KB |
Output is correct |
41 |
Correct |
237 ms |
17404 KB |
Output is correct |