Advertisement
GastonFontenla

Untitled

Jun 12th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct Grafo
  7. {
  8.     vector <vector <int> > adj;
  9.     vector <bool> v; ///Vector de visitados
  10.     int nodos, aristas;
  11.     void leer()
  12.     {
  13.         cin >> nodos >> aristas;
  14.         adj.resize(nodos+1);
  15.         v = vector <bool> (nodos+1, false);
  16.         int desde, hacia;
  17.         for(int i=0; i<aristas; i++)
  18.         {
  19.             cin >> desde >> hacia;
  20.             adj[desde].push_back(hacia);
  21.             adj[hacia].push_back(desde);
  22.         }
  23.     }
  24.  
  25.     void BFS(int n)
  26.     {
  27.         v[n] = true; ///Marco como visitado
  28.  
  29.         for(int i=0; i<adj[n].size(); i++)
  30.             if(v[adj[n][i]] == false)
  31.                 DFS(adj[n][i]);
  32.     }
  33.  
  34.     int resolver()
  35.     {
  36.         DFS(1);
  37.         int cant = 0;
  38.         for(int i=1; i<v.size(); i++)
  39.             if(v[i] == true)
  40.                 cant++;
  41.         return cant;
  42.     }
  43. };
  44.  
  45. int main()
  46. {
  47.     /**
  48.     Problema:
  49.     Dado un grafo, decir cuales son los nodos que son alcanzables desde el nodo 1.
  50.     **/
  51.  
  52.     Grafo g;
  53.     g.leer();
  54.  
  55.     cout << g.resolver() << endl;
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement