Advertisement
keverman

Cycle detection

Feb 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. int V;
  2. std::vector<std::vector<int>> edges;
  3. std::vector<bool> visited, rec;
  4.  
  5. bool cycle_dfs(int u)
  6. {
  7.     if(!visited[u])
  8.     {
  9.         visited[u] = rec[u] = true;
  10.         for(int v : edges[u])
  11.             if((!visited[v] && cycle_dfs(v)) || rec[i])
  12.                 return true;
  13.         rec[i] = false;
  14.     }
  15.     return false;
  16. }
  17.  
  18. bool has_cycles()
  19. {
  20.     visited.resize(V, false), rec.resize(V, false);
  21.     for(int u = 0; u < V; u++)
  22.         if(cycle_dfs(u))
  23.             return true;
  24.  
  25.     return false;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement