spider68

detect cycle in directed by color

May 24th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. bool dfs(int i,bool a[],bool b[],bool c[],vector<int>adj[])
  2. {
  3.     a[i]=false;
  4.     b[i]=true;
  5.     for(auto it=adj[i].begin();it!=adj[i].end();it++)
  6.     {
  7.         if(b[*it])return true;
  8.         if(a[*it]&&dfs(*it,a,b,c,adj))return true;
  9.     }
  10.     b[i]=false;
  11.     c[i]=true;
  12.     return false;
  13. }
  14.  
  15. bool isCyclic(int V, vector<int> adj[])
  16. {
  17.     bool a[V]={false};
  18.     bool b[V]={false};
  19.     bool c[V]={false};
  20.     for(int i=0;i<V;i++)a[i]=true;
  21.     for(int i=0;i<V;i++)
  22.     {
  23.         if(a[i])
  24.         {
  25.             if(dfs(i,a,b,c,adj))return true;
  26.         }
  27.     }
  28.     return false;
  29. }
Add Comment
Please, Sign In to add comment