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. C# 相关面试试题简单的总结

    最近一个搞NET开发的朋友离职了,想让我给他找点关于NET的相关面试题,准备抱一下佛脚,迎接新的挑战. 我赶紧找到以前检索的各种宝典,试题,今天梳理一下关于NET的基础知识点. 1.面向对象语言的三大 ...

  2. ThinkPHP+JQuery实现文件的异步上传

    前端代码 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  3. Xamarin.Forms 3.0的新特性

    近期因为工作关系开始使用Xamarin,翻译了两篇国外的介绍3.0新特性的文章,供大家参考. 第一篇文章来自Xamarin官网,原文地址:https://blog.xamarin.com/xamari ...

  4. 04 Docker/基础设施 - DevOps之路

    04 Docker/基础设施 - DevOps之路 文章Github地址,欢迎start:https://github.com/li-keli/DevOps-WiKi Docker是一个开源的引擎,可 ...

  5. linux下的shell脚本(基本)

    shell有些命令和格式不用老是容易忘,学而时习之,不亦说乎~ 先说明以下内容来自: http://c.biancheng.net/cpp/shell/ ,C语言中文网,请大家支持原作,点击链接查看. ...

  6. spring security运行流程图(转)

    原文:http://blog.csdn.net/u011511684/article/details/31394493 示例下载地址:http://download.csdn.net/detail/u ...

  7. Elasticsearch的DSL之比较重要的几个查询语句

    1.  match_all { "match_all": {}} 匹配所有的, 当不给查询条件时,默认. 2. match 进行full text search或者exact va ...

  8. 有界算子p129

    ? 如果我把这里的1改成2,把1/(a-b) 换成1/2(a-b) 为什么不能是? 2. 这里的x是关于t的函数,为什么x属于 结果了?和x应该没有关系呀? 3. 那为什么T的范数不是一个固定值?为什 ...

  9. 软工网络15团队作业7——Alpha冲刺之事后诸葛亮

    Deadline: 2018-5-16 22:00PM,以博客提交至班级博客时间为准 事后诸葛亮分析 Alpha冲刺,很多同学经历了"Learning by doing"的学一门新 ...

  10. JAVAString初始化的引用问题

    1 String a="Hello JAVA"; 2 3 String b=a; 4 5 System.out.println(a); 6 7 System.out.println ...