Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- void addEdge(vector<int> adj[], int u, int v)
- {
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- int cycle(vector<int> adj[],vector<int> &vis,int x,int parent)
- {
- vis[x]=1;
- for(int i:adj[x])
- {
- if(vis[i]==0)
- {
- if(cycle(adj,vis,i,x))
- return 1;
- }
- else if(vis[i]==1 && i!=parent)
- return 1;
- }
- return 0;
- }
- void printGraph(vector<int> adj[], int V)
- {
- for (int v = 0; v < V; ++v)
- {
- cout << "Adjacency list of vertex "
- << v << "\n";
- for (auto x : adj[v])
- cout << x <<" ";
- cout<<"\n";
- }
- }
- int main()
- {
- int V = 5;
- vector<int> adj[V];
- addEdge(adj,0,1);
- addEdge(adj,0,2);
- addEdge(adj,0,3);
- addEdge(adj,2,3);
- addEdge(adj,3,4);
- //printGraph(adj,V);
- vector<int> vis(V,0);
- int cyc=0;
- for(int i=0;i<V;i++)
- {
- if(vis[i]==0)
- {
- if(cycle(adj,vis,i,-1))
- {
- cout<<"The graph contains a cycle";
- cyc=1;
- break;
- }
- }
- }
- if(cyc==0)
- cout<<"The graph does not contain a cycle";
- return 0;
- }
- https://www.last.fm/user/jd19045
- https://fliphtml5.com/homepage/dygjb
- https://peatix.com/user/10949624
- https://sketchfab.com/ipritskerec
- https://slashdot.org/~wokavet849
- https://flipboard.com/@wokavetwhecode
- https://my.archdaily.com/us/@wokavet849
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement