Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool dfs(vector<int> adj[],int &V,int &node,vector<int> &visited,vector<int> &pathVisited)
- {
- visited[node]=1;
- pathVisited[node]=1;
- bool ans=false;
- for(auto x:adj[node])
- {
- if(visited[x]==1 && pathVisited[x]==1)
- ans=true;
- else if(visited[x]==0)
- {
- ans = ans || dfs(adj,V,x,visited,pathVisited);
- pathVisited[x]=0;
- }
- }
- if(ans==true)
- return true;
- return false;
- }
- bool isCyclic(int V, vector<int> adj[])
- {
- vector<int> visited(V,0);
- vector<int> pathVisited(V,0);
- for(int i=0;i<V;i++)
- {
- if(visited[i]==0)
- {
- if(dfs(adj,V,i,visited,pathVisited) == true)
- return true;
- }
- }
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement