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];
bool moze(int n, int ona, int on, int wait)
{
fff(i,1,n) vis[i]=false;
//cout<<"cekam koliko: "<<wait<<endl;
//cout<<"razlica: ";
queue<pii> bfs;
bfs.push({on,wait});
while (!bfs.empty())
{
auto [p,d]=bfs.front(); bfs.pop();
if (vis[p]) continue; vis[p]=true; //cout<<p<<" ";
if (duba[p]<duba[ona]) return false;
if (dubb[p]<dubb[ona]) return false;
if (d==0) continue;
for (auto it : g[p]) bfs.push({it,d-1});
}
return true;
}
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]=1,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]=1,dubb[p]=d;
for (auto it : g[p]) bfs.push({it,d+1});
}
int l=-1,r=min(duba[on],dubb[on]);
while (r-l>1)
{
int mid=(l+r)/2;
if (moze(n,ona,on,mid)) l=mid;
else r=mid;
//cout<<endl<<endl;
}
cout<<l<<"\n";
}
//Note to self: Check for overflow
Compilation message (stderr)
007.cpp: In function 'bool moze(int, int, int, int)':
007.cpp:55:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
55 | if (vis[p]) continue; vis[p]=true; //cout<<p<<" ";
| ^~
007.cpp:55:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
55 | if (vis[p]) continue; vis[p]=true; //cout<<p<<" ";
| ^~~
007.cpp: In function 'int main()':
007.cpp:82:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
82 | if (vis[p]) continue; vis[p]=1,duba[p]=d;
| ^~
007.cpp:82:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
82 | if (vis[p]) continue; vis[p]=1,duba[p]=d;
| ^~~
007.cpp:91:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
91 | if (vis[p]) continue; vis[p]=1,dubb[p]=d;
| ^~
007.cpp:91:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
91 | if (vis[p]) continue; vis[p]=1,dubb[p]=d;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |