Created
April 4, 2022 05:49
-
-
Save devil-cyber/5d64d91812689b2b2f4e9bfe919275f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<vector> | |
using namespace std; | |
class Graph{ | |
int V; | |
vector<int> *l; | |
public: | |
Graph(int v){ | |
V = v; | |
l = new vector<int>[V]; | |
} | |
void addEdge(int x, int y, bool undir=true){ | |
l[x].push_back(y); | |
if(undir) | |
l[y].push_back(x); | |
} | |
bool dfs(int src, vector<bool> &visted, int parent){ | |
visted[src]=true; | |
for(auto node : l[src]){ | |
if(!visted[node]){ | |
bool val = dfs(node, visted, src); | |
if(val==true) | |
return true; | |
} | |
else if(parent!=node) | |
return true; | |
} | |
return false; | |
} | |
}; | |
int main(){ | |
Graph g(3); | |
g.addEdge(0, 1); | |
g.addEdge(0, 2); | |
vector<bool> visted(3, false); | |
cout<<g.dfs(0, visted, -1)<<endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment