Glaas2

Recorrido_amplitud_grafo

Jul 12th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <deque>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int main( ) {
  9.    int n, m; // n vertices, m aristas
  10.    cin >> n >> m;
  11.  
  12.    vector<int> adyacencia[n];
  13.    for (int i = 0; i < m; ++i) {
  14.       char x, y;
  15.       cin >> x >> y;
  16.       adyacencia[x - 'A'].push_back(y - 'A');
  17.       adyacencia[y - 'A'].push_back(x - 'A');
  18.    }
  19.  
  20.     deque<int> cola = { 0 };
  21.     bool impresos[n]; fill_n(&impresos[0], n, false);
  22.  
  23.     do{
  24.         int procesar=cola.front();
  25.         cola.pop_front();
  26.  
  27.         if(impresos[procesar]==false)
  28.         {
  29.             cout<< char(procesar + 'A')<<" ";
  30.             impresos[procesar]= true;
  31.             for(int vecino: adyacencia[procesar])
  32.             {
  33.                 cola.push_back(vecino);
  34.             }
  35.         }
  36.  
  37.     }while(!cola.empty());
  38. }
Advertisement
Add Comment
Please, Sign In to add comment