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[2][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);
if(d[0][s] > d[0][t] || d[1][s] > d[1][t])
return cout << -1 << endl , 0;
cout << min(d[0][t] - d[0][s] , d[1][t] - d[1][s]) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |