Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- #define ll long long
- #define ld long double
- #define pb push_back
- #define fe first
- #define se second
- #define nl "\n"
- #define pp pair < ll , ll >
- #define sz(x) (ll)x.size()
- #define st(x) sort(x.begin(),x.end())
- #define rst(x) sort(x.rbegin(), x.rend())
- #define all(x) x.begin(),x.end()
- long double pi = 3.14159265358979323;
- const double EPS = 1e-12;
- const int N = 1e6 + 5;
- //Just A Gentle Reminder about MOD
- const int mod = 1e9 + 7;
- int vis[N];
- vector < ll > v[N];
- //Cycle in a directed edge
- bool cycle ;
- void dfs(ll s){
- vis[s] = 1;
- for(auto it : v[s]){
- if(!vis[it]){
- dfs(it);
- }
- else if(vis[it] == 1){
- cycle = 1;
- return ;
- }
- }
- vis[s] = 2;
- }
- int main()
- {
- fast;
- ll n , m;
- cin >> n >> m ;
- for(int i = 0 ; i < m ; i++ ){
- ll a , b;
- cin >> a >> b;
- v[a].pb(b);
- }
- dfs(1);
- if(cycle)cout << "Cycle Found\n";
- else cout << "NO";
- return 0;
- //It's not end it yet
- }
Advertisement
Add Comment
Please, Sign In to add comment