zoukankan      html  css  js  c++  java
  • Codeforces Round #175 (Div. 2) B. Find Marble(简单模拟)

    B. Find Marble
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 n space-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.

    Sample test(s)
    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

     AC Code:

     1 #include <iostream>
     2 #include <fstream>
     3 #include <string>
     4 #include <set>
     5 #include <map>
     6 #include <vector>
     7 #include <stack>
     8 #include <queue>
     9 #include <cmath>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <algorithm>
    13 #include <utility>
    14 using namespace std;
    15 #define ll long long
    16 #define cti const int
    17 #define ctll const long long
    18 #define dg(i) cout << '*' << i << endl;
    19 
    20 int p[100002];
    21 
    22 int main()
    23 {
    24     int n, s, t, cnt;
    25     while(scanf("%d %d %d", &n, &s, &t) != EOF)
    26     {
    27         for(int i = 1; i <= n; i++) scanf("%d", &p[i]);
    28         int tmp = s;
    29         cnt = 0;
    30         while(s != t)
    31         {
    32             cnt++;
    33             s = p[s];
    34             if(s == tmp) break;
    35         }
    36         if(s == t) printf("%d\n", cnt);
    37         else printf("-1\n");
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    js 文件的操作
    js重点基础知识 以及小案例_最简单的轮播图 简单的动态表格( encodeURIComponent()编码比 encodeURI()编码)
    2阶——数据库连接池 c3p0 , druid, dbcp (其实所有的连接池都实现了dataSource接口,就可以调用getconnection方法)
    2阶——JDBC,JDBCTemplate(操作数据库)
    vue + django 批量删除
    简单的模糊搜索 Vue + django
    vue 父子组件传参简单的分页
    vue 多对多反序列化上传图片
    模型里的 抽象类 表继承
    django 多对多反序列添加
  • 原文地址:https://www.cnblogs.com/cszlg/p/2989656.html
Copyright © 2011-2022 走看看