Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define MAX 100003
- vector<int> ady[MAX];
- bool visitado[MAX];
- int previ[MAX];
- int a , v;
- void dfs(){
- int pasos=0, maxi , actual, fin;
- set<int>verticesporvisitar;
- for (int i=1;i<=v;i++)
- verticesporvisitar.insert(i);
- memset( visitado , 0 , sizeof( visitado ) ); //inicializamos como no visitado
- stack<int> S;
- while(!verticesporvisitar.empty()){
- int ini=*(verticesporvisitar.begin());
- S.push( ini );
- previ[ ini ] = -1;
- visitado[ ini ] = true;
- int nvisit=1;
- maxi = 0;
- while( !S.empty() ){
- actual = S.top(); S.pop();
- verticesporvisitar.erase(actual);
- //if (verticesporvisitar.empty())
- // break;
- printf("actual: %d",actual);
- for(int i = 0 ; i < ady[ actual ].size() ; ++i ){
- int v = ady[ actual ][ i ];
- if( !visitado[ v ]){
- pasos++;
- printf(" %d %d pasos: %d\n",actual, v,pasos);
- visitado[ v ] = true;
- verticesporvisitar.erase(v);
- previ[ v ] = actual;
- S.push( v );
- }
- else{
- printf(" %d %d SI\n",actual, v);
- return;
- }
- }
- }
- }
- if (pasos!=a)
- printf("SI\n");
- else
- printf("NO\n");
- printf("pasos: %d\n",pasos);
- }
- int main(){
- int x , y;
- scanf("%d %d" , &v , &a );
- for(int i = 1 ; i <= a ; ++i ){
- scanf("%d %d" , &x , &y );
- ady[ x ].push_back( y );
- //ady[ y ].push_back( x );
- }
- dfs();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment