# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
535267 | LucaDantas | 007 (CEOI14_007) | C++17 | 305 ms | 27876 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;
constexpr int maxn = 2e5+10;
vector<int> g[maxn];
int dist[2][maxn];
void bfs(int k, int s) {
dist[k][s] = 0;
queue<int> q;
q.push(s);
while(q.size()) {
int u = q.front(); q.pop();
for(int v : g[u])
if(dist[k][v] > dist[k][u]+1)
dist[k][v] = dist[k][u]+1, q.push(v);
}
}
bool mark[maxn];
void dfs(int u, int& mn) {
mark[u] = 1;
mn = min(mn, dist[0][u]);
for(int v : g[u])
if(!mark[v] && dist[0][v] == dist[1][v]) dfs(v, mn);
}
int main() {
int n, m; scanf("%d %d", &n, &m);
int s, t, a, b; scanf("%d %d %d %d", &s, &t, &a, &b);
for(int i = 0; i < m; i++) {
int u, v; scanf("%d %d", &u, &v);
g[u].push_back(v); g[v].push_back(u);
}
memset(dist, 0x3f, sizeof dist); // já coloco infinito pra todo mundo
bfs(0, a);
bfs(1, b);
int especial = 0;
if(dist[0][s] == dist[1][s] && dist[0][t] == dist[1][t]) {
memset(mark, 0, sizeof mark);
int S = 0x3f3f3f3f; dfs(s, S);
memset(mark, 0, sizeof mark);
int T = 0x3f3f3f3f; dfs(t, T);
if(S > T) especial = 1;
}
printf("%d\n", max(-1, min(dist[0][t] - dist[0][s], dist[1][t] - dist[1][s]) - especial));
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |