title: detect cycle in directed graph with non-recursive dfs
body: <p>My code is working but not for all test cases.</p>
  
  <p>What I am trying to do here is creating a 'boolean ifparent array' which holds the record of the path I am traversing through.</p>
  
  <p>'boolean visited array' keeps record of all the visited vertices.</p>
  
  <p>I am using a stack for DFS.</p>
  
  <pre><code>//v is no of vertex, adj[] is the adjacency matrix
  bool isCyclic(int v, vector&lt;int&gt; adj[])
  {
      stack&lt;int&gt; st;
      st.push(0);
  
      vector&lt;bool&gt; visited(v, false);
      vector&lt;bool&gt; ifparent(v, false);
      int flag= 0;
  
      int s;
      while(!st.empty()){
          s= st.top();
          ifparent[s]= true;
          visited[s]=true;
          flag=0;
  
          for(auto i: adj[s]){
              if(visited[i]){
                  if(ifparent[i])
                      return true;
              }
              else if(!flag){
                  st.push(i);
                  flag= 1;
              }
          }
  
          if(!flag){
              ifparent[s]= false;
              st.pop();
          }
      }
  
      return false;
  
  }
  </code></pre>