# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
30123 | cdemirer | 007 (CEOI14_007) | C++14 | 1000 ms | 413620 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;
typedef long long ll;
typedef pair<ll, ll> llp;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
int N, M;
int Z, D, SV1, SV2;
vvi edges;
int sv1dists[200000], sv2dists[200000];
bool vis1[200000] = {0};
bool vis2[200000] = {0};
void bfs(int *dists, bool *vis, int x) {
queue<int> Q;
queue<int> Qt;
int d = 0;
Q.push(x);
while (!Q.empty()) {
while (!Q.empty()) {
int x = Q.front();
Q.pop();
dists[x] = d;
vis[x] = true;
for (int i = 0; i < edges[x].size(); i++) {
if (vis[edges[x][i]]) continue;
Qt.push(edges[x][i]);
}
}
while (!Qt.empty()) {
int x = Qt.front();
Qt.pop();
Q.push(x);
}
d++;
}
}
int moveit(int x) {
int cnt = 0;
while (true) {
bool fnd = false;
for (int i = 0; i < edges[x].size(); i++) {
int v = edges[x][i];
if (sv1dists[v] == sv1dists[x]-1 && sv1dists[v] == sv2dists[v]) {
fnd = true;
x = v;
break;
}
}
if (!fnd) return cnt;
else cnt++;
}
}
int main(int argc, char **argv) {
//freopen("input", "r", stdin);
//freopen("output", "w", stdout);
scanf("%d%d", &N, &M);
scanf("%d%d%d%d", &Z, &D, &SV1, &SV2);
Z--; D--; SV1--; SV2--;
edges.resize(N);
for (int i = 0; i < M; i++) {
int a, b; scanf("%d%d", &a, &b);
a--, b--;
edges[a].pb(b);
edges[b].pb(a);
}
bfs(sv1dists, vis1, SV1);
bfs(sv2dists, vis2, SV2);
int ans = -1;
ans = max(ans, min(sv1dists[D] - sv1dists[Z], sv2dists[D] - sv2dists[Z]));
if (ans != -1 && sv1dists[D] == sv2dists[D] && sv1dists[Z] == sv2dists[Z]) {
int a = moveit(D);
int b = moveit(Z);
if (a > b + ans) ans--;
}
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |