# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
92921 | Flying_dragon_02 | 007 (CEOI14_007) | C++14 | 325 ms | 17248 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;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
const int N = 2e5 + 5;
int n, m, s, d, a, b, dista[N], distb[N], dist[N];
vector<int> graph[N];
queue<int> q;
int get(int f) {
int res = 0;
for(int i = 1; i <= N - 5; i++)
dist[i] = N;
dist[f] = 0;
q.push(f);
while(!q.empty()) {
int u = q.front();
q.pop();
res = max(res, dist[u]);
for(int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
if(dist[v] > dist[u] + 1 && dista[v] + 1 == dista[u] && distb[v] + 1 == distb[u]) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
return res;
}
int main() {
//freopen("test.inp", "r", stdin);
scanf("%d%d", &n, &m);
scanf("%d%d%d%d", &s, &d, &a, &b);
for(int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
graph[u].pb(v);
graph[v].pb(u);
}
for(int i = 1; i <= N - 5; i++) {
dista[i] = N;
distb[i] = N;
}
dista[a] = 0;
q.push(a);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
if(dista[v] > dista[u] + 1) {
dista[v] = dista[u] + 1;
q.push(v);
}
}
}
distb[b] = 0;
q.push(b);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
if(distb[v] > distb[u] + 1) {
distb[v] = distb[u] + 1;
q.push(v);
}
}
}
int w1 = dista[d] - dista[s], w2 = distb[d] - distb[s];
if(min(w1, w2) < 0) {
printf("-1");
exit(0);
}
int ans;
if(w1 != w2) {
printf("%d", min(w1, w2));
exit(0);
}
else {
int lmaoa = get(s), lmaob = get(d);
if(lmaoa + w1 >= lmaob)
ans = w1;
else
ans = w1 - 1;
}
if(ans < 0)
printf("-1");
else
printf("%d", ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |