This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)
using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;
//Note to self: Check for overflow
graph g(200005);
bool vis[200005];
int duba[200005];
int dubb[200005];
int main()
{
FAST;
int n,m; cin>>n>>m;
int ona,on,a,b; cin>>ona>>on>>a>>b;
while (m--)
{
int u,v; cin>>u>>v;
g[u].pb(v),g[v].pb(u);
}
queue<pii> bfs;
bfs.push({a,0});
while (!bfs.empty())
{
auto [p,d]=bfs.front(); bfs.pop();
if (vis[p]) continue; vis[p]=true,duba[p]=d;
for (auto it : g[p]) bfs.push({it,d+1});
}
fff(i,1,n) vis[i]=false;
bfs.push({b,0});
while (!bfs.empty())
{
auto [p,d]=bfs.front(); bfs.pop();
if (vis[p]) continue; vis[p]=true,dubb[p]=d;
for (auto it : g[p]) bfs.push({it,d+1});
}
if (duba[on]<duba[ona]) return cout<<-1,0;
if (dubb[on]<dubb[ona]) return cout<<-1,0;
if (duba[on]-duba[ona]<dubb[on]-dubb[ona]) return cout<<duba[on]-duba[ona],0;
if (duba[on]-duba[ona]>dubb[on]-dubb[ona]) return cout<<dubb[on]-dubb[ona],0;
fff(i,1,n) vis[i]=false;
int korakona=0;
bfs.push({ona,0});
while (!bfs.empty())
{
auto [p,d]=bfs.front(); bfs.pop();
if (vis[p]) continue; vis[p]=true;
if (d>korakona) korakona=d;
for (auto it : g[p]) if (duba[it]<duba[p] && dubb[it]<dubb[p]) bfs.push({it,d+1});
}
fff(i,1,n) vis[i]=false;
int korakon=0;
bfs.push({on,0});
while (!bfs.empty())
{
auto [p,d]=bfs.front(); bfs.pop();
if (vis[p]) continue; vis[p]=true;
if (d>korakon) korakon=d;
for (auto it : g[p]) if (duba[it]<duba[p] && dubb[it]<dubb[p]) bfs.push({it,d+1});
}
if (duba[on]-duba[ona]+korakona >= korakon) return cout<<duba[on]-duba[ona],0;
cout<<duba[on]-duba[ona]-1;
}
//Note to self: Check for overflow
Compilation message (stderr)
007.cpp: In function 'int main()':
007.cpp:60:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
60 | if (vis[p]) continue; vis[p]=true,duba[p]=d;
| ^~
007.cpp:60:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
60 | if (vis[p]) continue; vis[p]=true,duba[p]=d;
| ^~~
007.cpp:69:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
69 | if (vis[p]) continue; vis[p]=true,dubb[p]=d;
| ^~
007.cpp:69:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
69 | if (vis[p]) continue; vis[p]=true,dubb[p]=d;
| ^~~
007.cpp:84:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
84 | if (vis[p]) continue; vis[p]=true;
| ^~
007.cpp:84:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
84 | if (vis[p]) continue; vis[p]=true;
| ^~~
007.cpp:95:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
95 | if (vis[p]) continue; vis[p]=true;
| ^~
007.cpp:95:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
95 | if (vis[p]) continue; vis[p]=true;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |