Advertisement
wokavet849

Tubewisp undirected cycles

Jan 11th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. void addEdge(vector<int> adj[], int u, int v)
  4. {
  5. adj[u].push_back(v);
  6. adj[v].push_back(u);
  7. }
  8. int cycle(vector<int> adj[],vector<int> &vis,int x,int parent)
  9. {
  10. vis[x]=1;
  11. for(int i:adj[x])
  12. {
  13. if(vis[i]==0)
  14. {
  15. if(cycle(adj,vis,i,x))
  16. return 1;
  17. }
  18. else if(vis[i]==1 && i!=parent)
  19. return 1;
  20. }
  21. return 0;
  22. }
  23. void printGraph(vector<int> adj[], int V)
  24. {
  25. for (int v = 0; v < V; ++v)
  26. {
  27. cout << "Adjacency list of vertex "
  28. << v << "\n";
  29. for (auto x : adj[v])
  30. cout << x <<" ";
  31. cout<<"\n";
  32. }
  33. }
  34. int main()
  35. {
  36. int V = 5;
  37. vector<int> adj[V];
  38. addEdge(adj,0,1);
  39. addEdge(adj,0,2);
  40. addEdge(adj,0,3);
  41. addEdge(adj,2,3);
  42. addEdge(adj,3,4);
  43. //printGraph(adj,V);
  44. vector<int> vis(V,0);
  45. int cyc=0;
  46. for(int i=0;i<V;i++)
  47. {
  48. if(vis[i]==0)
  49. {
  50. if(cycle(adj,vis,i,-1))
  51. {
  52. cout<<"The graph contains a cycle";
  53. cyc=1;
  54. break;
  55. }
  56. }
  57. }
  58. if(cyc==0)
  59. cout<<"The graph does not contain a cycle";
  60. return 0;
  61. }
  62. https://www.last.fm/user/jd19045
  63. https://fliphtml5.com/homepage/dygjb
  64. https://peatix.com/user/10949624
  65. https://sketchfab.com/ipritskerec
  66. https://slashdot.org/~wokavet849
  67. https://flipboard.com/@wokavetwhecode
  68. https://my.archdaily.com/us/@wokavet849
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement