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的更多相关文章
随机推荐
- input表单提交完毕,返回重新填入有黄色背景,和 历史记录 清除
<input autocomplete="value"> // 添加这个属性,可以解决然后添加一个css input:-webkit-autofill {box-sha ...
- SpringMVC自动封装List对象 —— 自定义参数解析器
前台传递的参数为集合对象时,后台Controller希望用一个List集合接收数据. 原生SpringMVC是不支持,Controller参数定义为List类型时,接收参数会报如下错误: org.sp ...
- Ajax跨越问题原因分析与解决思路
1.什么是AJAX跨域问题 简单来说,就是前端调用后端服务接口时 如果服务接口不是同一个域,就会产生跨域问题 2.AJAX跨域场景 前后端分离.服务化的开发模式 前后端开发独立,前端需要大量调用后端接 ...
- Django Rest framework基础使用之 serializer
rest-framework文档地址:http://www.django-rest-framework.org/ Django Rest framework是一个非常强大且灵活的工具包,用于构建web ...
- mysql 库 行 列的 操作使用
-----------------------------------------------------------------------------时间不等你,下一刻,全是新的.每一刻都让它变得 ...
- Jenkins- job之间传参
前言: 本文介绍插件: Parameterized Trigger plugin的具体使用方法. 一.插件介绍 Parameterized Trigger plugin插件可以让你在构建完成时触发新的 ...
- 16-使用Selenium模拟浏览器抓取淘宝商品美食信息
淘宝由于含有很多请求参数和加密参数,如果直接分析ajax会非常繁琐,selenium自动化测试工具可以驱动浏览器自动完成一些操作,如模拟点击.输入.下拉等,这样我们只需要关心操作而不需要关心后台发生了 ...
- elasticsearch聚合操作——本质就是针对搜索后的结果使用桶bucket(允许嵌套)进行group by,统计下分组结果,包括min/max/avg
分析 Elasticsearch有一个功能叫做聚合(aggregations),它允许你在数据上生成复杂的分析统计.它很像SQL中的GROUP BY但是功能更强大. 举个例子,让我们找到所有职员中最大 ...
- WinRAR从入门到高级的操作技巧集合
一.基础技巧 1.批量建立文件夹 如果在工作中,经常要建立很多相同文件夹结构(如在备份数据时).那可以把这个繁琐的工作让WinRAR完成:先在“资源管理器”中把多个文件夹结构建好(包括其下的子文件夹) ...
- Drools 规则引擎
Drools - Drools - Business Rules Management System (Java™, Open Source) http://drools.org/ [Drools]J ...