Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <climits>
- #include <deque>
- #include <iostream>
- #include <vector>
- int main( )
- {
- std::ios_base::sync_with_stdio(0);
- std::cin.tie(0);
- std::cout.tie(0);
- int n, m;
- std::cin >> n >> m;
- std::vector<int> adyacencia[n];
- for (int i = 0; i < m; ++i) {
- int x, y;
- std::cin >> x >> y;
- adyacencia[x-1].push_back(y-1);
- adyacencia[y-1].push_back(x-1);
- }
- std::deque<int> cola;
- cola.push_back(0);
- int acumulado = 0;
- int dist[n][2];
- for(int i = 0; i < n; i++) {
- dist[i][0] = INT_MAX;
- dist[i][1] = INT_MAX;
- }
- dist[0][0] = 0;
- for (int acumulado = 0; !cola.empty( ); ++acumulado) {
- for (int i = cola.size( ); i != 0; --i, cola.pop_front( )) {
- for (int w : adyacencia[cola.front( )]) {
- if((acumulado) % 2 == 0 && dist[w][1] == INT_MAX) {
- dist[w][1] = acumulado + 1;
- cola.push_back(w);
- }
- else if((acumulado) % 2 != 0 && dist[w][0] == INT_MAX) {
- dist[w][0] = acumulado + 1;
- cola.push_back(w);
- }
- }
- }
- }
- int q;
- std::cin >> q;
- while(q--) {
- int x, y;
- std::cin >> x >> y;
- std::cout << (dist[x-1][y % 2] <= y) << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement