#include <bits/stdc++.h>
/*
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
*/
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define F first
#define S second
#define pb push_back
#define FIO freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout)
#define md(a) (a%mod+mod)%mod
#define all(a) a.begin(), a.end()
#define MP make_pair
#define lc (id<<1)
#define rc (lc|1)
#define mid (l+r)/2
#define kill(a) cout << a << "\n", exit(0)
#define SZ(a) (ll)a.size()
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef long long ll;
typedef long double ld;
typedef vector<vector<ll>> matrix;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll const maxn=6e5+10, mod=1e9+7, INF=1e9, LOG=20, sq=65;
ll poww(ll a, ll b, ll mod) {
if (b == 0) return 1;
return 1 * poww(1 * a * a % mod, b / 2, mod) * ((b % 2 == 1) ? a : 1) % mod;
}
ll n, m, k, t, a, b, dis[4][maxn];
vector<ll> g[maxn];
void BFS(ll ind, ll s)
{
fill(dis[ind], dis[ind]+maxn, INF);
dis[ind][s]=0;
queue<ll> q;
q.push(s);
while(SZ(q))
{
ll v=q.front();
q.pop();
for(ll u:g[v])
{
if(dis[ind][u]>dis[ind][v]+1)
{
dis[ind][u]=dis[ind][v]+1;
q.push(u);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
cin>>k>>t>>a>>b;
for(ll i=1;i<=m;i++)
{
ll v, u;
cin>>v>>u;
g[v].pb(u);
g[u].pb(v);
}
BFS(0, a);
BFS(1, b);
BFS(2, k);
BFS(3, t);
ll K=INF, T=INF;
if(dis[0][k]==dis[1][k])
{
for(ll v=1;v<=n;v++) if(dis[0][v]==dis[1][v] && dis[0][v]+dis[2][v]==dis[0][k])
K=min(K, dis[0][v]);
}
if(dis[0][t]==dis[1][t])
{
for(ll v=1;v<=n;v++) if(dis[0][v]==dis[1][v] && dis[0][v]+dis[3][v]==dis[0][t])
T=min(T, dis[0][v]);
}
ll dk=min(dis[0][k], dis[1][k]);
ll dt=min(dis[0][t], dis[1][t]);
ll ans=(dt-dk);
if(K==INF && T==INF)
{
bool ok=dis[0][k]<dis[1][k];
bool ot=dis[0][t]<dis[1][t];
if(ok!=ot) ans--;
}
else ans-=(K>T);
if(ans<0) ans=-1;
cout<<ans<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |