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 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).
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
Copy
4 2 3 1 4output
Copy
3input
Copy
4 4 4 4 4output
Copy
-1input
Copy
4 2 1 4 3output
Copy
1Note
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.
题意:
给定一串序列为num,第一个人喊x,第num[x]个人喊2,第num[num[x]]个人喊1,问能使得每个人喊x之后到达的人,喊x,又回到这个人的最小x。题意不是很好叙述...
思路:
要想满足题设的条件,每个人就都必须在一个环上,这样才有可能传递回去,如果有人不在环上,就一定不能形成。
当形成多个环时,我们就找每个环的长度,用vector存下来,对于偶数长度的环,最小可以是长度除2,可以保证再转回去,对于奇数长度的环,只能是自己到自己,然后把所有有可能的数取最小公倍数即可。
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int n;
int num[120];
bool vis[120];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
}
vector<int>v;
int ans=0;
for(int i=1;i<=n;i++)
{
if(vis[i]==false)
{
vis[i]=true;
if(num[i]==i)
{
v.push_back(1);
continue;
}
int now=num[i];
int cnt=1;
while(now!=i)
{
if(vis[now])
{
cnt=-1;
break;
}
vis[now]=true;
now=num[now];
cnt++;
}
if(cnt==-1)
{
ans=-1;
break;
}
else
{
v.push_back(cnt);
}
}
}
if(ans==-1)
printf("-1\n");
else
{
ans=1;
for(int i=v.size()-1;i>=0;i--)
{
if(v[i]%2==0)
v[i]/=2;
ans=ans*v[i]/__gcd(ans,v[i]);
}
printf("%d\n",ans);
}
}