B. Find marble

Source: Internet
Author: User
Time limit per test

2 seconds

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

Petya and Vasya are playing a game. Petya's gotNNon-transparent glasses, standing in a row. The Glasses 'positions are indexed with Integers
From 1NFrom left to right.
Note that the positions are indexed but the glasses are not.

First Petya puts a marble under the glass in positionS. Then he performs some (possibly zero) shuffling operations. One shuffling operation
Means moving the glass from the first position to positionP1,
The glass from the second position to positionP2 and
So on. That is, a glass goes from positionITo positionPI.
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 positionT. Vasya's task is to say what minimum number of shuffling
Operations Petya has saved med or determine that Petya has made a mistake and the marble cocould not have got from positionSTo positionT.

Input

The first line contains three integers:N, Bytes,S, Bytes,T(1 digit ≤ DigitNLimit ≤ limit 105; 1 digit ≤ DigitS, Bytes,TLimit ≤ limitN)-
The number of glasses, the ball's initial and final position. The second line containsNSpace-separated integers:P1, bytes,P2, middle..., middle ,...,PN(1 digit ≤ DigitPILimit ≤ limitN)-
The shuffling operation parameters. it is guaranteed that allPI'S
Are distinct.

Note thatSCan equalT.

Output

If the marble can move from positionSTo positionT,
Then print on a single line a non-negative integer-the minimum number of shuffling operations, needed to get the marble to positionT.
If it is impossible, print number-1.

Sample test (s) Input
4 2 12 3 4 1
Output
3
Input
4 3 34 1 3 2
Output
0
Input
4 3 41 2 3 4
Output
-1
Input
3 1 32 1 3
Output
-1

Explanation of problem solving: This question distinguishes array values from array subscripts, focusing on conversion between values and lower targets.

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespace std;int a[300001];int main(){int n,i,s,t;int count;int pos;count=1;scanf("%d %d %d",&n,&s,&t);for(i=1;i<=n;i++){scanf("%d",&a[i]);}if(s==t){printf("0\n");}else{pos=a[s];while(1){if(pos==t){printf("%d\n",count);break;}else if(pos==s){printf("-1\n");break;}else{pos=a[pos];count++;}}}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.