-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolice.cpp
More file actions
43 lines (35 loc) · 692 Bytes
/
police.cpp
File metadata and controls
43 lines (35 loc) · 692 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// NOTE: it is recommended to use this even if you don't understand the following code.
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main() {
// uncomment the following lines if you want to read/write from files
// ifstream cin("input.txt");
// ofstream cout("output.txt");
int N, K;
cin >> N >> K;
int V[N+1];
for (int i=1; i<=N; i++) {
cin >> V[i];
}
// insert your code here
int t=0;
if (K == 1) {
t = 0;
} else if (count(V, V+N, K) == 0) {
t = -1;
} else {
int h = 1;
while (h != K) {
h = V[h];
t ++;
if (t == N) {
t = -1;
break;
}
}
}
cout << t << endl; // print the result
return 0;
}