Advertisement
Alexvans

Llegando

Jul 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <climits>
  2. #include <deque>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. int main( )
  7. {
  8.    std::ios_base::sync_with_stdio(0);
  9.    std::cin.tie(0);
  10.    std::cout.tie(0);
  11.    int n, m;
  12.    std::cin >> n >> m;
  13.  
  14.    std::vector<int> adyacencia[n];
  15.    for (int i = 0; i < m; ++i) {
  16.       int x, y;
  17.       std::cin >> x >> y;
  18.       adyacencia[x-1].push_back(y-1);
  19.       adyacencia[y-1].push_back(x-1);
  20.    }
  21.  
  22.    std::deque<int> cola;
  23.    cola.push_back(0);
  24.    int acumulado = 0;
  25.    int dist[n][2];
  26.    for(int i = 0; i < n; i++) {
  27.       dist[i][0] = INT_MAX;
  28.       dist[i][1] = INT_MAX;
  29.    }
  30.    dist[0][0] = 0;
  31.    
  32.    for (int acumulado = 0; !cola.empty( ); ++acumulado) {
  33.       for (int i = cola.size( ); i != 0; --i, cola.pop_front( )) {
  34.          for (int w : adyacencia[cola.front( )]) {
  35.             if((acumulado) % 2 == 0 && dist[w][1] == INT_MAX) {
  36.                dist[w][1] = acumulado + 1;
  37.                cola.push_back(w);
  38.             }
  39.             else if((acumulado) % 2 != 0 && dist[w][0] == INT_MAX) {
  40.                dist[w][0] = acumulado + 1;
  41.                cola.push_back(w);
  42.             }
  43.          }
  44.       }
  45.    }
  46.    
  47.    int q;
  48.    std::cin >> q;
  49.    while(q--) {
  50.       int x, y;
  51.       std::cin >> x >> y;
  52.       std::cout << (dist[x-1][y % 2] <= y) << "\n";
  53.    }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement