karlangadas

mal

Feb 5th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MAX 100003
  6. vector<int> ady[MAX];
  7. bool visitado[MAX];  
  8. int previ[MAX];
  9. int  a , v;
  10.  
  11. void dfs(){
  12.  
  13.     int pasos=0, maxi , actual, fin;
  14.     set<int>verticesporvisitar;
  15.     for (int i=1;i<=v;i++)
  16.         verticesporvisitar.insert(i);
  17.     memset( visitado , 0 , sizeof( visitado ) );         //inicializamos como no visitado
  18.     stack<int> S;
  19.     while(!verticesporvisitar.empty()){
  20.         int ini=*(verticesporvisitar.begin());
  21.         S.push( ini );
  22.         previ[ ini ] = -1;
  23.         visitado[ ini ] = true;
  24.         int nvisit=1;
  25.         maxi = 0;
  26.    
  27.         while( !S.empty() ){
  28.            actual = S.top(); S.pop();
  29.            verticesporvisitar.erase(actual);
  30.             //if (verticesporvisitar.empty())
  31.             //  break;
  32.             printf("actual: %d",actual);
  33.            
  34.            for(int i = 0 ; i < ady[ actual ].size() ; ++i ){
  35.               int v = ady[ actual ][ i ];
  36.               if( !visitado[ v ]){                  
  37.                  pasos++;
  38.                  printf(" %d %d pasos: %d\n",actual, v,pasos);
  39.                  visitado[ v ] = true;
  40.                  verticesporvisitar.erase(v);
  41.                  previ[ v ] = actual;
  42.                  S.push( v );
  43.                
  44.               }
  45.               else{
  46.                 printf(" %d %d SI\n",actual, v);
  47.                 return;
  48.               }
  49.            }
  50.         }
  51.         }
  52.     if (pasos!=a)
  53.         printf("SI\n");
  54.     else
  55.         printf("NO\n");
  56.     printf("pasos: %d\n",pasos);
  57. }
  58.  
  59. int main(){
  60.  
  61.     int  x , y;
  62.     scanf("%d %d" , &v , &a );
  63.    
  64.     for(int i = 1 ; i <= a ; ++i ){
  65.         scanf("%d %d" , &x , &y );
  66.         ady[ x ].push_back( y );
  67.         //ady[ y ].push_back( x );
  68.     }
  69.  
  70.     dfs();
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment