CF741A.Arpa's loud Owf and Mehrdad's evil plan

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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 crush__x and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crush__x calls crush__crush__x 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 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. crush__i = i).

正如你所注意到的,阿帕(Arpa)的国度里有许多可爱的女孩。

阿帕国度中的人编号从 11 到 nn。每个人恰好有一个暗恋对象,第 ii 个人的暗恋对象是编号为 crushi\text{crush}_i 的人。

某天,阿帕站在宫殿顶端高声大喊“Owf”,于是阿帕国度中一场有趣的游戏就此开始。游戏规则如下:

游戏由若干轮组成。假设某人 xx 想发起一轮游戏,他便拨通 crushx\text{crush}_x 的电话,并说:“Oww...wwf”(其中字母 w 重复 tt 次),然后立即挂断。若 t>1t > 1,则 crushx\text{crush}_x 立即拨通 crushcrushx\text{crush}_{\text{crush}_x} 的电话,并说:“Oww...wwf”(其中字母 w 重复 t−1t - 1 次),然后立即挂断。如此继续,直到某人收到一条“Owf”(即 t=1t = 1)。此人被称为该轮的“琼-琼(Joon-Joon)”。任意时刻至多只能进行一轮游戏。

梅赫拉德(Mehrdad)怀有一个邪恶计划,想让游戏变得更有趣:他希望找出最小的 tt(满足 t≥1t \geq 1),使得对每个人 xx,若 xx 发起一轮游戏且最终 yy 成为该轮的琼-琼,则当由 yy 发起一轮游戏时,xx 必然成为该轮的琼-琼。请为梅赫拉德找出满足条件的 tt(若存在)。

阿帕国度中一个奇特的事实是:某人可以暗恋自己(即 crushi=i\text{crush}_i = 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 crush__i (1 ≤ crush__i ≤ n) — the number of i-th person's crush.

输入的第一行包含一个整数 nn(1≤n≤1001 \leq n \leq 100)—— 表示 Arpa 的领地里的人数。

第二行包含 nn 个整数,其中第 ii 个数为 crushicrush_i(1≤crushi≤n1 \leq crush_i \leq n)—— 表示第 ii 个人的暗恋对象的编号。

输出格式

If there is no t satisfying the condition, print -1. Otherwise print such smallest t.

若不存在满足条件的 tt,则输出 −1-1;否则输出满足条件的最小 tt。

输入输出样例

  • 输入#1

    4
    2 3 1 4

    输出#1

    3
  • 输入#2

    4
    4 4 4 4

    输出#2

    -1
  • 输入#3

    4
    2 1 4 3

    输出#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.

在第一个样例中,假设 t=3t = 3。

若第一人发起某一轮:

第一人呼叫第二人并说 “Owwwf”,接着第二人呼叫第三人并说 “Owwf”,然后第三人呼叫第一人并说 “Owf”,于是第一人成为该轮的 Joon-Joon。因此当 x=1x = 1 时,条件满足。

对第二人和第三人发起的情况,过程类似。

若第四人发起某一轮:

第四人呼叫自己并说 “Owwwf”,接着再次呼叫自己并说 “Owwf”,然后再一次呼叫自己并说 “Owf”,于是第四人成为该轮的 Joon-Joon。因此当 x=4x = 4 时,条件满足。

在最后一个样例中,若第一人发起一轮,则第二人成为 Joon-Joon;反之亦然。

输入解题思路,AI测评打分。不知道怎么写?

首页