AlenAntonelli

problema 1

Aug 11th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct grafo {
  6.  
  7.     vector< vector<int> > ady;
  8.     vector<bool> visit;
  9.     int n, m;
  10.  
  11.     void leer()
  12.     {
  13.         cin>>n>>m;
  14.  
  15.         ady.resize( n+1 );
  16.         visit = vector<bool> (n+1, false);
  17.  
  18.         int a, b;
  19.         for (int i=0; i<m; i++)
  20.         {
  21.             cin>>a>>b;
  22.             ady[a].push_back(b);
  23.             ady[b].push_back(a);
  24.         }
  25.  
  26.  
  27.     }
  28.  
  29.     bool es_arbol = true;
  30.  
  31.     DFS( int nodo, int padre )
  32.     {
  33.         visit[nodo] = true;
  34.  
  35.         for (int i=0; i<ady[nodo].size(); i++)
  36.         {
  37.             int vecino = ady[nodo][i];
  38.  
  39.             cout<<padre<<" -> "<<nodo<<" -> "<<vecino<<endl;
  40.  
  41.             if ( !visit[vecino] )
  42.                 DFS(vecino,nodo);
  43.             else if (vecino != padre)
  44.                 es_arbol = false;
  45.         }
  46.     }
  47.  
  48.     void resp ()
  49.     {
  50.         if ( m != n-1 )
  51.             cout<<"NO";
  52.         else
  53.         {
  54.             DFS(1,0);
  55.             if (es_arbol && ady[1].size() )
  56.                 cout<<"SI";
  57.             else cout<<"NO";
  58.         }
  59.     }
  60.  
  61. };
  62.  
  63. int main ()
  64. {
  65.     grafo g;
  66.  
  67.     g.leer();
  68.     g.resp();
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment