Problem:


Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Runtime Error

Solution code:
N , K = map(int,input().split())

A = list(map(int,input().split()))

reach = [-1 for i in range(N)]
reach[0] = 0 #最初の町は0回でいく
now = 0
#loop = 1
dic ={}
dic[0] = 0
for i in range(N+1):
  to = A[now]-1
  if reach[to] != -1:
    ini = i+1
    #print(i,to,reach[to])
    loop = i+1 - reach[to]
    break
  if i+1 not in dic:
    dic[i+1] = to
  reach[to] = i+1
  #loop += 1
  now = to


print(dic,reach,loop,ini)
if K <= ini:
  print(dic[K]+1)
else:
  t = (ini-loop) + (K-ini)%loop
  print(dic[t]+1)

Submission status: Runtime Error

Solution code:
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
from decimal import *

import numpy

def costCalc1(index,cost):
    if costList1[index] == -1:
        costList1[index] = cost
        costCalc1(a[index]-1,cost+1)
    else:
        costCalc2(a[index]-1,0)


def costCalc2(index,cost):
    if costList2[index] == -1:
        costList2[index] = cost
        costCalc2(a[index]-1,cost+1)

if __name__ == '__main__':
    n, k = map(int, input().split())
    a = [int(i) for i in input().split()]
    costList1 = [-1]*n
    costList2 = [-1]*n

    costCalc1(0,0)
    maxCount1 = max(costList1)+1
    maxCount2 = max(costList2)+1

    if costList1==costList2:
        k2 = k % maxCount1
        print(costList1.index(k2) + 1)
    else:
        k2 = k- maxCount1
        k2 = k2 % maxCount2
        print(costList2.index(k2) + 1)

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict,Counter

sys.setrecursionlimit(10**7)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    a=[0]+lmis()
    memo=[1]+[-1]*n
    i=0
    while i<k:
        nt=memo[i]
        i+=1
        if a[nt] not in memo: memo[i]=a[nt]
        else:
            memo[i]=a[nt]
            break
    start=memo.index(memo[i])
    roop=i-start
    cnt=k-start
    cnt-=cnt//roop*roop
    print(memo[start+cnt] if k>n else memo[k])

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    tree = [[] for _ in range(n)]
    for idx, a in enumerate(A):
        tree[idx].append(a - 1)

    start = []
    depth = [0 for _ in range(n)]

    def dfs(v, p, d, cnt):
        depth[v] = d
        for u in tree[v]:
            if u == p:
                continue

            if visited[u]:
                start.append([u + 1, depth[u], cnt + 1])
                return
            else:
                visited[u] = True
                dfs(u, v, d + 1, cnt + 1)
        return

    visited = [False for _ in range(n)]
    visited[0] = True
    dfs(0, -1, 0, 0)

    if k <= start[0][1]:
        print(depth.index(k) + 1)
    else:
        cnt = (k - start[0][1]) % (start[0][2] - start[0][1])
        res = start[0][1] + cnt
        print(depth.index(res) + 1)


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
import sys
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = {}
d2 = {}
d3 = {}
loopc = 0
for i in range(1, n + 1):
    d[i] = a[i - 1]


b = d[1]
d2[b] = 1
d3[i] = b
amari = 0
if k >= 2:
    if n >= k:
        for i in range(2, k + 1):
            if b in d:
                b = d[b]
    else:
        for i in range(2, n + 1):
            if b in d:
                b = d[b]
                if b not in d2:
                    b = d[b]
                    d2[b] = i
                    d3[i] = b
                elif b in d2:
                    loopc = i - d2[b]
            else:
                print(b)
                sys.exit()

        amari = k - n
        amari = amari % loopc
        for i in range(amari + 1):
            b = d[b]
        print(b)
else:
    print(b)

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

def find_min_cycle(a):
    memo = {1: 0}
    idx = 0
    cnt = 1

    while a[idx] not in memo:
        memo[a[idx]] = cnt
        cnt += 1
        idx = a[idx]-1

    return memo[a[idx]], cnt-memo[a[idx]], memo

offset, cycle_len, memo = find_min_cycle(a)
rmemo = {v: k for k, v in memo.items()}

if k <= offset:
    print(memo[k])
else:
    i = (k-offset)%cycle_len
    print(rmemo[offset+i])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
