codeforces285B

时间:2021-07-28 08:51:43

Find Marble

CodeForces - 285B

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 p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. 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.

Input

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 nspace-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct.

Note that s can equal t.

Output

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.

Examples

Input
4 2 1
2 3 4 1
Output
3
Input
4 3 3
4 1 3 2
Output
0
Input
4 3 4
1 2 3 4
Output
-1
Input
3 1 3
2 1 3
Output
-1

sol:容易发现可以交换的最终一定会变成一个环,于是缩成一个个联通块,如果S和T不在同一个块中,就puts("-1"),否则就O(n)模拟需要几步才能到
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,S,T,P[N];
int Father[N];
inline int Get_Father(int x)
{
return (Father[x]==x)?(Father[x]):(Father[x]=Get_Father(Father[x]));
}
inline void Merg(int x,int y)
{
int xx=Get_Father(x),yy=Get_Father(y);
if(xx==yy) return;
Father[xx]=yy;
}
int main()
{
int i;
R(n); R(S); R(T);
for(i=;i<=n;i++) Father[i]=i;
for(i=;i<=n;i++)
{
R(P[i]); Merg(i,P[i]);
}
if(Get_Father(S)!=Get_Father(T)) return *puts("-1");
int Now=S,ans=;
while(Now!=T)
{
Now=P[Now]; ans++;
}
Wl(ans);
return ;
}
/*
input
4 2 1
2 3 4 1
output
3 input
4 3 3
4 1 3 2
output
0 input
4 3 4
1 2 3 4
output
-1 input
3 1 3
2 1 3
output
-1
*/
 

codeforces285B的更多相关文章

    随机推荐

    1. android 的闪屏效果

      android的闪屏效果,就是我们刚开始启动应用的时候弹出的界面或者动画,过2秒之后自动的跳转到主界面. 其实,实现这个效果很简单,使用Handler对象的postDelayed方法就可以实现.在这个 ...

    2. PLSQL转义字符

      http://blog.csdn.net/cunxiyuan108/article/details/5800800

    3. ylbtech-LanguageSamples-Threading&lpar;线程处理&rpar;

      ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Threading(线程处理) 1.A,示例(Sample) 返回顶部 “线程处理”示例 ...

    4. 基于kryonet的RPC,使用kryo进行序列化

      Kryo是一个序列化框架. Kryonet是一个基于kryo的RPC框架,它实现了一套高效简洁的API,它通过NIO实现了TCP和UDP通讯,目前还不支持Http. 自己写了一个测试代码,运行了下,感 ...

    5. myeclipse设置凝视

      Window-perferences--java--Code Style--Code Templates--Commments 类凝视:Types /** *@desc *@author haozk ...

    6. NFC Spy&colon;基于Android 4&period;4及以上手机的非接智能卡跟踪仪

      NFC Spy 用来查看读卡器和智能卡之间的指令.数据的交互传输过程,以便 NFC/HCE 开发者分析研究底层通讯协议,定位错误指令. 本程序要使用两部带有 NFC 硬件的 Android 手机,并且 ...

    7. UC手机浏览器js加入收藏夹

      概述 对于某些网站来说,让用户一键把网页加入收藏夹的设计是非常棒的,它能提醒用户把网页加入收藏夹,从而增加用户的回访率,使网站获得更多的流量. 在PC端,只有ie和ff支持用js把网页加入收藏夹的操作 ...

    8. &lbrack;Micropython&rsqb;TPYBoard v202 v102&plus;v202 家庭无线温湿度检测

       一.实验器件 1.TPYBoard v102 1块 2.TPYBoard v202 1块 3.Nokia 5110LCD显示屏 1块 4.DHT11温湿度传感器 1个 5.micro USB 数据线 ...

    9. 转:jdk动态代理实现

      原文链接: jdk动态代理 注:文章中用常用的流程实现 动态代理,流程逻辑比较清晰.文章后面对 “为什么要使用接口” 原理分析还未细看. jdk的动态代理为什么用接口,内部是什么原理呢?看了几篇文章貌 ...

    10. SAS 报表输出一些新式控制

      SAS 报表输出一些新式控制 *******************************:*Purpose: 报表*Programm: *Programmor: *Date: *Version: ...

    相关文章