Codeforces Round #175 (Div. 2) B. Find Marble(简单模拟)
2013-03-29 21:37
495 查看
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
output
input
output
input
output
input
output
AC Code:
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:
#include <iostream> #include <fstream> #include <string> #include <set> #include <map> #include <vector> #include <stack> #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <algorithm> #include <utility> using namespace std; #define ll long long #define cti const int #define ctll const long long #define dg(i) cout << '*' << i << endl; int p[100002]; int main() { int n, s, t, cnt; while(scanf("%d %d %d", &n, &s, &t) != EOF) { for(int i = 1; i <= n; i++) scanf("%d", &p[i]); int tmp = s; cnt = 0; while(s != t) { cnt++; s = p[s]; if(s == tmp) break; } if(s == t) printf("%d\n", cnt); else printf("-1\n"); } return 0; }
相关文章推荐
- Codeforces Round #402 (Div. 2) C. Dishonest Sellers 简单模拟
- cf#323-div2-A. Asphalting Roads-简单模拟
- B. Tell Your World Codeforces Round #431 (Div. 2)(简单模拟)
- Codeforces Beta Round #92 (Div. 2) / 124A The number of positions 【简单模拟】【水题】
- Codeforces Round #338 (Div. 2)Bulbs(简单模拟)
- 【Codeforces Round 354 (Div 2)B】【简单模拟】Pyramid of Glasses 漏斗漏水n层t秒灌满多少个
- Codeforces Round #259 (Div. 2) A. Little Pony and Crystal Mine(简单模拟实现题)
- cf#323-div2-B. Robot's Task-简单模拟
- 【Codeforces Round 333 (Div 2)A 】【水题 简单进制模拟】Two Bases 不同进制数比大小
- Codeforces Round #247 (Div. 2) B. Shower Line(简单模拟)
- Codeforces Round #350 (Div. 2) E:Correct Bracket Sequence Editor (简单链表模拟)
- Codeforces Round #366 (Div. 2) A. Hulk (简单模拟)
- Codeforces Round #304 (Div. 2)-C. Soldier and Cards-简单模拟
- Codeforces Round #167 (Div. 2) A. Dima and Friends (模拟,简单)
- Codeforces Round #199 (Div. 2) -- B. Xenia and Spies (简单模拟)
- Codeforces Round #107 (Div. 2) A(简单模拟)
- js简单模拟div注册登录功能
- Codeforces Round #107 (Div. 2) B (简单模拟)
- Codeforces Round #387 (Div. 2)B. Mammoth's Genome Decoding(简单模拟)
- Codeforces Round #403 (Div. 2) A. Andryusha and Socks(简单模拟)