| 1 | import java.util.*; |
| 2 | |
| 3 | class Main { |
| 4 | public static void main(String[] args) { |
| 5 | // 入力 |
| 6 | Scanner sc = new Scanner(System.in); |
| 7 | int N = sc.nextInt(); |
| 8 | long K = sc.nextLong(); |
| 9 | int[] A = new int[N + 1]; |
| 10 | for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); |
| 11 | |
| 12 | // 配列の初期化 |
| 13 | long[] First = new long[N + 1]; |
| 14 | long[] Second = new long[N + 1]; |
| 15 | for (int i = 1; i <= N; i++) First[i] = -1; |
| 16 | for (int i = 1; i <= N; i++) Second[i] = -1; |
| 17 | |
| 18 | // 答えを求める(cur は現在いる町の番号) |
| 19 | long cnt = 0; |
| 20 | int cur = 1; |
| 21 | while (true) { |
| 22 | // First, Second の更新 |
| 23 | if (First[cur] == -1) First[cur] = cnt; |
| 24 | else if (Second[cur] == -1) Second[cur] = cnt; |
| 25 | |
| 26 | // K 回の移動後に町 cur にいるか判定 |
| 27 | if (cnt == K) { |
| 28 | System.out.println(cur); |
| 29 | System.exit(0); |
| 30 | } |
| 31 | else if (Second[cur] != -1L && (K - First[cur]) % (Second[cur] - First[cur]) == 0) { |
| 32 | System.out.println(cur); |
| 33 | System.exit(0); |
| 34 | } |
| 35 | |
| 36 | // 移動 |
| 37 | cur = A[cur]; |
| 38 | cnt += 1; |
| 39 | } |
| 40 | } |
| 41 | } |
nothing calls this directly
no outgoing calls
no test coverage detected