p = a[0]
s = a[0]
g = []
q = []
for i in range(n+1):
    g.append(p)
    p = a[p-1]
g.reverse()
for i in range(n):
    if g[0] == g[i+1]:
        mod = i+1
        break
for i in range(k%mod):
    q.append(s)
    s = a[s-1]
q.reverse()
print(q[0])

Submission status: Runtime Error

Solution code:
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
from decimal import *

import numpy

def costCalc1(index,cost):
    if costList1[index] == -1:
        costList1[index] = cost
        costCalc1(a[index]-1,cost+1)
    else:
        costCalc2(a[index]-1,0)


def costCalc2(index,cost):
    if costList2[index] == -1:
        costList2[index] = cost
        costCalc2(a[index]-1,cost+1)

if __name__ == '__main__':
    n, k = map(int, input().split())
    a = [int(i) for i in input().split()]
    costList1 = [-1]*n
    costList2 = [-1]*n

    costCalc1(0,0)
    maxCount1 = max(costList1)+1
    maxCount2 = max(costList2)+1

    print(maxCount1)
    print(maxCount2)

    if costList1==costList2:
        k2 = k % maxCount1
        print(costList1.index(k2) + 1)
    else:
        k2 = k- maxCount1
        k2 = k2 % maxCount2
        print(costList2.index(k2) + 1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

visited = {1: 0}
index = 0
last = 1

for i in range(K):
    t = A[last - 1]
    index += 1
    if t in visited:
        z = K - i - 1
        l = index - visited[t]
        print([k for k, v in visited.items() if v == visited[t] + (z % l)][0])
        exit()
    else:
        visited[t] = index
        last = t

print(visited[-1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

n = 1
ls = [1]
lps = 0
lpn = 0
for i in range(K):
    n = A[n-1]
    ls += [n]
    if A[n-1] in ls:
        lps = ls.index(A[n-1])
        lpn = i+2-lps
        break

print(ls[lps:][(K-lps)%lpn])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = []
load = []

for i in range(k-1):
    #print(nxt)
    if not(nxt in seen):
        seen.append(nxt)
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        break



print(res)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))

currTown = 1
visited = [1]
count = 0
const = 0
mod = 0

for i in range(K):
    nextTown = A[currTown-1]
    if((nextTown in visited) == False):
        visited.append(nextTown)
        count += 1
        currTown = nextTown
    else:
        const = visited.index(nextTown)
        mod = len(visited) - const
        break

for i in range(const):
    visited.pop(0)

print(visited[(K - const) % mod])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a_list = list(map(int,input().split()))
dum = a_list[0]
ans_list = []
for i in range(1,k+1):
    k = k - 1
    ans_list.append(str(dum))
    dum = a_list[dum-1]
    if ans_list.count(str(dum)) == 1:
        break
#print(k)
#print(ans_list)
ind = ans_list.index(str(dum))
loop = ans_list[ind:]
len_loop = len(loop)
loop_after = k%len_loop
print(loop[loop_after-1])
#print(ans_list[1:])
#rint(ans_list)
#print(dum)
#print(ind)

Submission status: Runtime Error

Solution code:
def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    path = {}
    c = 1
    i = 1
    while c < k:
        if not i in path.keys():
            path[i] = c
            i = A[i - 1]
            c += 1
        elif i in path.keys():
            start = path[i]
            roop = c - path[i]
            break
    roop += start - 1
    idx = 1
    for r in range(roop):
        idx = A[idx - 1]
    print(idx)

resolve()

Submission status: Runtime Error

Solution code:
# ----------------------------
N,K = map(int, input().split())
A = list(map(int, input().split()))
A.append(0)
n = K%N
now = 0
for i in range(n):
    now = A[now+1]
print(A[now])

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

N, K = map(int, input().split())

A = list(map(int, input().split()))


visited = set()
visited.add(1)
lst = [1, ]

now = 1
for i in range(K):
    now = A[now-1]
    if now in visited:
        last = K - i
        loop_point = now
        break
    visited.add(now)
    lst.append(now)

for i in range(len(lst)):
    if lst[i] == loop_point:
        inter = i
        loop = lst[i:]
        flg = 1

if not flg:
    print(lst[-1])
    exit()        

last = (last-1) % len(loop)
print(loop[last])

Submission status: Runtime Error

Solution code:
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
c = [0]*N

idx = 0
stp = 0
ans = 0
while(True):
  stp += 1
  idx = A[idx] - 1
  if c[idx]==0 and idx!=0:
    c[idx] = stp
  else:
    tmp1 = c[idx]
    tmp2 = stp - tmp1
    break
  if stp>=K:
    ans = idx + 1
    break

k1 = K - tmp1
k2 = k1%tmp2

for _ in range(k2):
  idx = A[idx] - 1
if(ans>0):
  print(ans)
else:
  print(idx+1)

Submission status: Runtime Error

Solution code:
int1 = lambda s: int(s)-1

N, K = map(int, input().split())
A = list(map(int1, input().split()))

X = []
visited = set()
now = 0
while now not in visited:
    visited.add(now)
    X.append(now)
    now = A[now]

if K < len(X):
    print(X[k])
else:
    s = X.index(now)
    print(X[s + ((K - s) % (len(X) - s))] + 1)

Submission status: Runtime Error

Solution code:
import re
def validate(text):
  match=re.search(r'(...)\1{2,}', text)
  return match.group(),match.start()

n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=1
count=''
for j in range(11):
  ans=a[ans-1]
  count+=str(ans)
#print(validate(count)[0][:3],validate(count)[1])
ans=1
for i in range((k-validate(count)[1])%len(validate(count)[0][:3])+validate(count)[1]):
  ans=a[ans-1]
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
teleport = dict()
for i, a in enumerate(A):
    teleport[i+1] = a
# teleport

state = 1
connect = []
looplen= 0
for n in range(K):
    connect.append(state)
    state = teleport[state] 
    if state in connect:
        looplen = n-connect.index(state)+1
        # print(connect.index(state))
        # print(n)
        break
connect
index = k%looplen
end = connect[index]
print(end)

Submission status: Runtime Error

Solution code:
#200510D
N, K = map(int, input().split())
A = list(map(int,input().split()))

b = A[0]

if A[0] == 1:
    ans = 1

else
    for i in range(N):
        c = A[b-1]
        b = c
    ans = b
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = {}
d2 = {}
d3 = {}
loopc = 0
for i in range(1, n + 1):
    d[i] = a[i - 1]


b = d[1]
d2[b] = 1
d3[i] = b
amari = 0
if k >= 2:
    if n >= k:
        for i in range(2, k + 1):
            if b in d:
                b = d[b]
    else:
        for i in range(2, n + 1):
            if b in d:
                b = d[b]
                if b not in d2:
                    b = d[b]
                    d2[b] = i
                    d3[i] = b
                elif b in d2:
                    loopc = i - d2[b]
            else:
                print(b)
                exit()

        amari = k - n
        amari = amari % loopc
        for i in range(amari + 1):
            b = d[b]
        print(b)
else:
    print(b)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

L = [0] * (N+1)
now = 1
p = []

for i in range(K):
    if L[now] == 1:
        break
    p.append(now)
    L[now] = 1
    now = A[now-1]

for i in range(len(p)):
    if p[i] == now:
        ans = p[i + (K - i) % len(p[i:])]

print(ans)

Submission status: Runtime Error

Solution code:
def costCalc(index,cost):
    if costList[index] == -1:
        costList[index] = cost
        costCalc(a[index]-1,cost+1)

if __name__ == '__main__':
    n, k = map(int, input().split())
    a = [int(i) for i in input().split()]
    costList = [-1]*n
    costCalc(0,0)
    maxCount = max(costList)+1

    k2 = k %maxCount
    print(costList.index(k2)+1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
arr = [int(x) for x in input().split()]
now = 1
s = [1]
for i in range(1,k + 1):
    now = arr[now - 1]
    if now in s:
        kai = i
        num = s.index(now)
        rp = len(s) - num
        break
    else:
        s.append(now)
k -= kai
s = s[num::]
amari = k % rp
print(s[amari])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n, k = map(int, input().split())

a_list = input().split()
a_list = [int(a) for a in a_list]

loop_dict = {}

loop_dict[0] = 1
next_a = 1
for i, a in enumerate(a_list, 1):
    if k <= i:
        start = i
        break

    next_a = a_list[next_a - 1]

    if next_a in loop_dict.values():
        start = [k for k, v in loop_dict.items() if v == next_a][0]
        end = len(loop_dict) - 1
        diff = end - start
        break

    loop_dict[i] = next_a

if k < start:
    print(loop_dict[k])
else:
    k -= start
    mod = k % (diff + 1)
    print(loop_dict[mod+start])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
i=0
count=0
while i<k:
  temp=a[temp-1]
  result.append(temp)
  i=i+1
  for j in range(0,i-1):
    if temp==result[j]:
      count=count+1
  if count==1:
    break
temp=0
l=len(result)
for i in range(0,l):
  if result[l-1]==result[i]:
    temp=i
    break
if k<l:
  print(result[k+1])
else:
  l2=l-1-temp
  print(result[temp+(k-temp)%l2])

Submission status: Runtime Error

Solution code:
N , K = map(int,input().split())
B = {}
C = {}
D = {0}
A = list(map(int,input().split()))
place = 0
num = 0
C[0] = 0
while True:
    place = int(A[place]) - 1
    num += 1   
    if place in D:
        Y = int(num) - int(C[place])
        break
    else:
        B[num] = place
        C[place] = num
        D.add(place)
X = (K - num) % Y
print(B[C[place] + X] + 1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
k += 1
l1 = list(map(int,input().split()))
l = list(i-1 for i in l1)
bit = list(0 for i in range(n))
bit[0] ==1
mi = [0]
m = l[0]
er = 0
while er ==0:
  if bit[m]==1:
    a = mi.index(m)
    r = mi[a:]
    er  =1
  else:
    bit[m] = 1
    mi.append(m)
    m = l[m]
 # print(bit)
be = mi[:a]+r
#print(be)
if k<= len(be):
  print(be[k])
else:
  w = k-len(be)
  u = w%len(r)-1
  print(r[u]+1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

li = [0]
li.extend(list(map(int, input().split())))

def search(town, times):
  if times <= 0:
    return town
  else:
    nextTown = li[town]
    return search(nextTown, times-1)

tuita = [0]*(n+1)
cnt = 0
town = 1

while True:
  town = li[town]
  cnt += 1
  if tuita[town] == 0:
    tuita[town] = cnt
  else:
    if k < tuita[town]:
      print(search(1, k))
    elif k == tuita[town]:
      print(town)
    else:
      print(search(town, (k - tuita[town]) % (cnt - tuita[town])))
    break

Submission status: Runtime Error

Solution code:
a = list(map(int,input().split()))
z = 0
b = [1]
c = [0]*n
for i in range(n*3):
  j = [a[b[i]-1]]
  b.extend(j)
  c[a[b[i]-1]-1] = c[a[b[i]-1]-1]+1
  if c[a[b[i]-1]-1] == 2:
    z = z + 1

if k <= n:
  y = b[k]
else:
  l = (k-n)%z
  y = b[n+l]
print(y)

Submission status: Runtime Error

Solution code:
def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    path = {}
    c = 1
    i = 1
    while c < k:
        if not i in path.keys():
            path[i] = c
            i = A[i - 1]
            c += 1
        elif i in path.keys():
            start = path[i]
            roop = c - path[i]
            break
    roop = k % roop
    idx = 1
    for r in range(roop):
        idx = A[idx - 1]
    print(idx)

resolve()

Submission status: Runtime Error

Solution code:
tmp = input().split()
N = int(tmp[0])
K = int(tmp[1])

tmp = input().split()

Map = [0]*N
GroupFlag = [0]*N

for i in range(N):
    Map[i] = int(tmp[i])

for i in range(N):
    nexti = Map[i]
    GroupFlag[nexti-1] = 1
GroupFlag[0] = 1
NN = sum(GroupFlag)
K = K%NN
if K == 0:
    K = NN

rt = [0]*K

def trip(self, steps):
    ntown = Map[self-1]
    if steps == K:
        return ntown
    rt[steps]=trip(ntown,steps+1)


    
Ft = trip(1,0)
print(rt[K-1])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
p = a[0]
s = a[0]
g = []
q = []
for i in range(n+1):
    g.append(p)
    p = a[p-1]
g.reverse()
for i in range(n):
    if g[0] == g[i+1]:
        mod = i+1
        break
for i in range(k%3):
    q.append(s)
    s = a[s-1]
print(q[-1])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A=list(map(int,input().split()))

P=[1]
now=1

for i in range(K):
    now=A[now-1]
    if now in P:
        x = i+1
        y = P.index(now)
        break
    else:
        P.append(now)

c=(K-y)%(x-y)

for i in range(c):
    now=A[now-1]

print(now)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = tuple(map(int, input().split()))

rec = [0] * N
costs = [0] * N

loc = 1
rec[loc-1] = 1

finished = True
for i in range(1, K+1):
    next_loc = A[loc - 1]

    if rec[next_loc - 1] == 1:
        break

    costs[next_loc - 1] = i
    rec[next_loc - 1] = 1
    loc = next_loc

loop_index_cost = costs[next_loc - 1]
loop_cost = i - loop_index_cost

cs = tuple(map(lambda x: x - loop_index_cost, costs))

final_loc = cs.index((K - loop_index_cost) % loop_cost) + 1
print(final_loc)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
*A,=map(int,input().split())

point=[0]*N
count_X=0
count_Y=0
i=0
while 3 not in point:
    if 2 not in point:
        count_X+=1
    point[i]+=1
    i=A[i]-1
    if 2 in point:
        count_Y+=1
count_Y-=1

if K>=count_X:
    Z=count_X+(K-count_X)%count_Y
else:
    Z=K

i=0
ans=1
while i<Z:
    ans=A[ans-1]
    i+=1

print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
temp = [i+1 for i in range(n)]
d = dict(zip(temp, A))
temp.clear()
temp.append(1)
curr = 0
i = 1
loop_idx = 0
#print(temp)
for _ in range(k):
    curr = d[i]
    i = curr
    if curr in temp:
        loop_idx = temp.index(curr) -1
        break
    temp.append(curr)
#loop_distance = loop_idx
cycle = (k - loop_idx) % (len(temp) - loop_idx)
#print(temp, cycle, loop_idx)
print(temp[cycle])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
done=[0 for _ in range(N)]
done[0]=1
W=[0]
loop=0
city=0
for i in range(N):
    if done[A[i]-1]==2:
        init=W.index(A[i]-1)
        loop=i-init
        break
    city=A[city]-1
    W.append(city)
    done[city]+=1

if min(done)>0:
    init=0
    loop=N

print(W[(K-init)%loop+init]+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
t = [0]*(n+1)
l = [1]
t[1] = 1
x = 1
for i in range(n):
    if(t[a[x]] == 0):
        x = a[x]
        l.append(x)
    else:
        z = i+1
        break
print(l[k%z])

Submission status: Runtime Error

Solution code:
def get_key_from_value(d, val):
    keys = [k for k, v in d.items() if v == val]
    if keys:
        return keys[0]
    return None
N, K = map(int, input().split())
Telep = list(map(int,input().split()))
Town = {}
next_town = 1
now_town = 1
for i in range(1,K+1):
  now_town = next_town
  if i == K:
    ans = now_town
    break;
  else:
    if now_town not in Town.values():
      #print("now = ",now_town)
      Town[i]=now_town
      next_town = Telep[now_town-1]
      #print("next=",next_town)
    elif now_town in Town.values():
      first_visit = get_key_from_value(Town,now_town)
      #print("first_visit=",first_visit)
      remain_loop= K-i+1
      #print("remain_loop=",remain_loop)
      loop_len = len(Town) - first_visit+1
      #print("loop_len=",loop_len)
      ans = Town[now_town+remain_loop%loop_len]
      break;
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
visit = [False] * n
visit[0] = True
nx = a[0]
keiro = [1]
for i in range(k):
    if visit[nx-1] == False:
        visit[nx-1] = True
        keiro.append(nx)
    else:
        roop = nx
        break
    nx = a[nx-1]
basyo = keiro.index(roop)-1
ix = (k-basyo) % len(keiro)

print(keiro[2:][ix])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
List = list(map(int, input().split()))
Lists = []
Listss = []
a = 0
count = 0
counts = 0
while a != Lists:
  a = List[a]
  Lists.append("a")
  count = count+1
while count != Listss:
  a = List[a]
  Listss.appened("a")
  counts = counts+1
countss = (K - count) % counts 
for i in range(countss):
  a = List[a]
print(a)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))

visited=[0]*N#訪問フラグ

ima_pos=0#現在地
mirai_pos=0#移動先

count_first=0#一巡目のループ
first_DP=[]

while(visited[ima_pos]==0):
  visited[ima_pos]=1#訪れたら更新
  mirai_pos=A[ima_pos]-1
  ima_pos=mirai_pos
  first_DP.append(ima_pos)
  count_first+=1#何回移動したか

second_start=ima_pos#2巡目ループのスタート地点
count_second=0#2巡目以降のループ
second_DP=[]

while(True):#スタート地点に戻ってきたら抜ける
  visited[ima_pos]=2#訪れたら更新
  mirai_pos=A[ima_pos]-1
  ima_pos=mirai_pos
  second_DP.append(ima_pos)
  count_second+=1#何回移動したか
  if ima_pos==second_start:
    break

if K<=count_first:
  print(first_DP[K]+1)
else:
  cc=K-count_first
  print(second_DP[cc%count_second-1]+1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

li = [0]
li.extend(list(map(int, input().split())))

def search(town, times):
  if times == 0:
    return town
  else:
    nextTown = li[town]
    return search(nextTown, times-1)

tuita = [0]*(n+1)
cnt = 0
town = 1

while True:
  town = li[town]
  cnt += 1
  if tuita[town] == 0:
    tuita[town] = cnt
  else:
    if k < tuita[town]:
      print(search(1, k))
    elif k == tuita[town]:
      print(town)
    else:
      print("a")
      break
      print(search(town, (k - tuita[town]) % (cnt - tuita[town])))
    break

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
cnt=0
i=0
q=[1]
while True:
    q.append(a[i])
    if q.count(a[i])==2:
        break
    i=a[i]-1
l=0
for i ,j in enumerate(q):
    if j==q[-1] :
        l=i
        break
if k>len(q[:l])+1:
    if q[-1]!=1:
        k-=len(q[:l])-1
    k%=len(q[l+1:])+1
    print(q[l+k+1])
else:
    print(q[k+1])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]

l = [0]*n
x = [1]
w = 0
for i in range(k):
  l[x[i]-1] += 1
  w = a[x[i]-1]
  x.append(w)
  if l[w-1] == 1:
    break

c = 0
for i in range(k):
  if x[-1] == x[i]:
    c = i
    break

ans = (k-c)%(len(x)-1-c)    

if k < i+ans:
  print(x[i+ans])
else:
  print(x[k-1])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
m=max(a,key=a.count)
res=0
ans=0
q=0
for i in range(1,k+1):
    res=a[res]-1
    if res==m-1:
        break
k=k-i
for i in range(1,k+1):
    res=a[res]-1
    if res==m-1:
        q=i
        break
k=k%q
for i in range(1,k+1):
    res=a[res]-1
print(res+1)

Submission status: Runtime Error

Solution code:
n , k = map(int , input().split())
a = list(map(int , input().split()))
ans = [1]
i = a[0]

while i not in ans:
  ans.append(i)
  i = a[i - 1]

repeat = ans[max(0 , ans.index(i) - 1):]
start = ans.index(repeat[0])
print(ans[(k + 1) % len(ans) - 1 + start])

Submission status: Runtime Error

Solution code:
N, K = input().split()
AN = input().split()
NAN = AN.index('1')
NK = int(K) % int(N)
if NK + NAN > len(AN):
    NK = NK + NAN - len(AN)
print(AN[NK])

Submission status: Runtime Error

Solution code:
def DFS(num):
    global time
    if D[num]==0:
        D[num]=time
        time +=1
    for i in M[num]:
        if color[i]=="white":
            color[num]="gray"
            color[i]="gray"
            DFS(i)
        elif color[i]=="gray":
            color[num]="black"
            color[i]="black"
            DFS(i)
        elif color[i]=="black":
            return


n,k=map(int,input().split())
A=list(map(int,input().split()))

B=[]
point,cnt=0,0
while cnt<n*2:
    point=A[point]
    B.append(point)
    point -=1
    cnt +=1

M=[set() for _ in range(n+1)]
for i in range(len(B)-1):
    a,b=B[i],B[i+1]
    M[a].add(b)

time=1
color=["white" for _ in range(n+1)]
D=[0 for _ in range(n+1)]
DFS(A[0])
route=[]
for i in range(n+1):
    if color[i]=="black":
        route.append([D[i],i])

C=[]
for i in sorted(route):
    C.append(i[1])

k=k-D[C[0]]
print(C[k%len(C)])

Submission status: Runtime Error

Solution code:
def main(num0,num,listB,count):
    num = listB[num-1]
    count += 1
    if num == num0:
        return count
    elif count > len(listB):
        return main(listB[num-1],listB[num-1],listB,0)
    else:
        return main(num0,num,listB,count)

def move(number,listA,m):
    if m == 0:
        return number
    else:
        number = listA[number-1]
        return move(number,listA,m-1)

n,k = map(int,input().split())
a = list(map(int,input().split()))
b = main(1,1,a,0)
c = k % b
ans = move(1,a,c)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
lst=[0]
now=0
memo=0
town=[0]*n
town[0]=1
b=0
for i in range(k):
	now=a[now]
	if town[now]==1:
		memo=now
		b=1
		break
	lst.append(now)
	town[now]=1
if b==1:
	i=a.index(memo)
	l=len(lst)
	if memo!=0:
		lst=lst[i:]
		l-=len(lst)
		print(lst[(k-l)%len(lst)]+1)
	else:
		print(lst[k%l]+1)

else:
	print(now+1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
g = dict()

for i, v in enumerate(a):
    g[i+1] = v

def find_num(target):
    path = 1
    target_ = g[target]
    while target_ != target:
        target_ = g[target_]
        path += 1
    return path

target = 1
visited = dict()
for i in range(k):
    target = g[target]
    if target not in visited:
        visited[target] = True
    else:
        p = find_num(target)
        break
for _ in range((k-1-i)%p):
    target = g[target]
print(target)

Submission status: Runtime Error

Solution code:
all = input()
towns = int(all.split(' ')[0])
K = int(all.split(' ')[1])
alltele = input().split(' ')
alltele = list(map(int,alltele))
alltele.insert(0,0)
start = alltele[1]
K = K%towns
for i in range(K,1,-1):
	to = alltele[start]
	start = to
print(to)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
a_list=list(map(int,input().split()))
a_list.append(0)
tmp=a_list[0]
tmp_list=[]
K2=K%N
count=1
while count<7:
    tmp=a_list[tmp-1]
    tmp_list.append(tmp)
    count+=1
if K==1:
    print(a_list[0])
else:
    print(tmp_list[K2-2])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

li = [0]
li.extend(list(map(int, input().split())))

def search(town, times):
  if times == 0:
    return town
  else:
    nextTown = li[town]
    return search(nextTown, times-1)

tuita = [0]*(n+1)
cnt = 0
town = 1

while True:
  town = li[town]
  cnt += 1
  if tuita[town] == 0:
    tuita[town] = cnt
  else:
    if k < tuita[town]:
      print(search(1, k))
    elif k == tuita[town]:
      print(town)
    else:
      print(search(town, (k - tuita[town]) % (cnt - tuita[town])))
    break

Submission status: Runtime Error

Solution code:
"""
"""
N, K = map(int, input().split())
A = list(map(int, input().split()))
loop_end_list = [1]
loop = 0
s = 0
e = 1
while True:
    s = A[e-1]
    e = s
    if e in loop_end_list:
        break
    else:
        loop_end_list.append(e)
        loop += 1
ans = K % loop
if ans == 0:
    print(1)
else:
    print(loop_end_list[loop])

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())

l = list(map(int, input().split()))

city = 1
l2 = [1]
while True:
    city = l[city-1]
    if city in l2:
        break
    l2.append(city)
first = l2.index(city)
if len(l2) <= k:
    if city == l2[-1]:
        print(city)
    elif city == l2[city]:
        print(city)
    else:
        l2 = l2[first:]
        print(l2[(k-first)%(len(l2))])
else:
    print(l2[k])

Submission status: Runtime Error

Solution code:
n,k =map(int,input().split())
a=list(map(int,input().split()))
a = [1] + a
def next(i):
    return a[i]

z = [0] * (n+1)

place = 1
T = 0
interval = 0
if k < n:
    for i in range(k):
        place = next(place)
    print(place);exit()
place = 0
for i in range(n):
    place = next(place)
    if z[place] != 0:
        interval = T - z[place]
        break
    z[place] = T
    T += 1
m = (k-z[place])%interval
for i in range(m):
    place = next(place)
print(place)