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

Solution (Python):
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]

    bit_k = bin(K)[2:]

    telepo_list = [A_list]

    for _ in range(len(bit_k)-1):
        temp_list = telepo_list[-1]
        new_telep = [temp_list[i-1] for i in temp_list]
        telepo_list.append(new_telep)

    

    goto = 1
    for i in range(len(bit_k)):
        temp_list = telepo_list[i]
        if ((K >> i) & 1):
            goto = temp_list[goto - 1]
            

    print(goto)


if __name__ == "__main__":
    resolve()

Solution (Python):


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

A = [a-1 for a in A]

b = bin(K)
D = {0:list(range(N))} 
D[1] = [A[D[0][i]] for i in range(N)]

for i in range(1, len(b)-2):
    D[2**i] = [D[2**(i-1)][D[2**(i-1)][j]] for j in range(N)]


n = 2**(len(b[2:])-1)
S = D[n][0] 
keta = len(b[2:]) - 2
for j in range(len(b[3:])):
    k = 2**keta
    if b[j+3] == '1':
        S = D[k][S]
    keta -= 1

print(S+1)

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

T=[10**9]*(N+1)
label=[-1]*(N+1)
T[0]=1
label[1]=0
for i in range(1,N+1):
    T[i]=A[T[i-1]-1]
    if label[T[i]]>=0:
        poss=label[T[i]]
        pose=i
        break
    else:
        label[T[i]]=i

pos=K-poss-1
if pos>=0:
    pos=pos%(pose-poss)
    print(T[poss+pos+1])
else:
    print(T[K])

Solution (Python):
N, K = [int(x) for x in input().split()]
A = [int(x) - 1 for x in input().split()]

i = 0
T = []
S = set()
st = 0
while True:
    T.append(i)
    S.add(i)
    i = A[i]
    if i in S:
        st = T.index(i)
        break




idx = (K - st) % (len(T) - st)

if K > st:
    print(T[st + idx] + 1)
else:
    print(T[K] + 1)

Solution (Python):


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

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

b = bin(K)
D = {0:list(range(N))} 
D[1] = [A[D[0][i]] for i in range(N)]

for i in range(1, len(b)-2):
    D[2**i] = [D[2**(i-1)][D[2**(i-1)][j]] for j in range(N)]


n = 2**(len(b[2:])-1)
S = D[n][0] 
keta = len(b[2:]) - 2
for j in range(len(b[3:])):
    k = 2**keta
    if b[j+3] == '1':
        S = D[k][S]
    keta -= 1

print(S+1)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))

passed = [-1] * (n+1)
route = []
v = 1
while passed[v] == -1:
    passed[v] = len(route)
    route.append(v)
    v = a[v-1]

c = len(route) - passed[v]
l = passed[v]

if k < l:
    print(route[k])
else:
    k -= l
    k %= c
    print(route[l+k])

Solution (Python):
def resolve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    loop = [1]
    arrived = [0] * N
    now = 1
    arrived[now - 1] = 1

    for i in range(K):
        now = A[now - 1]
        if arrived[now - 1] == 0:
            arrived[now - 1] = 1
            loop.append(now)
        else:
            loop = loop[loop.index(now):]
            arrive = loop[(K - (i + 1)) % len(loop)]
            print(arrive)
            return

    print(now)

resolve()

Solution (Python):
'''
・基本的には素直なシミュレーションを行う
・ループ分のシミュレーションを省略することでTLEを回避
・Kは「残りのワープ数」と考える

K回のワープ中にループが含まれる場合、
1. 素直なシミュレーションを行う。このとき、Kを減算していく。
2. ループを検出したら、Kからループ長をできるだけ多く引くことで、計算を省略する。
3. 残りのステップ数、再度シミュレーションを行う
という手順で答えを求められる。
なお、ループが含まれない場合も1のみで答えを求められる。
'''

def main():
    N, K = map(int, input().split())
    A = [a-1 for a in map(int, input().split())]    
    B = [None]*N    
    now = 0         
    step = 0        
    B[now] = 0      
    while K:
        now = A[now]    
        step += 1
        K -= 1
        if B[now] is not None:  
            K %= (step - B[now])      
        else:
            B[now] = step  
    print(now+1)    

main()

Solution (Python):
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
city = 0
all = []
roop = []
while sub[city] != 2:
  if sub[city] == 0:
    all.append(city+1)
  else:
    roop.append(city+1)
  sub[city] += 1
  city = A[city] -1
if len(all) > k:
  print(all[k])
else:
  print(roop[(k-(len(all)-len(roop)))%len(roop)])

Solution (Python):

import collections
import itertools as it
import math

 


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




ord_ = [-1] * (n + 1) 
homon = [] 
current = 0
while ord_[current] == -1:
    ord_[current] = len(homon)
    homon.append(current)
    current = a[current] - 1

freq = len(homon) - ord_[current] 
n_before_loop = ord_[current] 

if k < n_before_loop:
    print(homon[k] + 1)
else:
    k -= n_before_loop
    k %= freq
    print(homon[n_before_loop+k] + 1)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
log = []
visited = [False for i in range(n)]
now = a[0]-1
loop = False
for i in range(k-1):
    if visited[now] == True:
        loop = True
        break
    log.append(now)
    visited[now] = True
    now = a[now]-1
else:
    print(now+1)
if loop:
    for i in range(len(log)):
        if log[i] == now:
            first = len(log[:i])
            log_list = log[i:]
            loop_num = len(log_list)
            break
    k -= first
    k %= loop_num
    print(log_list[k-1]+1)

Solution (Python):
import numpy as np
import itertools
n, k = map(int, input().split())
a = list(map(int, input().split()))

now = 1
town_list = [0]*n
town_list[0] = 1
tn_list = [1]

while(1):
    next = a[now-1]
    now = next
    if town_list[now-1] == 0:
        town_list[now-1] = 1
        tn_list.append(now)

    else:
        idx = tn_list.index(now)
        initial = tn_list[:idx]
        cycle = tn_list[idx:]
        break

if k >= len(initial):
    end_idx = (k-len(initial))%len(cycle)
    print(cycle[end_idx])
else:
    print(initial[k])

Solution (Python):
n,k = map(int,input().split())
li = [0]+list(map(int,input().split()))

memo = [-1]*(n+1)
start = 1
syuuki = 0
haji = 0

for i in range(k):
    if memo[start] != -1:
        syuuki = i - memo[start]
        haji = memo[start]
        break
    memo[start] = i
    start = li[start]
else:
    print(start)
    exit()


k-=haji
k%=syuuki
k+=haji

start = 1
for kk in range(k):
    start = li[start]
print(start)

Solution (Python):

import collections
import itertools as it
import math

 


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




ord_ = [-1] * (n + 1) 
s = []
current = 0
while ord_[current] == -1:
    ord_[current] = len(s)
    s.append(current)
    current = a[current] - 1
c = len(s) - ord_[current]
l = ord_[current]

if k < l:
    print(s[k] + 1)
else:
    k -= l
    k %= c
    print(s[l+k] + 1)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = 60
dp = [[-1]*N for _ in range(L+1)]
for i,a in enumerate(A):
    dp[0][i] = a-1
for i in range(L):
    for j in range(N):
        dp[i+1][j] = dp[i][dp[i][j]]
ans = 0
for i in range(L):
    if K&(1<<i):
        ans = dp[i][ans]
print(ans+1)

Solution (Python):
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left
from functools import reduce
input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n, k = i_map()
    num_list = i_list()

    trans_list = [0] * n

    next_trans = num_list[0]
    for i in range(n):
        trans_list[i] = next_trans
        next_trans = num_list[next_trans-1]

    

    chcek_list = [INF] * n
    for i in range(n):
        if chcek_list[trans_list[i]-1] != INF:
            break
        chcek_list[trans_list[i]-1] = 1

    first_town = trans_list.index(trans_list[i])

    period = i - first_town

    if k <= n:
        print(trans_list[k-1])
    else:
        trans_list = trans_list[first_town:]
        trans_list = trans_list[:period]
        print(trans_list[(k-first_town)%period-1])

if __name__ == '__main__':
    main()

Solution (Python):

N, K = map(int, input().split())
A = [0]
A.extend(list(map(int, input().split())))


l = [0]*(N+1)
now = 1
i = 1
while l[now] == 0:
    l[now] = i
    now = A[now]
    i += 1

ans = 0
if K < l[now]:
    ans = l.index(K+1)
else:
    k = (K-l[now]+1)%(len([x for x in l if x!=0])-l[now]+1)
    ans = l.index(l[now]+k)


print(ans)

Solution (Python):
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    
    r = 60
    import math
    r = math.ceil(math.log2(k))
    idx = 0
    a = [a[i]-1 for i in range(n)]
    for i in range(r):
        if (k>>i)&1 == 1:
            idx = a[idx]
        a = [a[a[j]] for j in range(n)]
    print(idx+1)    

if __name__ == '__main__':
    main()

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

pos = 0
visit = [0] * N
move = []
roop = []

while visit[pos] != 2:
    if visit[pos] == 0:
        move.append(pos)
    else:
        roop.append(pos)

    visit[pos] += 1
    pos = A[pos] - 1

if len(move) > K:
    print(move[K] + 1)
else:
    print(roop[(K- (len(move) - len(roop))) % len(roop)] + 1)

Solution (Python):
n, k = map(int, input().split())
a = list(map(lambda s: int(s) - 1, input().split()))

cur = 0
while k > 0:
    if k % 2 == 1:
        cur = a[cur]
    next = [a[a[i]] for i in range(n)]
    a = next
    k //= 2
print(cur + 1)

Solution (Python):
import sys
readline = sys.stdin.readline
N,K = map(int,readline().split())
A = [0] + list(map(int,readline().split()))
c = 1
while K:
  if K & 1:
    c = A[c]
  A = [A[A[i]] for i in range(len(A))]
  K >>= 1
print(c)

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

visit = [-1]*(N+1)
history = []
v, cnt = 1, 0
while visit[v] == -1:
  visit[v] = cnt
  history.append(v)
  v = A[v-1]
  cnt += 1
period = cnt - visit[v]
l = visit[v]

if K < l:
  print(history[K])
else:
  K -= l
  K %= period
  print(history[l+K])

Solution (Python):

import collections
import itertools as it
import math

 


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




ord_ = [-1] * (n + 1)
s = []
v = 1
while ord_[v] == -1:
    ord_[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - ord_[v]
l = ord_[v]

if k < l:
    print(s[k])
else:
    k -= l
    k %= c
    print(s[l+k])

Solution (Python):
n,k=map(int,input().split())
a = list(map(int,input().split()))
visit = []
visit = [0]*n
pos=0
move=[]
roop=[]
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos]+=1
    pos = a[pos]-1
if len(move)>k:
    print(move[k]+1)
else:
    mod = (k-(len(move)-len(roop)))%len(roop)
    print(roop[mod]+1)

Solution (Python):




def main():
    
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    start = 0
    visited = {start}
    visited_list = [start]
    while True:
        start = a[start] - 1
        if start in visited:
            loop_start = start
            break
        visited_list.append(start)
        visited.add(start)
    p = visited_list.index(loop_start)
    loop = visited_list[p:]
    if k < p:
        print(visited_list[k] + 1)
    else:
        print(loop[(k - p) % len(loop)] + 1)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
n, k = map(int, input().split())
a = list(map(int, input().split())) 

visited = [None]*n
cur = 0
for i in range(k):
    if visited[cur] is None:
        visited[cur] = i
        cur = a[cur]-1
    else:
        break
else:
    print(cur+1)
    sys.exit()

k -= i
k %= i-visited[cur]

for i in range(k):
    cur = a[cur]-1

print(cur+1)

Solution (Python):
import sys
import math
import itertools

def main():
  n,k = map(int, input().split())
  a = list(map(int,sys.stdin.readline().split()))

  now = 0
  times = 0
  visit =[[0],[-1 for _ in range(n)]]
  visit[1][0] = 0
  roop = 0
  roop_start = 0

  for _ in range(k):
    times += 1
    now = a[now] - 1

    if visit[1][now] != -1:
      roop_start = visit[1][now] 
      roop = times - roop_start 
      break
    else:
      visit[0].append(now)
      visit[1][now] = len(visit[0]) - 1

  if roop > 0:
    k = (k - roop_start) % roop

  
  
  
  
  print(visit[0][roop_start + k] + 1)

  
  
  

  
  

  


if __name__ == '__main__':
  main()

Solution (Python):

from collections import Counter
from copy import deepcopy
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
A = list(map(int, input().split()))
connection ={}
for i,v in enumerate(A):
    connection[i+1] =v 


passed=[1]
nex =connection[1]
lap =[]
lap_num =0
pass_set =set(passed)
for i in range(1,N+1):
    passed.append(nex)
    if nex not in pass_set:
        pass_set.add(nex)
        nex =connection[nex]
    else:
        
        break

indx_s =passed.index(nex) +1
indx_f =len(passed)
lap_num =indx_f -indx_s

if indx_f < K:
    mod =(K -indx_s) % lap_num
    ans =passed[indx_s +mod]
else:
    ans =passed[K]
print(ans)

Solution (Python):
n,k = map(int, input().split())
a = list(map(int,input().split()))
pos = 0
visit = [0] * n
move = []
roop = []
while visit[pos] !=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = a[pos] - 1

if len(move)>k:
    print(move[k]+1)
else:
    print(roop[(k-(len(move)-len(roop)))%len(roop)]+1)

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

Town = []

rec = [-1] * (n+1)
idx = 1

while (rec[idx] == -1):
    rec[idx] = len(Town)
    Town.append(idx)
    idx = A[idx-1]


Exce = rec[idx]

Cycl = len(Town) - Exce

if Exce > k:
    print(Town[k])
else:
    Warp = (k - Exce) % Cycl
    print(Town[Exce+Warp])

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    A = LI_()

    ord = [-1]*N

    city = 0
    cnt = 0
    teleport_path = []
    while ord[city]==-1:
        ord[city] = cnt
        teleport_path.append(city)
        city = A[city]
        cnt += 1
    loop_head = ord[city]
    loop_length = len(teleport_path) - loop_head
    

    if K-1<loop_head:
        print(teleport_path[K]+1)
    else:
        K = loop_head + (K-loop_head)%loop_length
        print(teleport_path[K]+1)

if __name__ == '__main__':
    resolve()

Solution (Python):
N, K = map(int, input().split())
Alist = list(map(int, input().split()))


loop_num = 2 * 10 ** 5
loop_start_point = -1
point = 1
count_list = [-1] * N

for i in range(K):
    
    count_list[point - 1] = i
    nextPoint = Alist[point - 1]

    if count_list[nextPoint - 1] != -1:
        loop_start_point = nextPoint
        loop_num = i - count_list[loop_start_point - 1] + 1
        break
    
    point = nextPoint

if loop_start_point == -1:
    print(point)
else:
    point = loop_start_point
    progress_num = (K - count_list[loop_start_point-1]) % loop_num
    for i in range(progress_num):
        point = Alist[point - 1]
    
    
    
    print(point)

Solution (Python):



import sys

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    nw = 1
    lp0 = [1]
    lpl = {1}
    for i in range(N):
        nw = A[nw-1]
        if nw in lpl:
            break
        else:
            lp0.append(nw)
            lpl.add(nw)
    lpc = i+1 - lp0.index(nw)
    lp = lp0[-lpc:]

    if K<=len(lp0)-1:
        print(lp0[K])
    else:
        K-= len(lp0)-1
        K=K%lpc - 1
        print(lp[K])

if __name__ == '__main__':
    main()

Solution (Python):
N,K=map(int,input().split())
A=[~-int(x) for x in input().split()]

p,c=[-1]*N,0
for i in range(N+1):
  if p[c]==-1:
    p[c]=i
    c=A[c]
  else:
    break

print(1+p.index(p[c]+(K-p[c])%(i-p[c]) if K>=p[c] else K))

Solution (Python):
def main():
    
    IN = [int(_) for _ in input().split()]
    A = [int(_) for _ in input().split()]
    N = IN[0]
    K = IN[1]

    
    l = []
    l.append(1)
    
    nextInd = 1
    
    mark = [False] * N
    mark[0] = True
    
    startLoopCount = 0
    
    while True:
        nextInd = A[nextInd-1]
        if mark[nextInd-1]:
            startLoopCount = l.index(nextInd)
            break
        l.append(nextInd)
        mark[nextInd-1] = True
    
    move = l[0:startLoopCount]
    loop = l[startLoopCount:len(l)]

    
    
    
    
    if K < startLoopCount:
        print(move[K])
        return
    index = (K - startLoopCount) % len(loop)
    print(loop[index])
    return

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n, k = map(int, input().split())
    As = list(map(int, input().split()))
    i = 0
    visit = [0]*n
    move = []
    roop = []
    while visit[i] != 2:
        if visit[i] == 0:
            move.append(i)
        else:
            roop.append(i)
        visit[i] += 1
        i = As[i] - 1

    if len(move) > k:
        ans = move[k] + 1
    else:
        ans = roop[(k-(len(move)-len(roop))) % len(roop)]+1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
C=[1]
s=set([1])
for i in range(N):
  if A[C[i]-1] in s:
    l=C.index(A[C[i]-1])+1
    break
  else:
    C.append(A[C[i]-1])
    s.add(A[C[i]-1])
if K+1<=l:
  print(C[K])
else:
  print(C[l-1+(K+1-l)%(len(C)-l+1)])

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [1]+a
flg = [0] * (n + 1)

now = 0
i = 1
flg2 = 1
loop = 0
while (i < k+1):
    if (flg2 and flg[a[now]] != 0):
        flg2 = 0
        loop = i-flg[a[now]]
        i += ((k - i) // loop - 1) * loop
    now = a[now]
    flg[now] = i
    i += 1
print(a[now])

Solution (Python):
def resolve():
    '''
    code here
    '''
    import collections
    N, K = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]
    order_dict = dict(zip(range(1,N+1),A_list))

    foot_print = [-1 for _ in range(N+1)]
    loop_que = collections.deque([1])

    goto = 1

    while foot_print[goto] == -1:
        foot_print[goto] = 1
        goto = order_dict[goto]
        loop_que.append(goto) 

    loop_start = loop_que.index(goto) + 1
    loop_len = len(loop_que) - loop_start

    
    


    data_list = list(loop_que)
    if K >= len(loop_que):
        temp_K = K - loop_start + 1
        temp_K %= loop_len
        temp_loop = data_list[loop_start-1:-1]
        print(temp_loop[temp_K])
    else:

        print(data_list[K])

if __name__ == "__main__":
    resolve()

Solution (Python):
import sys

readline = sys.stdin.readline
n, k = map(int, readline().split())
A = [i - 1 for i in map(int, readline().split())]

m = 60
d = [A]
pre = A
for i in range(m):
    current = [pre[pre[j]] for j in range(n)]
    d.append(current)
    pre = current

route = 0
for i in range(m-1, -1, -1):
    if k >= (x := 1 << i):
        route = d[i][route]
        k -= x
print(route+1)

Solution (Python):
from collections import defaultdict
N,K=map(int,input().strip().split())
A=[0]+list(map(int,input().strip().split()))

G=defaultdict(list)
prev=1

visited=set()
visited.add(1)

next=A[1]
while(not next in visited):
    G[prev].append(next)
    prev=next
    visited.add(next)
    next=A[next]
G[prev].append(next)



origin=next
next=G[origin][0]
cnt=0 
while(next!=origin):
    cnt+=1
    next=G[next][0]

cnt+=1
origin1=1
next=G[1][0]
dist=0
while(next!=origin):
    dist+=1
    next=G[next][0]
dist+=1
if K<dist:
    cnt =0
    origin=1
    next=origin
    while(cnt!=K):
        cnt+=1
        next=G[next][0]
    print(next)
else:
    K=K-dist
    N=K%cnt

    cnt=0
    next=origin
    while(cnt!=N):
        cnt+=1
        next=G[next][0]
    print(next)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
input = sys.stdin.buffer.readline

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))





class Doubling(object):
    def __init__(self,A,M):
        self.N = len(A)
        self.kmax = M.bit_length()
        
        self.nex = [[-1]*self.kmax for _ in range(N)]
        for i in range(N):
            self.nex[i][0] = A[i]
        
        for k in range(1,self.kmax):
            for i in range(N):
                if self.nex[i][k-1] == -1:
                    self.nex[i][k] = -1
                else:
                    self.nex[i][k] = self.nex[self.nex[i][k-1]][k-1]
    
    
    def query(self,i,x):
        L = x.bit_length()
        for k in range(L)[::-1]:
            if i == -1:
                break
            if (x >> k) & 1:
                i = self.nex[i][k]
        return i

N,K = LI()
A = LI()
A = list(map(lambda x: x-1, A))

d = Doubling(A,K)
print(d.query(0,K)+1)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
input = sys.stdin.buffer.readline

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))





class Doubling(object):
    def __init__(self,A,M):
        self.N = len(A)
        self.kmax = M.bit_length()
        
        self.nex = [[-1]*self.kmax for _ in range(N)]
        for i in range(N):
            self.nex[i][0] = A[i]
        
        for k in range(1,self.kmax):
            for i in range(N):
                if self.nex[i][k-1] == -1:
                    self.nex[i][k] = -1
                else:
                    self.nex[i][k] = self.nex[self.nex[i][k-1]][k-1]
    
    
    def query(self,i,x):
        for k in range(self.kmax)[::-1]:
            if i == -1:
                break
            if (x >> k) & 1:
                i = self.nex[i][k]
        return i

N,K = LI()
A = LI()
A = list(map(lambda x: x-1, A))

d = Doubling(A,K)
print(d.query(0,K)+1)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
t=[[0 for i in range(n)] for j in range(64)]
for i in range(64):
    for j in range(n):
        if i == 0:
            t[i][j]=a[j]
        else:
            t[i][j]=t[i-1][t[i-1][j]-1]
ans=1
for i in range(64):
    r = k%2
    k = k//2
    if r == 1:
        ans = t[i][ans-1]
    if k == 0:
        print(ans)
        exit()

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
check=[[] for i in range(n)]
c=0
g=1
tmp=[]
cy=[]
non=[]

while c<n:
    if check[g-1]==[]:
        c+=1
        tmp.append(g)
        check[g - 1].append(c)
        g=a[g-1]
    else:
        cy=tmp[(check[g-1][0]-1):]
        non=tmp[:(check[g-1][0]-1)]
        break
if k<len(non):
    print(non[k])
    exit()
k-=len(non)
k%=len(cy)


print(cy[k])

Solution (Python):
from sys import stdin
import numpy as np
 
def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    A = [int(x) for x in stdin.readline().rstrip().split()]
    X = 1
    L = [1]
    if A[0] == 1:
        print(1)
        exit()
    if N >= K :
        for i in range(K):
            X = A[X-1]
        print(X)
        exit()
    else:
        for i in range(N):
            X = A[X-1]
            L += [X]
        
        C = np.zeros(N)
        for i in range(N):
            if C[L[i]-1] == 0:
                C[L[i]-1] += 1
                
            else:
                break
        
        
        
        S = L.index(L[i+1])
        
        
        
        K = (K - i-1)%(i+1 - S)
        
        print(L[S+K])
        
main()

Solution (Python):
def main():
    
    N,K = map(int, input().split())
    A = list(map(int, input().split()))

    
    
    
    now = 0
    history = [0]
    isVisited = [False for i in range(N)]
    i = 0
    while i<K:
        
        now = A[now]-1
        
        
        if isVisited[now] == False:
            history.append(now)
            isVisited[now] = True
            i+=1
        
        else:
            start = history.index(now)
            r = start + (K-1-i)%(len(history)-start)
            now = history[r]
            break

    print(now+1)

main()

Solution (Python):
import collections

def getls():
    return [ipt for ipt in input().split()]
def getlsi():
    return [int(ipt) for ipt in input().split()]

[n,k] = getlsi()
a = getlsi()

dic = collections.defaultdict(int)

now = 1

cnt = 1
while dic[now] == 0:
    dic[now] = cnt
    now = a[now-1]
    cnt += 1
lp = cnt - dic[now]
if k <= dic[now]:
    next = 1
    for i in range(k):
        next = a[next-1]
    print(next)
else:
    next = 1
    tmp = (k+1 - dic[now]) % lp
    
    for i in range(dic[now]+ tmp-1):
        
        next = a[next-1]
    print(next)

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

def subtraction(x):
    return x - 1
A = list(map(subtraction, A))


R = [0]*n
R[0] = 1
ROUTE = [0]
t = A[0]
while R[t] == 0:
    ROUTE.append(t)
    R[t] = 1
    t = A[t]

x = ROUTE.index(t) 
length = len(ROUTE) - x

if k < x:
    print(ROUTE[k]+1)
else:
    k = k - x
    s = k % length
    print(ROUTE[x + s]+1)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int,input().split()))
b = [0]*N
next_town = 1
for i in range(N+1):
    if b[next_town-1] != 0:
        start = next_town
        cycle = i - b[next_town-1]


        break
    b[next_town-1] = i
    next_town = A[next_town-1]
    
next_town = 1
if b[start-1] >= K:
    for i in range(K):
        next_town = A[next_town-1]
    print(next_town)
else:
    next_town = start
    for i in range((K-b[start-1])%cycle):
        next_town = A[next_town-1]
    print(next_town)

Solution (Python):
n,k = map(int,input().split())
A = [int(a) -1 for a in input().split()]

now = 0
if k <= n:
  for i in range(k):
    now = A[now]
  print(now+1)
  
else:
  V = [-1]*n
  V[0] = 0
  for i in range(1,n):
    now = A[now] 
    if V[now] >= 0:
      break
    V[now] = i 
      
  for j in range( (k-i)%(i-V[now])):
    now = A[now]
    
  print(now+1)

Solution (Python):
def main():
    n, k = map(int, input().split())
    a = [int(i) - 1 for i in input().split()]
    ans = solve(n, k, a)
    print(ans)


def solve(n, k, a):
    
    
    mk = 60
    dp = [[i for i in a] for _ in range(mk)]
    for i in range(1, mk):
        for j in range(n):
            dp[i][j] = dp[i - 1][dp[i - 1][j]]

    

    pos = 0
    for b in range(mk):
        if k >> b & 1:
            pos = dp[b][pos]
    return pos + 1


if __name__ == '__main__':
    main()

Solution (Python):
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
idx=1
idx_li=[]
ord_=[-1]*n
flag=False
for i in range(k):
    idx_li.append(idx)
    ord_[idx-1]=1
    idx=a[idx-1]
    if ord_[idx-1]==1:
        flag=True
        break
if flag:
    
    loop = idx_li[idx_li.index(idx):]
    
    size=len(loop)
    print(loop[(k-(len(idx_li)-len(loop)))%size])
else:
    print(idx)

Solution (Python):
N,K = map(int, input().split())
A_list = [0] + [int(e) for e in input().split()]

used_tele = 0
repeating_flag = False
moving_dict = dict() 
moving_dict[1]= 0
now_town = 1

while used_tele<K:
    next_town = A_list[now_town]
    used_tele += 1
    
    
    if next_town in moving_dict:
        repeating_flag = True
        break
        
    moving_dict[next_town] = used_tele
    now_town = next_town
        
if repeating_flag == False:
    print(next_town)
else:
    moving_list = list(moving_dict.keys())
    repeating_start_town_index = moving_list.index(next_town)
    
    
    
    
    
    repeating_moving_list = moving_list[repeating_start_town_index:]
    K -= len(moving_list[0:repeating_start_town_index])
    
    print(repeating_moving_list[K%len(repeating_moving_list)])

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=[[0 for j in range(60)] for i in range(n)]
for i in range(len(a)):
    a[i]-=1
    f[i][0]=a[i]

for j in range(1,60):
    for i in range(n):
        f[i][j]=f[f[i][j-1]][j-1]
ans=0
for j in range(59,-1,-1):
    if 1<<j<=k:
        ans=f[ans][j]
        k-=1<<j
print(ans+1)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))

order = [-1] * (n + 1)

s = []
v = 1
while order[v] == -1:
	order[v] = len(s)
	s.append(v)
	v = a[v - 1]
c = len(s) - order[v]
l = order[v]

if k < l:
	print(s[k])
else:
	k -= l
	k %= c
	print(s[l + k])

Solution (Python):
n, k = map(int, input().split())
a = list(map(int,input().split()))
timestamp = [0]*n
position = 1
period = 0
start=0
for i in range(n*2):
  if timestamp[position-1] != 0:
    start = timestamp[position-1]
    period = i-timestamp[position-1]
    break
  timestamp[position-1] =i
  position = a[position-1]
  

if k <=start:
  position = 1
  for i in range(k):
    position = a[position-1]
  print(position)
  
else:
  cycle = (k-start)%period
  for i in range(cycle):
    position = a[position-1]
  print(position)

Solution (Python):
n, k = map(int, input().split())
A = list(map(int, input().split()))
L = [1]
S = {1}

while A[L[-1]-1] not in S:
  last = L[-1]-1
  L.append(A[last])
  S |= {A[last]}

a = A[L[-1]-1]
j = L.index(a)
r = len(L)
l = r - j

if k+1 <= j:
    print(L[k])
else:
    print(L[j+(k-j)%l])