# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
132330 | bogdan10bos | 007 (CEOI14_007) | C++14 | 365 ms | 17784 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/// 00:04 why?
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 2e5 + 5;
int N, M, S, D, A, B;
vector<int> edg[NMAX];
vector<int> BFS(int start)
{
vector<int> d(N + 1, 1 << 30);
d[start] = 0;
queue<int> q;
q.push(start);
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto nxt: edg[nod])
if(d[nxt] > d[nod] + 1)
{
d[nxt] = d[nod] + 1;
q.push(nxt);
}
}
return d;
}
int main()
{
//freopen("1.in", "r", stdin);
scanf("%d%d", &N, &M);
scanf("%d%d%d%d", &S, &D, &A, &B);
for(int i = 1; i <= M; i++)
{
int x, y;
scanf("%d%d", &x, &y);
edg[x].push_back(y);
edg[y].push_back(x);
}
auto dS = BFS(S);
auto dD = BFS(D);
if(dD[A] + 1 <= dS[A] || dD[B] + 1 <= dS[B])
{
printf("-1\n");
exit(0);
}
int timp = -1;
if(dD[A] - dS[A] != dD[B] - dS[B])
timp = min(dD[A] - dS[A], dD[B] - dS[B]);
else
{
auto dA = BFS(A);
auto dB = BFS(B);
int maxStepsS = 0;
for(int i = 1; i <= N; i++)
if(dA[i] == dB[i] && dA[i] + dS[i] == dS[A])
maxStepsS = max(maxStepsS, dS[i]);
int maxStepsD = 0;
for(int i = 1; i <= N; i++)
if(dA[i] == dB[i] && dA[i] + dD[i] == dD[A])
maxStepsD = max(maxStepsD, dD[i]);
timp = min(dD[A] - dS[A], dD[B] - dS[B]);
maxStepsD -= (dD[A] - dS[A]);
if(maxStepsS < maxStepsD) timp--;
}
printf("%d\n", timp);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |