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. LDAP2-创建OU创建用户

    创建OU创建用户 1.创建OU 选择Organisational unit 组织单元 输入OU名称 提交信息 结果创建成功 2.创建员工 选择ou选择新建子条目 选择默认模板 选择inetorgper ...

  2. Long类型参数传到前端精度丢失的解决方案

        由于公司数据库表的id是利用雪花算法生成的,所以实体类里面定义的数据类型为Long.但是这个数据传到前端时,发生了精度丢失的现象.本文记录了从java后端的角度如何解决这个精度丢失的问题,便于 ...

  3. JSOUP如何优秀的下载JPEG等二进制图像

    引言 JSOUP默认是不支持解析JPEG等二进制图像的,解决方法也很简单,只需要加上Jsoup.ignoreContentType(true)这一行代码就可以.关于这一点的原因,来看看官方API说明. ...

  4. A2D JS框架

    写了个微型JS框架 主要实现了:showDialog.noConflict.定位元素.event绑定功能 使用端的代码: <head> <title></title> ...

  5. JVM总括三-字节码、字节码指令、JIT编译执行

    JVM总括三-字节码.字节码指令.JIT编译执行 目录:JVM总括:目录 java文件编译后的class文件,java跨平台的中间层,JVM通过对字节码的解释执行(执行模式,还有JIT编译执行,下面讲 ...

  6. Python股票分析系列——获得标普500的所有公司股票数据.p6

    该系列视频已经搬运至bilibili: 点击查看 欢迎来到Python for Finance教程系列的第6部分. 在之前的Python教程中,我们介绍了如何获取我们感兴趣的公司名单(在我们的案例中是 ...

  7. Jenkins - Extended E-mail配置教程

    前言: 在Jenkins的使用中邮件提醒是一个常用功能,Extended E-mail Notification是一个功能更为齐全,使用也更为复杂的插件,本文即将为大家详细讲解如何配置相关内容,感兴趣 ...

  8. php之常用扩展总结

    在此总结,开发中经常使用到的扩展,来进行日常PHP的开发工作 bcmath(精确数值处理) bz2 calendar Core ctype curl date dom ereg exif filein ...

  9. C#复习笔记(3)--C#2:解决C#1的问题(实现迭代器的捷径)

    实现迭代器的捷径 从这个题目上可以看到C#1实现一个迭代器模式的话是多么的痛苦,我自己也写过不下40行的代码来实现一个迭代器,C#中的迭代器模式是通过IEnumerable和他的泛型等价物IEnume ...

  10. C#设计模式之6:抽象工厂模式

    前面分析了简单工厂模式和工厂方法模式,接着来看一下抽象工厂模式,他与工厂方法模式有一些相似的地方,也有不同的地方. 先来看一个不用工厂方法模式实现的订购披萨的代码: 对象依赖的问题:当你直接实例化一个 ...