Submission #101175

#TimeUsernameProblemLanguageResultExecution timeMemory
101175square1001007 (CEOI14_007)C++14
0 / 100
375 ms15748 KiB
#include <queue> #include <vector> #include <iostream> using namespace std; vector<int> shortest_path(int src, vector<vector<int> > &G) { int N = G.size(); vector<int> dist(N, -1); queue<int> que; que.push(src); dist[src] = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i : G[u]) { if (dist[i] == -1) { dist[i] = dist[u] + 1; que.push(i); } } } return dist; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; int S, D, A, B; cin >> S >> D >> A >> B; --S, --D, --A, --B; vector<vector<int> > G(N); for (int i = 0; i < M; ++i) { int a, b; cin >> a >> b; --a, --b; G[a].push_back(b); G[b].push_back(a); } vector<int> da = shortest_path(A, G); vector<int> db = shortest_path(B, G); int s1 = da[D] - da[S]; int s2 = db[D] - db[S]; int ans = max(s1, s2); if (ans < 0) cout << -1 << endl; else cout << ans << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...