Submission #37201

#TimeUsernameProblemLanguageResultExecution timeMemory
37201aome007 (CEOI14_007)C++14
100 / 100
366 ms19956 KiB
#include <bits/stdc++.h> using namespace std; const int N = 200005; const int INF = 1e9; int n, m; int p0, p1, a[2]; int dis[2][N]; int f[N], dis2[N]; bool lose[N]; vector<int> G[N]; void bfs(int t) { for (int i = 1; i <= n; ++i) dis[t][i] = INF; queue<int> qu; dis[t][a[t]] = 0, qu.push(a[t]); while (qu.size()) { int u = qu.front(); qu.pop(); for (auto v : G[u]) { if (dis[t][v] == INF) { dis[t][v] = dis[t][u] + 1, qu.push(v); } } } } void dfs(int u) { f[u] = 1; for (auto v : G[u]) { if (dis[0][v] + 1 != dis[0][u] || dis[1][v] + 1 != dis[1][u]) continue; if (!f[v]) dfs(v); f[u] = max(f[u], f[v] + 1); } } int main() { ios::sync_with_stdio(false); cin >> n >> m; cin >> p0 >> p1 >> a[0] >> a[1]; for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; G[u].push_back(v), G[v].push_back(u); } bfs(0), bfs(1); for (int i = 1; i <= n; ++i) { if (dis[0][i] < dis[0][p0] || dis[1][i] < dis[1][p0]) lose[i] = 1; } if (dis[0][p0] == dis[1][p0]) { for (int i = 1; i <= n; ++i) { if (dis[0][i] == dis[0][p0] && dis[1][i] == dis[1][p0] && !f[i]) dfs(i); } for (int i = 1; i <= n; ++i) { if (f[i] > f[p0]) lose[i] = 1; } } for (int i = 1; i <= n; ++i) dis2[i] = -1; queue<int> qu; dis2[p1] = 0, qu.push(p1); while (qu.size()) { int u = qu.front(); qu.pop(); if (lose[u]) { cout << dis2[u] - 1; return 0; } for (auto v : G[u]) { if (dis2[v] == -1) { dis2[v] = dis2[u] + 1, qu.push(v); } } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...