# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
155933 | Akashi | 007 (CEOI14_007) | C++14 | 434 ms | 17912 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.
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool viz[200005];
int s, d, a, b;
int da[200005], db[200005], ds[200005], dd[200005];
queue <int> q;
vector <int> v[200005];
void bfs(int nod, int d[]){
for(int i = 1; i <= n ; ++i) d[i] = 1e9;
memset(viz, 0, sizeof(viz));
q.push(nod);
d[nod] = 0; viz[nod] = 1;
while(!q.empty()){
int nod = q.front();
q.pop();
for(auto it : v[nod]){
if(viz[it]) continue ;
q.push(it);
viz[it] = 1;
d[it] = d[nod] + 1;
}
}
}
int main()
{
// freopen("1.in", "r", stdin);
scanf("%d%d", &n, &m);
scanf("%d%d%d%d", &s, &d, &a, &b);
int x, y;
for(int i = 1; i <= m ; ++i){
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
bfs(a, da);
bfs(b, db);
bfs(s, ds);
bfs(d, dd);
if(dd[b] < ds[b] || dd[a] < ds[a]){
printf("-1");
return 0;
}
int tmp = -1;
if(dd[b] - ds[b] != dd[a] - ds[a])
tmp = min(dd[b] - ds[b], dd[a] - ds[a]);
else{
int dif = min(dd[b] - ds[b], dd[a] - ds[a]), nr1 = 0, nr2 = 0;
for(int i = 1; i <= n ; ++i)
if(da[i] == db[i] && da[i] + ds[i] == ds[a]) nr1 = max(nr1, ds[i]);
for(int i = 1; i <= n ; ++i)
if(da[i] == db[i] && da[i] + dd[i] == dd[a]) nr2 = max(nr2, dd[i]);
if(nr1 > nr2 - dif) tmp = min(dd[b] - ds[b], dd[a] - ds[a]);
else tmp = min(dd[b] - ds[b], dd[a] - ds[a]) - 1;
}
printf("%d", tmp);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |