Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan

来源:互联网 发布:双色球数据分据库 编辑:程序博客网 时间:2024/05/23 19:43
C. Arpa's loud Owf and Mehrdad's evil plan
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

As you have noticed, there are lovely girls in Arpa’s land.

People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.

Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.

The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeatedt times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called theJoon-Joon of the round. There can't be two rounds at the same time.

Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from yx would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.

Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).

Input

The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.

The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush.

Output

If there is no t satisfying the condition, print -1. Otherwise print such smallest t.

Examples
input
42 3 1 4
output
3
input
44 4 4 4
output
-1
input
42 1 4 3
output
1
Note

In the first sample suppose t = 3.

If the first person starts some round:

The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is1.

The process is similar for the second and the third person.

If the fourth person starts some round:

The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.

In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.


题目大意:第i个人能够呼叫a[i]这个人,然后不停地呼叫,现在定义了一个t,指的是从某个人开始进行呼叫,然后t次呼叫后到达一个人,之后在呼叫t次,到达第一次呼叫的那个人,现在要求出从任意一个开始的t的最小值
解题思路:由于这个t指的是从任意一个开始,所以他应该所有环的最小公倍数,然后环还是很好找的,但是若跑了任意一个环得到的长度是偶数,意味着可以除2
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;typedef long long LL;int cnt,ans;vector<int> tu[105];int check[105],n;int GCD(int x, int y){if(y == 0) return x;else return GCD(y, x % y);}int LCM(int x, int y){return x / GCD(x, y) * y;}void dfs(int x,int flag){//cnt++;int k=tu[x][0];check[x]=1;if(check[k]==1){if(k!=flag)cnt=-1;if(x==flag&&cnt==1)cnt++;return;}else{cnt++;dfs(k,flag);}}int main(){cin>>n;int i,x;for(i=1;i<=n;i++){cin>>x;tu[i].push_back(x);}ans=1;for(i=1;i<=n;i++){memset(check,0,sizeof(check));cnt=1;dfs(i,i);if(cnt<0){cout<<-1<<endl;return 0;}if(cnt%2==0)cnt/=2;ans=LCM(ans,cnt);}cout<<ans<<endl;return 0;}


阅读全文
0 0
原创粉丝点击