Advertisement
Manioc

cycle find

Nov 16th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<vector<int> > grafo;
  6. vector<pair<int, int> > env;
  7.  
  8. bool cf(int start){
  9.     env[start].second = true;
  10.     env[start].first = true;
  11.     for(int i = 0;i < grafo[start].size(); i++){
  12.         if(!env[grafo[start][i]].first){
  13.             if (cf(grafo[start][i])) return true;
  14.         } if (env[grafo[start][i]].second) return true;
  15.     }
  16.     env[start].second = false;
  17.     return false;
  18. }
  19.    
  20. string resp(int N){
  21.     for(int i = 1; i <= N; i++){
  22.         if(!env[i].first){
  23.             if(cf(i)) return "SIM";
  24.         }
  25.     }
  26.     return "NAO";
  27. }
  28. int main(){
  29.     int cases, N, M;
  30.     scanf("%d", &cases);
  31.     while(cases--){
  32.         scanf("%d %d", &N, &M);
  33.         grafo.assign(N+1, vector<int> ());
  34.         env.assign(N+1, make_pair(0,0));
  35.         while(M--){
  36.             int A, B;
  37.             scanf("%d %d", &A, &B);
  38.             grafo[A].push_back(B);
  39.         }
  40.         cout << resp(N) << endl;
  41.         grafo.clear();
  42.         env.clear();
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement