fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. bool dfs(int s,const vector<vector<int>> &adj,vector<bool> &vis,vector<bool> &stack_n){
  4. vis[s]=true;
  5. stack_n[s]=true;
  6. for(int ngb:adj[s]){
  7. if(vis[ngb]==false){
  8. if(dfs(ngb,adj,vis,stack_n)){
  9. return true;
  10. break;
  11. }
  12. }else{
  13. if(stack_n[ngb]=true){
  14. return true;
  15. }
  16. }
  17. }
  18. stack_n[s]=false;
  19. return false;
  20. }
  21. int main(){
  22. int v,e;
  23. cin>>v>>e;
  24. vector<vector<int>> adj(v);
  25. for(int i=0;i<e;i++){
  26. int a;
  27. int b;
  28. cin>>a>>b;
  29. adj[a].push_back(b);
  30. }
  31. bool flag=false;
  32. vector<bool> vis(v,false);
  33. vector<bool> stack_n(v,false);
  34. for(int i=0;i<v;i++){
  35. if(vis[i]==false){
  36. if(dfs(i,adj,vis,stack_n)){
  37. flag=true;
  38. break;
  39. }
  40. }
  41. }
  42. if(flag){
  43. cout<<"cycle";
  44. }else{
  45. cout<<"no cycle";
  46. }
  47. }
Success #stdin #stdout 0.01s 5296KB
stdin
4 3

0 1
1 2
2 3
stdout
no cycle