nrk009

cycledirectedgraph

Nov 4th, 2020
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  5. #define ll long long
  6. #define ld long double
  7. #define pb push_back
  8. #define fe first
  9. #define se second
  10. #define nl "\n"
  11. #define pp pair < ll , ll >
  12. #define sz(x) (ll)x.size()
  13. #define st(x) sort(x.begin(),x.end())
  14. #define rst(x) sort(x.rbegin(), x.rend())
  15. #define all(x) x.begin(),x.end()
  16. long double pi = 3.14159265358979323;
  17.  
  18. const double EPS = 1e-12;
  19. const int N = 1e6 + 5;
  20. //Just A Gentle Reminder about MOD
  21. const int mod = 1e9 + 7;
  22.  
  23. int vis[N];
  24. vector < ll > v[N];
  25. //Cycle in a directed edge
  26. bool cycle ;
  27.  
  28. void dfs(ll s){
  29.  
  30.     vis[s] = 1;
  31.    
  32.     for(auto it : v[s]){
  33.         if(!vis[it]){
  34.             dfs(it);
  35.         }
  36.         else if(vis[it] == 1){
  37.             cycle = 1;
  38.             return  ;
  39.         }
  40.     }
  41.     vis[s] = 2;
  42. }
  43.  
  44.  
  45. int main()
  46. {
  47.     fast;
  48.     ll n , m;
  49.     cin >> n >> m ;
  50.     for(int i = 0 ; i < m ; i++ ){
  51.         ll a , b;
  52.         cin >> a >> b;
  53.         v[a].pb(b);
  54.     }
  55.     dfs(1);
  56.     if(cycle)cout << "Cycle Found\n";
  57.     else cout << "NO";
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.    
  80.     return 0;
  81. //It's not end it yet
  82. }
Advertisement
Add Comment
Please, Sign In to add comment