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;
typedef long long ll;
typedef long double ld;
const int infint = 1e9;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e6 + 7;
int dist1[MAXN], dist2[MAXN], dist3[MAXN], n, m, s, d, a, b;
vector<int> G[MAXN];
void bfs(int s, int type)
{
if(type == 0)
dist1[s] = 0;
else
if(type == 1)
dist2[s] = 0;
else
dist3[s] = 0;
queue<int> q;
q.push(s);
while(!q.empty())
{
int st = q.front();
q.pop();
for (auto v : G[st])
{
if(type == 0 && dist1[v] == -1)
dist1[v] = dist1[st] + 1, q.push(v);
else
if(type == 1 && dist2[v] == -1)
dist2[v] = dist2[st] + 1, q.push(v);
else
if(type == 2 && dist3[v] == -1)
dist3[v] = dist3[st] + 1, q.push(v);
}
}
}
bool check(int u, int v)
{
if(dist1[v] < dist1[u] || dist2[v] < dist2[u])
return 0;
return 1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
memset(dist1, -1, sizeof dist1);
memset(dist2, -1, sizeof dist2);
memset(dist3, -1, sizeof dist3);
cin >> n >> m >> s >> d >> a >> b;
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
bfs(a, 0);
bfs(b, 1);
bfs(d, 2);
if(check(s, d) == 0)
return cout << -1, 0;
int L = 0, R = n + 1;
while(R - L > 1)
{
int mid = (L + R) >> 1;
bool c = 1;
for (int i = 1; i <= n; i++)
if(dist3[i] <= mid && check(s, i) == 0)
c = 0;
if(c)
L = mid;
else
R = mid;
}
cout << L;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |