AlenAntonelli

ayuda

May 20th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. //* Alen Antonelli 2018*/ https://www.hackerrank.com/challenges/components-in-graph/problem
  2. #include <iostream>
  3. #include <vector>
  4. #include <map>
  5. using namespace std;
  6.  
  7. struct grafo {
  8.     vector< vector<int> > adj;
  9.     map<int, int> mapa;
  10.     int N;
  11.    
  12.     int dar_id (int x)
  13.     {
  14.         if(mapa[x]==0)
  15.             mapa[x]=mapa.size();
  16.         return mapa[x];
  17.     }
  18.    
  19.     void leer()
  20.     {
  21.         cin>>N;
  22.        
  23.         adj.resize(N+1);
  24.        
  25.         int a, b;
  26.         int desde, hasta;
  27.        
  28.         for (int i=0; i<N; i++)
  29.         {
  30.             cin>>a>>b;
  31.             desde = dar_id(a);
  32.             hasta = dar_id(b);
  33.            
  34.             adj[desde].push_back(hasta);
  35.             adj[hasta].push_back(desde);
  36.         }
  37.     }
  38.    
  39.     void mostrar()
  40.     {
  41.         /*for(int i=1; i<=N; i++)
  42.         {
  43.             cout<< endl << i << "; ";
  44.             for(int j=1; j<=adj[i].size(); j++)
  45.                 cout<<adj[i][j]<<", ";
  46.         } */
  47.     }
  48. };
  49.  
  50. int main()
  51. {
  52.     grafo g;
  53.     g.leer();
  54.     g.mostrar();
  55.    
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment