fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int>adj_list[100];
  4. int visited[100];
  5. bool DFS(int currnode,int par)
  6. {
  7. visited[currnode]=1;
  8. for(int i=0;i<adj_list[currnode].size();i++)
  9. {
  10. int child=adj_list[currnode][i];
  11. if(!visited[child])
  12. {
  13. if(DFS(child,currnode))
  14. {
  15. return true;
  16. }
  17. else if(child!=par)
  18. {
  19. return true;
  20. }
  21. }
  22. }
  23. return false;
  24. }
  25. int main()
  26. {
  27. int node,edge;
  28. cin>>node>>edge;
  29. for(int i=1;i<=edge;i++)
  30. {
  31. int u,v;
  32. cin>>u>>v;
  33. adj_list[u].push_back(v);
  34. }
  35. bool foundcycle=false;
  36. for(int i=1;i<=node;i++)
  37. {
  38. if(!visited[i])
  39. {
  40. if(DFS(i,-1))
  41. {
  42. foundcycle=true;
  43. break;
  44. }
  45. }
  46. if(foundcycle)
  47. {
  48. cout<<"cycle"<<endl;
  49. }
  50. else
  51. {
  52. cout<<"not"<<endl;
  53. }
  54. }
  55. }
  56.  
Success #stdin #stdout 0s 5320KB
stdin
4 4
1 2
2 3
3 4
4 2
stdout
Standard output is empty