nrk009

cyleundirectedgraph

Nov 4th, 2020
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 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. bool vis[N];
  24. vector < ll > v[N];
  25. //Cycle in a directed edge
  26. bool cycle ;
  27. ll par[N];
  28.  
  29. void dfs(ll s , ll par){
  30.  
  31.     vis[s] = 1;
  32.    
  33.     for(auto it : v[s]){
  34.         if(it == par)continue;
  35.         if(!vis[it]){
  36.            // par[it] = s;
  37.             dfs(it,s);
  38.         }
  39.         else{
  40.             cycle = 1;
  41.             return  ;
  42.         }
  43.     }
  44.    // vis[s] = 2;
  45. }
  46.  
  47.  
  48. int main()
  49. {
  50.     fast;
  51.     ll n , m;
  52.     cin >> n >> m ;
  53.     for(int i = 0 ; i < m ; i++ ){
  54.         ll a , b;
  55.         cin >> a >> b;
  56.         v[a].pb(b);
  57.         v[b].pb(a);
  58.     }
  59.     dfs(1,-1);
  60.     if(cycle)cout << "Cycle Found\n";
  61.     else cout << "NO";
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.    
  84.     return 0;
  85. //It's not end it yet
  86. }
Advertisement
Add Comment
Please, Sign In to add comment