Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- bool a[101][101];
- int n,res,ans;
- bool t;
- bool pr()
- {
- for (int i=1;i<=n;i++)
- {
- for (int j=1;j<=n;j++)
- if (a[j][i]) res++;
- if (res!=1) return false;
- res=0;
- }
- return true;
- }
- int dfs(int x,int y)
- {
- //cout<<res<<endl;
- if (x==y && t) return 0;// Если поставить сюда return (res-1) тогда не работает
- res++;
- for (int i=1;i<=n;i++)
- if (a[x][i]){t=true; dfs(i,y);}
- }
- int main()
- {
- cin>>n;
- for (int i=1;i<=n;i++)
- {
- int x;
- cin>>x;
- a[i][x]=true;
- }
- if (!pr()){cout<<"-1"; return 0;}
- ans=1;
- for (int i=1,d=0;i<=n;i++)
- {
- res=0; t=false;
- dfs(i,i);
- d=res;
- if (d%2==0) d/=2;
- // cout<<d<<"%%"<<endl;
- ans=(ans*d)/__gcd(ans,d);
- }
- cout<<ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement