CF285B Find Marble 题解
题目大意
给定
解题思路
就是按题意模拟,首先
对于无解的情况可以用一个桶记录所有的
实现代码
#include <bits/stdc++.h>
using namespace std;
int p[100001];
bool st[100001];
int main()
{
int n,s,t,ans=0;
cin>>n>>s>>t;
for(int i=1;i<=n;i++)
cin>>p[i];
while(true)
{
if(s==t) break;
if(st[s])
{
puts("-1");
return 0;
}
st[s]=true;
s=p[s];
ans++;
}
cout<<ans;
}