CF285B.Find Marble
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.
First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position _p_1, the glass from the second position to position _p_2 and so on. That is, a glass goes from position i to position p__i. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.
After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.
彼得和瓦西娅正在玩一个游戏。彼得有 n 个不透明的杯子,排成一列。杯子的位置从左到右用整数 1 到 n 编号。注意:此处编号的是位置,而非杯子本身。
首先,彼得将一颗弹珠放在位置 s 的杯子下方。然后他执行若干次(可能为零次)洗杯操作。一次洗杯操作的定义是:将位于第 1 个位置的杯子移动到位置 p1,将位于第 2 个位置的杯子移动到位置 p2,依此类推。即,位于位置 i 的杯子被移动到位置 pi。在一次洗杯操作中,所有杯子同时移动。当杯子被洗动时,弹珠并不会从一个杯子跳到另一个杯子;它始终随最初放置它的那个杯子一起移动。
在全部洗杯操作完成后,彼得向瓦西娅展示弹珠已移动至位置 t。瓦西娅的任务是:判断彼得最少执行了多少次洗杯操作;或判定彼得出错了——弹珠根本不可能从位置 s 移动到位置 t。
输入格式
The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: _p_1, _p_2, ..., p__n (1 ≤ p__i ≤ n) — the shuffling operation parameters. It is guaranteed that all p__i's are distinct.
Note that s can equal t.
第一行包含三个整数:n、s、t(1≤n≤105;1≤s,t≤n)——分别表示玻璃杯的数量、球的初始位置和最终位置。
第二行包含 n 个用空格分隔的整数:p1, p2, …, pn(1≤pi≤n)——表示洗牌操作的参数。保证所有 pi 互不相同。
注意:s 可能等于 t。
输出格式
If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.
如果弹珠可以从位置 s 移动到位置 t,则在一行中输出一个非负整数——将弹珠移动到位置 t 所需的最小洗牌操作次数;如果无法实现,则输出数字 −1。
输入输出样例
输入#1
4 2 1 2 3 4 1
输出#1
3
输入#2
4 3 3 4 1 3 2
输出#2
0
输入#3
4 3 4 1 2 3 4
输出#3
-1
输入#4
3 1 3 2 1 3
输出#4
-1
输入解题思路,AI测评打分。不知道怎么写?