codeforces285B
Find Marble
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
4 2 1
2 3 4 1
3
4 3 3
4 1 3 2
0
4 3 4
1 2 3 4
-1
3 1 3
2 1 3
-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.A乘以B 题目内容描述:看我没骗你吧 -- 这是一道你可以在10秒内完成的题:给定两个绝对值不超过100的整数A和B,输出A乘以B的值. 1) ...
- Linux并发与同步专题 (2)spinlock
关键词:wfe.FIFO ticket-based.spin_lock/spin_trylock/spin_unlock.spin_lock_irq/spin_lock_bh/spin_lock_ir ...
- CF434D Nanami's Power Plant 最小割
传送门 因为连距离限制的边的细节调了贼久QAQ 这个题和HNOI2013 切糕性质相同,都是有距离限制的最小割问题 对于每一个函数,用一条链记录变量\(x\)在不同取值下这个函数的贡献.对于一个\(x ...
- AndroidO bluedroid alarm 机制分析
bluedroid的alarm 机制实现在osi/osi/src/alarm.cc 中: 这里面实现了很多的接口: alarm_t* alarm_new(const char* name): alar ...
- JSOUP如何优秀的下载JPEG等二进制图像
引言 JSOUP默认是不支持解析JPEG等二进制图像的,解决方法也很简单,只需要加上Jsoup.ignoreContentType(true)这一行代码就可以.关于这一点的原因,来看看官方API说明. ...
- SpringBoot分布式 - SpringCloud
一:介绍 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...
- 给扔物线 HenCoder Plus 学员的一次分享文字版
半个月前,和我的终极技术目标扔物线朱凯一拍即合,到了他所开展的 HenCoder Plus 课程给大家分享了 1 个多小时的「模拟面试」心得,也顺便听了几次凯哥的课程,感觉真的挺用心的.自己也希望能一 ...
- python--map()、reduce()
map()和reduce()是一种在处理大数据时的重要思想,在平时也可以利用.在python中内置了这两个方法,map取映射的意思,reduce取归纳的意思. 一.map() map(func, ls ...
- Xamarin.Forms 3.0的新特性
近期因为工作关系开始使用Xamarin,翻译了两篇国外的介绍3.0新特性的文章,供大家参考. 第一篇文章来自Xamarin官网,原文地址:https://blog.xamarin.com/xamari ...
- jqGrid之treeGrid及行拖拽
单纯的做个小记录 今天做功能用到了jqGrid里面的treeGrid,遇到几个问题,这里做下记录 treeGrid 树表格的应用在官网给出了很直白的例子: 1.http://blog.mn886.ne ...