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