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 numbercrushi.
![](https://i-blog.csdnimg.cn/blog_migrate/34be21143dcfc40b4667874b41d339df.png)
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 callscrushx and says: "Oww...wwf" (the letterw is repeated t times) and cuts off the phone immediately. Ift > 1 thencrushx callscrushcrushx and says: "Oww...wwf" (the letterw is repeated t - 1 times) 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 smallestt (t ≥ 1) such that for each personx, ifx starts some round andy becomes the Joon-Joon of the round, then by starting fromy,x would become the Joon-Joon of the round. Find sucht 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 ofi-th person's crush.
If there is no t satisfying the condition, print-1. Otherwise print such smallestt.
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 ifx 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 whenx is4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
题意:
n个人,x可以打电话给crushx 然后说: "Oww...wwf" (w重复t次),如果t > 1 那么crushx 打给crushcrushx 然后说: "Oww...wwf" (w重复t-1次 ),循环下去直到t=1,到达的人被称为Joon-Joon。求最小的t,使得任意x开始,存在y是Joon-Joon,而当 y开始打电话时,x是Joon-Joon。没有则输出-1。
思路:
求环。存在偶数环则环的大小除2。最后求所有环的最小公倍数。
如果存在一个数不在环里则输出-1。
注意爆int。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn =105;
int data[maxn];
int first[maxn],nxt[maxn];
int u[maxn],v[maxn];
int vis[maxn];
int n;
int gst;
int dfs(int st,int len){
vis[st]=1;
int i,j;
int k=first[st];
while(k!=-1){
if(vis[v[k]]==0)
return dfs(v[k],len+1);
else if(vis[v[k]]!=0&&v[k]==gst)
return len;
else
break;
k=nxt[k];
}
return -1;
}
__int64 lcm (__int64 m,__int64 n){
__int64 f1=m*n;
__int64 flag;
while(n>0){
flag=m%n;
m=n;
n=flag;
}
return f1/m;
}
int main(){
scanf("%d",&n);
int i,j;
for(i=1;i<=n;i++)
first[i]=-1;
for(i=1;i<=n;i++){
scanf("%d",&data[i]);
u[i]=i;
v[i]=data[i];
nxt[i]=first[u[i]];
first[u[i]]=i;
}
int ans =1;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++){
int flen;
if(vis[i]==0){
gst=i;
flen=dfs(i,1);
}
if(flen==-1){
ans=-1;
break;
}
if(flen%2==0)
flen/=2;
ans=lcm(ans,flen);
}
printf("%d\n",ans);
return 0;
}
发烧,噩梦做了一晚上。起床看排名的时候,感觉字都是绿色的 0 0