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

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 numbercrushi.

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 letter w 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).

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 smallestt.

Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 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 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个人,第i个人可以打给第CRUSH(i)个人然后说"Owww...f"(w重复t次),然后CRUSH(i)个人打给CRUSH(CRUSH(i))说"Owww...f"(w重复t-1次),重复这个步骤直到t=1,这时候停在的第j个人称作Joon-Joon,现在问有没有最小的t使得任意x开始呼喊,最后y是Joon-Joon,然后y开始呼喊时,x是Joon-Joon,若有输出最小的t,否则输出-1。


思路:找环,如果不能分成独立的环的时候输出-1,记录每个环的点数m,当m为偶数时候,它的t=m/2,否则t=m,然后求所有的t的最小公倍数就是答案。

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int a[105];
ll b[105];
bool vis[105], flag;
int dfs(int u, int s, int l){
    if(u == s){
        l = l+1;
        return l;
    }
    if(vis[u]&&u != s){
        return -1;
    }
    vis[u] = 1;
    if(a[u]==s) return dfs(a[u], s, l);
    else return dfs(a[u], s, l+1);
}
ll gcd(ll a, ll b){
    if(b==0) return a;
    return gcd(b, a%b);
}
int main()
{
    int n, i, j, k;
    scanf("%d", &n);
    for(i = 1;i <= n;i++) scanf("%d", &a[i]);
    flag = 1;
    int p = 0;
    for(i = 1;i <= n;i++){
        if(vis[i]) continue;
        vis[i] = 1;
        int tmp = dfs(a[i], i, 1);
        if(tmp != -1) {
            if(tmp%2==0) tmp/=2;
            b[p++] = (ll)tmp;
        }
        else {
            flag = 0;
            break;
        }
    }
    if(!flag) printf("-1\n");
    else{
        ll lcm, c = b[0], e;
        for(i = 0;i < p;i++){
            e = gcd(c, b[i]);
            lcm = c*b[i]/e;
            c = lcm;
        }
        printf("%d\n", lcm);
    }
    //for(i = 1;i <= n;i++) if(!vis[i]) flag = 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值