zoukankan      html  css  js  c++  java
  • codeforces285B

    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=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0');    return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=100005;
    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=1;i<=n;i++) Father[i]=i;
        for(i=1;i<=n;i++)
        {
            R(P[i]); Merg(i,P[i]);
        }
        if(Get_Father(S)!=Get_Father(T)) return 0*puts("-1");
        int Now=S,ans=0;
        while(Now!=T)
        {
            Now=P[Now]; ans++;
        }
        Wl(ans);
        return 0;
    }
    /*
    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
    */
    View Code
     
  • 相关阅读:
    hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
    HDU 2147 kiki's game(博弈)
    C++学习47 文件的概念 文件流类与文件流对象 文件的打开与关闭
    C++学习46 getline()函数读入一行字符 一些与输入有关的istream类成员函数
    C++学习45 流成员函数put输出单个字符 cin输入流详解 get()函数读入一个字符
    C++学习44 格式化输出,C++输出格式控制
    C++学习43 输入输出有关的类和对象
    C++学习42 输入和输出的概念
    C++学习41 exception类
    C++学习40 抛出自己的异常
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10638692.html
Copyright © 2011-2022 走看看