Codeforces Round#383C. 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 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 ttimes) 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
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 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个人,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。

分析:需要注意的是题目要求的是求出符合每个人的循环节,就是说如果有一个人不能打电话最后传回到自己那么答案就是-1。如果一个人始终是打给自己的,那么无论t只要多大只要大于1就都适合。然后就是求每个人最小是几个t打电话能打回自己,然后求这么多个t的最小公倍数。注意如果先乘两个数在除两个数的最大公约数的话会答案会在乘的时候溢出,导致答案错误。是要用循环模拟一下每个人的打电话过程就可以最终算出每个人的t了。

#include<bits/stdc++.h>
using namespace std;
int a[105], vis[105];
int main()
{
    int n;
    while(cin >> n)
    {
        int flag = 0;
        long long ans = 1;
        for(int i = 1; i <= n; i++)
            cin >> a[i];
        for(int i = 1; i <= n; i++)
        {
            memset(vis, 0, sizeof(vis));
            int next = a[i], cou = 1, st = i;
            vis[i]++;
            while(st != next)
            {
                vis[next]++;
                if(vis[next] == 2)
                {
                    flag = 1;
                    break;
                }
                cou++;
                next = a[next];
            }
            if(ans == -1)
            {
                if(cou % 2 == 0)
                    ans = cou / 2;
                else
                    ans = cou;
            }
            else
            {
                if(cou % 2 == 0)
                    cou = cou / 2;
                long long gcd = __gcd(ans, (long long)cou);
                ans = ans * cou / gcd;
            }
            if(flag == 1)
                break;
        }
        if(flag)
            cout << -1 << endl;
        else
            cout << ans << endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值