My code is working but not for all test cases.
What I am trying to do here is creating a 'boolean ifparent array' which holds the record of the path I am traversing through.
'boolean visited array' keeps record of all the visited vertices.
I am using a stack for DFS.
//v is no of vertex, adj[] is the adjacency matrix
bool isCyclic(int v, vector<int> adj[])
{
stack<int> st;
st.push(0);
vector<bool> visited(v, false);
vector<bool> 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;
}