解题说明:此题把数组值和数组下标区分清楚即可,重点实现值和下标的转换
#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;
}