Advertisement
lodha1503

Untitled

Aug 22nd, 2023
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. class Solution {
  2.   public:
  3.     bool dfs(vector<int> adj[],int &V,int &node,vector<int> &visited,vector<int> &pathVisited)
  4.     {
  5.         visited[node]=1;
  6.         pathVisited[node]=1;
  7.        
  8.         bool ans=false;
  9.        
  10.         for(auto x:adj[node])
  11.         {
  12.             if(visited[x]==1 && pathVisited[x]==1)
  13.                 ans=true;
  14.            
  15.             else if(visited[x]==0)
  16.             {
  17.                 ans = ans || dfs(adj,V,x,visited,pathVisited);
  18.                 pathVisited[x]=0;
  19.             }
  20.                
  21.                
  22.         }
  23.        
  24.        
  25.         if(ans==true)
  26.             return true;
  27.        
  28.         return false;
  29.     }
  30.    
  31.     bool isCyclic(int V, vector<int> adj[])
  32.     {
  33.         vector<int> visited(V,0);
  34.         vector<int> pathVisited(V,0);
  35.        
  36.         for(int i=0;i<V;i++)
  37.         {
  38.             if(visited[i]==0)
  39.             {
  40.                 if(dfs(adj,V,i,visited,pathVisited) == true)
  41.                     return true;
  42.             }
  43.         }
  44.        
  45.         return false;
  46.        
  47.     }
  48. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement