Just check if cycle exit or not- c++


#1

Comment body goes h bool solve(int s, vector &visited,vector &order,vector adj[]){
visited[s] = 1;
order[s] = 1;
for(auto x:adj[s]){
if(!visited[x]){
bool c = solve(x,visited,order,adj);
if(c==true){
return true;
}
}
else if(order[x]){
return true;
}
}
order[s] = 0;
return false;
}
bool isCyclic(int V, vector adj[]) {
// code here
vector visited(V,0);
vector order(V,0);
for(int i=0;i<V;i++){
if(!visited[i]){
bool c = solve(i,visited,order,adj);
if(c==true){
return true;
}
}
}
return false;
}
int Solution::solve(int A, vector &B, vector &C) {
vector adj[A];
for(int i=0;i<B.size();i++){
adj[B[i]-1].push_back(C[i]-1);
}
if(isCyclic(A,adj)){
return 0;
}
return 1;
}ere.