A8908 | Find Marble
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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$ .
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$ .
输入格式
The first line contains three integers: $n,s,t\ (1<=n<=10^{5}; 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$ .
Note that $s$ can equal $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.
输入输出样例
输入 #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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted