GastonFontenla

Untitled

Jun 12th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 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.         int desde, hacia;
  16.         for(int i=0; i<aristas; i++)
  17.         {
  18.             cin >> desde >> hacia;
  19.             adj[desde].push_back(hacia);
  20.             adj[hacia].push_back(desde);
  21.         }
  22.     }
  23.    
  24.     void DFS(int n)
  25.     {
  26.         v[n] = true; ///Marco como visitado
  27.        
  28.         for(int i=0; i<adj[n].size(); i++)
  29.             if(v[adj[n][i]] == false)
  30.                 DFS(adj[n][i]);
  31.     }
  32.    
  33.     int resolver()
  34.     {
  35.         DFS(1);
  36.         int cant = 0;
  37.         for(int i=1; i<v.size(); i++)
  38.             if(v[i] == true)
  39.                 cant++;
  40.         return cant;
  41.     }
  42. };
  43.  
  44. int main()
  45. {
  46.     /**
  47.     Problema:
  48.     Dado un grafo, decir cuales son los nodos que son alcanzables desde el nodo 1.
  49.     **/
  50.    
  51.     Grafo g;
  52.     g.leer();
  53.    
  54.     cout << g.resolver() << endl;
  55.    
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment