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 repeated t 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 the Joon-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 y, x 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).
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.
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
4 2 3 1 4
3
4 4 4 4 4
-1
4 2 1 4 3
1
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 is 1.
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.
题意:有N个人,第 i 个人有一个 a[i],第 i 个人可以打电话给第 a[i] 个人,打了 t 次电话后终点为y,那么从 y 也要打 t 次电话之后终点为 i,问最少要打多少次电话才能让所有人满足这样的条件。不存在输出 -1.
解题思路:这样的一个个序列就是一个环,因为要让所有人满足这个条件,所以 x * m = t ,x是每一个环节自身的长度, m 是一个整数, t 是最后的答案,求出所有环的最小公倍数,就是答案。如果环的长度为偶数,那么存在着一个 y 可以使得 x 能打电话给 y 的同时, y 也能打电话给 x,这样长度可以除以2。判断存不存在的话,只有每一个点的入度都不为 0,它才有可能形成环,否则会不能形成环。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int N=200010;
const int INF=0x7FFFFFFF;
int a[105],f[105],visit[105];
int dfs(int x,int y)
{
if(visit[a[x]]) return y;
visit[a[x]]=1;
return dfs(a[x],y+1);
}
int gcd(int x,int y)
{
if(x>y) return gcd(y,x);
while(y%x)
{
int tmp=y%x;
y=x;
x=tmp;
}
return x;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(f,0,sizeof f);
memset(visit,0,sizeof visit);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
f[a[i]]++;
}
int flag=1;
for(int i=1;i<=n;i++)
if(!f[i]) flag=0;
if(!flag) printf("-1\n");
else
{
int xx=1;
for(int i=1;i<=n;i++)
{
if(visit[i]) continue;
visit[i]=1;
int yy=dfs(i,1);
xx=xx*yy/gcd(xx,yy);
}
if(xx&1) printf("%d\n",xx);
else printf("%d\n",xx/2);
}
}
return 0;
}