#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 200010, LOG=20;
int n, m, k, u, v, x, y, t, a, b, ans;
int dist[4][MAXN];
vector<int> G[MAXN];
queue<int> Q;
void BFS(int* dist, int src){
dist[src]=0;
Q.push(src);
while (Q.size()){
int v=Q.front();
Q.pop();
for (int u:G[v]) if (dist[v]+1<dist[u]){
dist[u]=dist[v]+1;
Q.push(u);
}
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
memset(dist, 31, sizeof(dist));
cin>>n>>m>>k>>t>>a>>b;
while (m--){
cin>>u>>v;
G[u].pb(v);
G[v].pb(u);
}
BFS(dist[0], a);
BFS(dist[1], b);
BFS(dist[2], k);
BFS(dist[3], t);
int fk=inf, ft=inf;
if (dist[0][k]==dist[1][k]){
for (int v=1; v<=n; v++){
if (dist[0][v]==dist[1][v] && dist[0][v]+dist[2][v]==dist[0][k]) fk=min(fk, dist[0][v]);
}
}
if (dist[0][t]==dist[1][t]){
for (int v=1; v<=n; v++){
if (dist[0][v]==dist[1][v] && dist[0][v]+dist[3][v]==dist[0][t]) ft=min(ft, dist[0][v]);
}
}
int dt=min(dist[0][t], dist[1][t]), dk=min(dist[0][k], dist[1][k]);
ans=dt-dk;
if (fk==inf && ft==inf){
int optk=(dist[1][k]==dk);
int optt=(dist[1][t]==dt);
ans-=(optk!=optt);
}
else ans-=(fk>ft);
if (ans<0) ans--;
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
9 ms |
8192 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
10 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8192 KB |
Output is correct |
10 |
Correct |
9 ms |
8192 KB |
Output is correct |
11 |
Correct |
9 ms |
8192 KB |
Output is correct |
12 |
Correct |
10 ms |
8192 KB |
Output is correct |
13 |
Correct |
9 ms |
8192 KB |
Output is correct |
14 |
Correct |
9 ms |
8192 KB |
Output is correct |
15 |
Correct |
9 ms |
8192 KB |
Output is correct |
16 |
Correct |
12 ms |
8192 KB |
Output is correct |
17 |
Correct |
9 ms |
8192 KB |
Output is correct |
18 |
Correct |
10 ms |
8192 KB |
Output is correct |
19 |
Correct |
9 ms |
8192 KB |
Output is correct |
20 |
Correct |
10 ms |
8192 KB |
Output is correct |
21 |
Correct |
9 ms |
8192 KB |
Output is correct |
22 |
Correct |
12 ms |
8192 KB |
Output is correct |
23 |
Correct |
9 ms |
8192 KB |
Output is correct |
24 |
Correct |
9 ms |
8192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
9984 KB |
Output is correct |
2 |
Correct |
40 ms |
10744 KB |
Output is correct |
3 |
Correct |
34 ms |
9984 KB |
Output is correct |
4 |
Correct |
46 ms |
11000 KB |
Output is correct |
5 |
Correct |
31 ms |
9856 KB |
Output is correct |
6 |
Correct |
31 ms |
10112 KB |
Output is correct |
7 |
Correct |
36 ms |
10240 KB |
Output is correct |
8 |
Correct |
36 ms |
10232 KB |
Output is correct |
9 |
Correct |
48 ms |
11128 KB |
Output is correct |
10 |
Correct |
171 ms |
19780 KB |
Output is correct |
11 |
Correct |
65 ms |
12024 KB |
Output is correct |
12 |
Correct |
89 ms |
13176 KB |
Output is correct |
13 |
Correct |
73 ms |
12408 KB |
Output is correct |
14 |
Incorrect |
58 ms |
11768 KB |
Output isn't correct |
15 |
Correct |
109 ms |
13176 KB |
Output is correct |
16 |
Correct |
89 ms |
13560 KB |
Output is correct |
17 |
Correct |
81 ms |
12920 KB |
Output is correct |
18 |
Correct |
91 ms |
12920 KB |
Output is correct |
19 |
Correct |
120 ms |
15228 KB |
Output is correct |
20 |
Correct |
222 ms |
21168 KB |
Output is correct |
21 |
Correct |
119 ms |
15224 KB |
Output is correct |
22 |
Correct |
116 ms |
14200 KB |
Output is correct |
23 |
Correct |
124 ms |
15096 KB |
Output is correct |
24 |
Correct |
128 ms |
15112 KB |
Output is correct |
25 |
Correct |
117 ms |
14712 KB |
Output is correct |
26 |
Correct |
109 ms |
14328 KB |
Output is correct |
27 |
Correct |
133 ms |
15224 KB |
Output is correct |
28 |
Correct |
161 ms |
15280 KB |
Output is correct |
29 |
Correct |
161 ms |
17272 KB |
Output is correct |
30 |
Correct |
230 ms |
22008 KB |
Output is correct |
31 |
Correct |
149 ms |
16252 KB |
Output is correct |
32 |
Correct |
135 ms |
15224 KB |
Output is correct |
33 |
Correct |
129 ms |
15356 KB |
Output is correct |
34 |
Correct |
155 ms |
15736 KB |
Output is correct |
35 |
Correct |
147 ms |
15608 KB |
Output is correct |
36 |
Correct |
146 ms |
15736 KB |
Output is correct |
37 |
Correct |
185 ms |
16888 KB |
Output is correct |
38 |
Correct |
169 ms |
16504 KB |
Output is correct |
39 |
Correct |
184 ms |
16632 KB |
Output is correct |
40 |
Correct |
216 ms |
20344 KB |
Output is correct |
41 |
Correct |
297 ms |
25464 KB |
Output is correct |