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>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 2e5 + 20;
vector<int> adj[maxn];
int d[3][maxn] , x[maxn];
void bfs(int src , int k)
{
memset(d[k] , 63 , sizeof d[k]);
queue<int> q;
q.push(src);
d[k][src] = 0;
while(!q.empty())
{
int v = q.front();
q.pop();
for(auto u : adj[v])
if(d[k][u] > d[k][v] + 1)
{
d[k][u] = d[k][v] + 1;
q.push(u);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n , m;
cin >> n >> m;
int a , b , s , t;
cin >> s >> t >> a >> b;
s-- , t-- , a-- , b--;
for(int i = 0; i < m; i++)
{
int a , b;
cin >> a >> b;
a-- , b--;
adj[a].pb(b);
adj[b].pb(a);
}
bfs(a , 0) , bfs(b , 1) , bfs(t , 2);
if(d[0][s] > d[0][t] || d[1][s] > d[1][t])
return cout << -1 << endl , 0;
vector<pair<int , int> > tmp;
for(int i = 0; i < n; i++)
tmp.pb({d[0][i] , i});
sort(tmp.begin() , tmp.end());
memset(x , -1 , sizeof x);
for(auto sht : tmp)
{
int v = sht.second;
x[v] = 0;
for(auto u : adj[v])
if(d[0][u] + 1 == d[0][v] && d[1][u] + 1 == d[1][v])
x[v] = max(x[v] , x[u] + 1);
}
int res = 1e9;
for(int t = 0; t < n; t++)
{
if(d[0][s] > d[0][t] || d[1][s] > d[1][t])
res = min(res , d[2][t]);
else if(d[0][s] == d[0][t] && d[1][s] == d[1][t] && x[t] > x[s])
res = min(res , d[2][t]);
}
cout << res - 1 << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |