|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +#ifdef ONLINE_JUDGE |
| 4 | +#define DISABLE_STACK_SIZE_CHANGE |
| 5 | +#endif |
| 6 | +#ifndef DISABLE_STACK_SIZE_CHANGE |
| 7 | +#include <sys/resource.h> |
| 8 | +#endif |
| 9 | +typedef long long ll; |
| 10 | +const ll MOD = 1000000007; |
| 11 | +#define pb push_back |
| 12 | +#define ff first |
| 13 | +#define ss second |
| 14 | +#define nl "\n" |
| 15 | +#define set_bits(x) __builtin_popcountll(x) |
| 16 | +#define all(x) (x).begin(), (x).end() |
| 17 | +#define debug(x) cerr<<#x<<" "<<x<<endl; |
| 18 | +#define loop(i,a,b) for(int i=(a);i<(b);i++) |
| 19 | +#define print(x) for(auto it:(x)) cout<<(it)<<" "; cout<<endl; |
| 20 | +#define showadj for (auto it : adj) { cout << it.ff << " ->"; for (auto i : it.ss) cout << i << " "; cout << endl; } |
| 21 | +template<typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } |
| 22 | +template<typename T> T binpow(T base,T power,T mod){ ll ans=1; base = base % mod;while(power){if(power&1) ans=(ans*base)%mod; base=((base*base)%mod); power>>=1;}return ans;} |
| 23 | + |
| 24 | +bool dfs(int node,unordered_map<int,bool>&visi,unordered_map<int,vector<int>>&adj,int parent,unordered_map<int,bool>&dfsvisi) |
| 25 | +{ |
| 26 | + visi[node]=true; |
| 27 | + dfsvisi[node]=true; |
| 28 | + for(auto nbr:adj[node]) |
| 29 | + { |
| 30 | + if(!visi[nbr]) |
| 31 | + { |
| 32 | + bool ans=dfs(nbr,visi,adj,node,dfsvisi); |
| 33 | + if(ans)return true; |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + if(dfsvisi[nbr])return true; |
| 38 | + } |
| 39 | + } |
| 40 | + dfsvisi[node]=false; |
| 41 | + return false; |
| 42 | +} |
| 43 | + |
| 44 | +int main() |
| 45 | +{ |
| 46 | + ios_base::sync_with_stdio(0);cin.tie(0); |
| 47 | + #ifndef DISABLE_STACK_SIZE_CHANGE |
| 48 | + rlimit rlim; |
| 49 | + if (getrlimit(RLIMIT_STACK, &rlim) != 0) {return 1;} |
| 50 | + rlim.rlim_cur = 1024 * 1024 * 1024; |
| 51 | + if (setrlimit(RLIMIT_STACK, &rlim) != 0) {return 2;} |
| 52 | + #endif |
| 53 | + |
| 54 | + int n; |
| 55 | + cin>>n; |
| 56 | + unordered_map<int,vector<int>>adj; |
| 57 | + unordered_map<int,bool>visi; |
| 58 | + unordered_map<int,bool>dfsvisi; |
| 59 | + loop(i,0,n) |
| 60 | + { |
| 61 | + int u,v; |
| 62 | + cin>>u>>v; |
| 63 | + adj[u].pb(v); |
| 64 | + } |
| 65 | + loop(i,0,n+1) |
| 66 | + { |
| 67 | + if(!visi[i]) |
| 68 | + { |
| 69 | + bool ans=dfs(i,visi,adj,0,dfsvisi); |
| 70 | + if(ans) |
| 71 | + { |
| 72 | + cout<<"cycle"<<endl; |
| 73 | + return 0; |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + cout<<"no"<<endl; |
| 79 | +} |
| 80 | + |
0 commit comments