# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
887977 | 2023-12-15T16:13:36 Z | qrno | Event Hopping (BOI22_events) | C++14 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; #define int long long vector<int> bfs(int source, vector<vector<int>> const& G) { int N = size(G); vector<int> dist(N, -1); queue<int> Q; dist[source] = 0; Q.push(source); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (auto u : G[v]) { if (dist[u] == -1) { Q.push(u); dist[u] = dist[v]+1; } } } return dist; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<pair<int, int>> E(N); for (auto &[x, y] : E) cin >> x >> y; vector<vector<int>> G(N); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i == j) continue; if (E[j].first <= E[i].second && E[i].second <= E[j].second) G[i].push_back(j); } } while (Q--) { int v, u; cin >> v >> u; v--, u--; auto dist = bfs(v, G); if (dist[u] == -1) cout << "impossible" << endl; else cout << dist[u] << endl; } }