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):
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
vidited = [False] * (N+1)
ans = [1, A[1]]
vidited[1] = True
vidited[A[1]] = True
step = 1
n2i = {1:0, A[1]:1}
while step < K:
    next_a = A[ans[-1]]
    if vidited[next_a]:
        break
    
    
    step += 1
    n2i[next_a] = step
    vidited[next_a] = True
    ans += [next_a]


if step >= K:
    print(ans[K])
else:
    
    
    print(ans[n2i[next_a]+(K-n2i[next_a])%(step+1-n2i[next_a])])

Solution (Python):
n, k = map(int, input().split())
teleport_list = list(map(int, input().split()))
t_cycle = [1]
c_set = set([1])
now = 1
while len(t_cycle) == len(c_set):
  now = teleport_list[now-1]
  t_cycle.append(now)
  c_set.add(now)

start_cycle = t_cycle[-1]
before_cycle_len = t_cycle.index(start_cycle)
cycle_len = len(t_cycle) - t_cycle.index(start_cycle) - 1

if k <= before_cycle_len:
  print(t_cycle[k])
else:
  print(t_cycle[(k-before_cycle_len)%cycle_len + before_cycle_len])

Solution (Python):
def find_cycle_start(A, start=1):
    N = len(A)
    next_nodes = [a - 1 for a in A]
    start -= 1
    visited = [0 for _ in range(N)]
    node = start
    visited[node] = 1
    for _ in range(N):
        node = next_nodes[node]
        if visited[node] == 1:
            break
        visited[node] = 1
    return node + 1  


def main():
    N, K = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    cycle_start = find_cycle_start(A)
    offset = list()
    a = 1
    while True:
        a = A[a - 1]
        if a == cycle_start:
            break
        offset.append(a)
    cycle = [cycle_start]
    period = 0
    for _ in range(N):
        a = A[a - 1]
        period += 1
        if a == cycle_start:
            break
        cycle.append(a)
    
    if K <= len(offset):
        print(offset[K - 1])
    else:
        rest = K - len(offset)
        print(cycle[(rest - 1) % period])


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
from collections import deque

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

L=[]
chk=[0 for _ in range(N)]
tmp=0
while(1):
    if chk[tmp]==1:
        break
    chk[tmp]=1
    L.append(tmp)
    tmp=A[tmp]-1

loop_s=A[L[-1]]-1
loop_g=L[-1]
one_to_s=0
tmp=0



while(1):
    if tmp==loop_s:
        break
    tmp=A[tmp]-1
    
    one_to_s+=1

loop_l=0
tmp=loop_s
Loop=[loop_s]
while(1):
    if tmp==loop_g:
        break
    tmp=A[tmp]-1
    Loop.append(tmp)
    loop_l+=1
loop_l+=1

if K<=one_to_s:
    tmp=0
    
    cnt=0
    while(1):
        if cnt==K:
            break
        tmp=A[tmp]-1
        cnt+=1
    ans=tmp + 1
else:
    K-=one_to_s
    num=K%loop_l
    ans=Loop[num]+1

print(ans)

Solution (Python):
n,k = [int(e) for e in input().split(" ")[:2]]
a_ls = [int(e) for e in input().split(" ")[:n]]

a_dic = {i+1: a_ls[i] for i in range(n)}
v_f_dic = {i+1: False for i in range(n)}

pos = 1
v_ls = [pos]
v_f_dic[pos] = True

for i in range(k):
	
	pos = a_dic[pos]
	v_ls.append(pos)
	if v_f_dic[pos] is True:
		
		idx0, idx1 = [i for i,e in enumerate(v_ls) if e == pos][:2]
		rep_l = idx1 - idx0
		rep_ls = v_ls[idx0:idx1]
		ans_idx = (k-idx0)%rep_l
		print(rep_ls[ans_idx])
		import sys
		sys.exit()
	v_f_dic[pos] = True
print(pos)

Solution (Python):
n,k=map(int,input().split())
a=[0]
aa=list(map(int,input().split()))
for i in range(n):
    a.append(aa[i])
count=[0]*(n+1)
now=1
r=[]
kaburi=-1
for i in range(1,n+1):
    if count[now]==0:
        count[now]=1
        r.append(now)
        now=a[now]
    else:  
        kaburi=now
        break

syuki=len(r)
kaburi_i=0
for i in range(syuki):
    if kaburi==r[i]:
        kaburi_i=i
num=syuki-kaburi_i 

if k<kaburi_i:
    print(r[k])
else:
    ind=k-kaburi_i
    ind=ind%num
    print(r[kaburi_i+ind])

Solution (Python):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l1=[0]*(n+1)
l2=[1]
a=0
l1[1]=1
while 1:
    if l1[l[a]]==1:
        l2.append(l[a])
        break
    l2.append(l[a])
    l1[l[a]]=1
    a=l[a]-1
if k<=len(l2)-1:
    print(l2[k])
else:
    a=l2.index(l2[-1])
    b=len(l2)-a-1
    c=(k-len(l2)+1)%b
    print(l2[a+c])

Solution (Python):
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
now = 1
path = [1]
flag = []
for i in range(N):
  if(i==0):
    flag.append(1)
  else:
    flag.append(0)
for i in range(N):
  now = A[now - 1]
  if(flag[now - 1] == 0):
    path.append(now)
    flag[now - 1] += 1
  else:
    break
roop = [path[len(path) - 1]]
for i in range(N):
  now = A[roop[i] - 1]
  if(flag[now - 1] == 1):
    roop.append(now)
    flag[now - 1] += 1
  else:
    break
roop.pop(0)
if(K <= len(path) - 1):
  print(path[K])
elif(K == len(path)):
  print(roop[0])
else:
  print(roop[(K - len(path))%len(roop)])

Solution (Python):
n,k=map(int,input().split())
a,c=list(map(lambda x: int(x)-1,input().split())),0
for _ in range(60):
  if k%2:
    c=a[c]
  k//=2
  a=[a[a[i]]for i in range(n)]
print(c+1)

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

ls = [1]
s = {1}
cnt = 0
flag = 0
for i in range(K):
    t = A[ls[-1]-1]
    if t in s:
        nls = ls[ls.index(t):]
        flag = 1
        break
    else:
        ls.append(t)
        s.add(t)
        cnt += 1
        
if flag == 1:
    nokori = K-cnt-1
    amari = nokori % len(nls)
    print(nls[amari])
else:
    print(ls[-1])

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

otozureta = []

otozureta2 = [0]
for i in range(n):
    otozureta.append(False)
otozureta[0] = True

flag = False
i = 0
loop = 0
looppoint = 0

while not flag:
    genzai = otozureta2[i]
    next = a[genzai] - 1
    otozureta2.append(next)

    if not otozureta[next]:
        otozureta[next] = True
        i = i + 1
    else:
        loop = i - otozureta2.index(next) + 1
        looppoint = otozureta2.index(next)
        flag = True

if k < looppoint:
    print(otozureta2[k] + 1)
else:
    print(otozureta2[((k - looppoint) % loop) + looppoint] + 1)

Solution (Python):
def ma():
    return map(int,input().split())
def main():
    n,k=ma()
    A=list(ma())
    ans=[]
    exp=[0]*(n+1)
    now=1
    
    while True:
        ans.append(now)
        exp[now]=1
        now=A[now-1]
        if exp[now]==1:
            use=ans.index(now)
            tool=len(ans)-use
            break 

    if use>=k:
        print(ans[k])
        exit()
    else:
        p=(k-use)%tool
        print(ans[use+p])

main()

Solution (Python):
N, K = map(int, input().split())
a = list(map(int, input().split()))
chk = [0] * N
start = -1
ido = [0]
chk[0] = 1
index = 0
cnt = 0

while True:
    index = a[index] - 1
    if chk[index] == 0:
        chk[index] += 1
        cnt += 1
        ido.append(index)
    else:
        start = index
        break

if cnt >= K:
    print(ido[K] + 1)
    exit()
tmp = ido.index(start)
ido = ido[tmp:]
K -= tmp

print(ido[K % len(ido)] + 1)

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, K = read_ints()
    A = [a-1 for a in read_ints()]
    distance = [-1]*N
    i = 0
    k = K
    while k > 0:
        if distance[i] != -1:
            cycle_length = K-k-distance[i]
            k %= cycle_length
            if k == 0:
                break
        else:
            distance[i] = K-k
        i = A[i]
        k -= 1
    return i+1


if __name__ == '__main__':
    print(solve())

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    went = [0] * N
    went[0] = 1
    Flag = True
    check = [0]
    cnt = 1
    now = 0
    while Flag and (cnt <= K + 1):
        now = A[now] - 1
        if went[now] == 1:
            ind = check.index(now)
            if ind == 0:
                cnt = len(check)
            else:
                cnt = len(check) - ind

            Flag = False
        else:
            check.append(now)
            went[now] = 1
            cnt += 1
            continue

    if len(check) > K:
        print(check[K] + 1)
    else:
        if ind == 0:
            K = K % cnt
            print(check[K] + 1)
        else:
            ind -= 1
            K = (K - ind - 1) % cnt
            print(check[ind + K + 1] + 1)


if __name__ == "__main__":
    main()

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

List=[0]*n
pattern=[1]
now=0
nex=0
List[0]=1
end_flag=0

while end_flag==0:
    nex=a[now]
    if (List[nex-1]==1):
        target1=nex
        end_flag=1
        break
    else:
        List[nex-1]+=1
        pattern.append(nex)
        now=nex-1

left=pattern.index(target1)
right=len(pattern)
kaisu=right-left
kaisu2=(k-right)%kaisu


if (k<=right-1):
    print(pattern[k])
else:
    print(pattern[left+kaisu2])

Solution (Python):
import sys
import math
from  collections import defaultdict




def solve(test):
	n, k = map(int, input().split())
	a = list(map(int, input().split()))
	fast = 1
	slow = 1
	fast = a[a[fast - 1] - 1]
	slow = a[slow - 1]
	while fast != slow:
		fast = a[a[fast - 1] - 1]
		slow = a[slow - 1]

	fast = 1
	count = 0
	while fast != slow:
		fast = a[fast - 1]
		slow = a[slow - 1]
		count += 1

	res = 1
	if k < count:
		while k:
			res = a[res - 1]
			k -= 1
		print(res)
		return
	k -= count

	cycle = 1
	fast = a[fast - 1]
	while fast != slow:
		fast = a[fast - 1]
		cycle += 1

	k %= cycle

	while k:
		fast = a[fast - 1]
		k -= 1

	print(fast)







if __name__ == "__main__":
	test_cases = 1
	for t in range(1, test_cases + 1):
		solve(t)

Solution (Python):
def resolve():
    n, g = map(int, input().split())
    a = list(map(int, input().split()))
    ar = [-1] * n
    k = 0
    now = 0
    for i in range(n+1):
        if ar[now] == -1:
            ar[now] = k
            now = a[now]-1
            k += 1
        else:
            start = ar[now]
            startnode = now
            end = k
    if g <= start:
        ans = 0
        for i in range(n):
            if ar[i] == g:
                ans = i+1
    else:
        g -= start
        g %= (end-start)
        now = startnode
        for i in range(g):
            now = a[now]-1
        ans = now + 1
    print(ans)
resolve()

Solution (Python):
n,k=map(int,input().split())
A=list(map(int,input().split()))
cnt=0
s=1
M=[0]
B=[0]*(n+1)
while cnt<n:
    s=A[s-1]
    if B[s-1]==0:
        B[s-1]+=1
    else:
        imp=s-1
        break
    M.append(s-1)
    cnt+=1
x=M.index(imp)
y=len(M)-x

if k<=x:
    print(M[k]+1)
else:
    N=M[x:]
    print(N[(k-x)%y]+1)

Solution (Python):
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
already=[0]*(n+1)
already[1]=1
history=[1]
tmp=1
now=0
jump=1
while jump:
  now+=1
  tmp=a[tmp]
  history.append(tmp)
  if now==k:
    ans=tmp
    break
  elif already[tmp]==1:
    jump=0
    past=history.index(tmp)
    loop=now-past
    ans=history[past+(k-now)%loop]
    break
  else:
    already[tmp]=1

print(ans)

Solution (Python):
N, K = map(int, input().split())
A = [int(a) - 1 for a in input().split()]
visited = [False] * N
if N >= K:
    ans = 0
    while K > 0:
        ans = A[ans]
        K -= 1
    print(ans + 1)
    exit()
cur = 0
visited[cur] = True
nxt = A[cur]
turn = 1
path = [[cur, turn]]
while True:
    if visited[nxt]:
        break
    cur = nxt
    visited[cur] = True
    nxt = A[cur]
    turn += 1
    path.append([cur, turn])
st = A[path[-1][0]]
st_turn = 1
for x in path:
    if x[0] == st:
        st_turn = x[1]
        break
fq = path[-1][1] - st_turn + 1
go = (K - (st_turn - 1)) % fq
ans = nxt
while go > 0:
    ans = A[ans]
    go -= 1
print(ans + 1)

Solution (Python):
N, K = map(int, input().split())
a = list(map(int, input().split()))
chk = [0] * N
start = -1
ido = [0]
chk[0] = 1
index = 0
cnt = 0
while True:
    index = a[index] - 1
    if chk[index] == 0:
        chk[index] += 1
        cnt += 1
        ido.append(index)
    else:
        start = index
        break

if cnt >= K:
    print(ido[K] + 1)
    exit()
tmp = ido.index(start)
ido = ido[tmp:]
K -= tmp
print(ido[K % len(ido)] + 1)

Solution (Python):
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))
d = dict()
c = 1
loop = False
k = 0
while(k<K):

    if not loop and str(c) in d:
        T = k-d[str(c)]
        
        a =  max((((K-k) // T)) - 1,0)
        
        k += a*T
        
        loop = True
    d[str(c)] = k
    c = A[c]
    k+=1
print (c)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  ans=[[] for _ in range(n)]
  basho=0
  f=inf
  for i in range(2*n+1):
    if f==inf and len(ans[basho])>1:
      f=basho
    ans[basho].append(i)
    basho=l[basho]-1

  
  

  if k<(2*n+1):
    for i,x in enumerate(ans):
      if k in x:
        return i+1

  k-=ans[f][0]
  span=ans[f][2]-ans[f][1]
  
  xx=k%span
  
  start=f
  while xx>0:
    start=l[start]-1
    xx-=1

  return start+1


print(main())

Solution (Python):




def main():
    
    N, K = map(int, input().split())
    A = [int(a) for a in input().split()]

    count = 0
    now = 1
    visited = [-1] * N
    visited[now - 1] = 0 
    while count < K:
        now = A[now - 1]
        count += 1
        if visited[now - 1] == -1:
            visited[now - 1] = count
        else:
            loop = count - visited[now - 1]
            rest = (K - visited[now - 1]) % loop
            
            if rest == 0:
                break
            for i in range(len(visited)):
                if visited[i] == visited[now - 1] + rest:
                    now = i + 1
                    count = K 
                    break
    
    print(now)




    pass

if __name__ == '__main__':
    main()

Solution (Python):
def solve(N, K, A):
    slow, fast = 1, 1
    is_first = True
    while is_first or slow != fast:
        is_first = False
        slow = A[slow - 1]
        fast = A[A[fast - 1] - 1]
    fast = 1
    steps_before_entering_cycle = []
    while slow != fast:
        steps_before_entering_cycle.append(fast)
        slow = A[slow - 1]
        fast = A[fast - 1]

    if K < len(steps_before_entering_cycle):
        return steps_before_entering_cycle[K]

    cycles = [slow]
    while A[slow - 1] != cycles[0]:
        cycles.append(A[slow - 1])
        slow = A[slow - 1]
    ret = cycles[(K - len(steps_before_entering_cycle)) % len(cycles)]
    return ret

if __name__ == '__main__':
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    print(solve(N, K, A))

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
a = LI()
check = [-1]*n
cntr = 0
que = deque([0])
while que:
    x = que.popleft()
    if check[x] != -1:
        break
    check[x] = cntr
    cntr += 1
    que.append(a[x]-1)
c = check[x]
if k < cntr:
    for i in range(n):
        if check[i] == k % cntr:
            print(i+1)
            quit()
    
check = [-1]*n
cntr = 0
que = deque([x])
while que:
    x = que.popleft()
    if check[x] != -1:
        break
    check[x] = cntr
    cntr += 1
    que.append(a[x]-1)

for i in range(n):
    if check[i] == (k-c) % cntr:
        print(i+1)

Solution (Python):
N, K = [int(v) for v in input().strip().split(" ")]
A = [int(v) for v in input().strip().split(" ")]

visited = set()
epoch = [0] * N
round = -1


now = 0
epoch[0] = -1
visited.add(now)
for i in range(K):
    now = A[now] - 1
    if now not in visited:
        visited.add(now)
        epoch[now] = i
    else:
        round = i - epoch[now]
        break

K -= (i + 1)

K = K % round

for _ in range(K):
    now = A[now] - 1

print(now + 1)

Solution (Python):
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

visited = [0] * N
order = [0]
visited[0] = 1

pos = 0
while True:
    next_town = A[pos]
    
    if visited[next_town] == 1:
        order.append(next_town)
        break
    visited[next_town] += 1
    pos = next_town
    order.append(pos)

idx = order.index(order[-1])
l_l = len(order) - idx - 1
l = len(order) -1 - l_l

if K < len(order):
    print(order[K] + 1)
    sys.exit()
    
K -= l
K %= l_l

print(order[l+K]+1)

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

count=[0]*(n+1)
cnt=0
i=1
while count[i]==0:
    count[i]=cnt
    if cnt==k:
        print(i)
        exit()
    cnt+=1
    i=a[i]

if cnt==k:
    print(i)
    exit()



loop=cnt-count[i]

goal=(k-cnt)%loop

if goal==0:
    print(i)
else:    
    print(count.index(count[i]+goal))

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

went={i:0 for i in range(1,n+1)}

now=1
cnt=1
for i in range(n+1):
    if went[now]>0:
        t=cnt-1
        s=went[now]
        break
    else:
        went[now]=cnt
        now=A[now-1]
        cnt+=1


if k <= t-1:
    val=k+1
else:
    val=((k+1-t)%(t-s+1)-1)%(t-s+1)+s
for ele in went:
    if went[ele]==val:
        print(ele)
        break

Solution (Python):
n,k=map(int,input().split())
l=list(map(int,input().split()))
d={}
for i in range(0,len(l)):
    d[i+1]=l[i]
ans=1
s=set()
seq=[]
e=0
for i in range(0,k):
    ans=d[ans]
    if ans in s:
        seq.append(ans)
        break
    else:
        s.add(ans)
    seq.append(ans)
else:
    print(ans)
    e=1
if e==0:
    b = seq.index(seq[-1])
    rep = seq[b:-1]
    sl = len(seq) - seq.index(seq[-1]) - 1
    k = k - b - 1
    ans = seq[b]
    x = k % sl
    
    print(rep[x])

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

now=0

been = [0]*n
been[0]=1
for i in range(n):
    now=a[now]
    if been[now]==0:
        been[now]=1
    else:
        t_stack=sum(been)
        break

start=now

been = [0]*n
been[now]=1
for i in range(n):
    now=a[now]
    if been[now]==0:
        been[now]=1
    else:
        period=sum(been)
        break

        
if k<=t_stack:
    now=0
    for i in range(k):
        now=a[now]
    print(now+1)
    
else:
    n_itr = (k-t_stack+period)%period
    now=start
    for i in range(n_itr):
        now=a[now]
    print(now+1)

Solution (Python):

import numpy as np
from collections import deque
import collections

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

found = set()
current = 1
log = []
while current not in found:
    found.add(current)
    log.append(current)
    current = a[current - 1]
for i, _log in enumerate(log):
    if (current == _log):
        loop_start = i
        loop_len = len(log) - i
        break





if (k < loop_start):
    print(log[k])
else:
    k = k - (loop_start)  
    k = k % (loop_len)  
    print(log[loop_start+k])

Solution (Python):
import sys

input = sys.stdin.readline


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

    visited = [False] * N
    visited[0] = True
    path = [1]
    loop_s_idx = -1
    loop_e_idx = -1
    a = 1
    while True:
        a = A[a - 1]
        if visited[a - 1]:
            loop_s_idx = path.index(a)
            loop_e_idx = len(path) - 1
            break
        else:
            visited[a - 1] = True
            path.append(a)

    if K < len(path):
        ans = path[K]
    else:
        loop_length = loop_e_idx - loop_s_idx + 1
        q, r, = divmod(K - loop_s_idx, loop_length)
        ans = path[loop_s_idx + r]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n,k=n1()
a=[0]
a.extend(n1())

from collections import deque
p=deque([1])
nex=1
town=[0]*(n+1)
town[1]=1
for i in range(n+1):
    last=nex
    nex=a[last]
    p.append(nex)
    if town[nex]==1:
        break
    else:
        town[nex]=1
p.pop()
first=p.index(nex)
length=len(p)
if k<length:
    print(p[k])
else:
    print(p[(k-first)%(length-first)+first])

Solution (Python):











N, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

a = [False] * len(A)
now = 1
arrive_list = []

while True:
    arrive_list.append(now)
    if a[now-1]:
        break
    else:
        a[now-1] = True
    now = A[now-1]

r = arrive_list.index(now)
K -= r

if K <= 0:
    print(arrive_list[K+r])
    exit(0)

K %= (sum(a)-r)
print(arrive_list[K+r])

Solution (Python):




import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    n,k=map(int, readline().rstrip().split())
    aa=list(map(int, readline().rstrip().split()))
    p=0

    if k<n:
        for i in range(k):
            p=(aa[p]-1)
        print(p+1)
        return

    memo=[-1]*n
    memo[0]=0
    for i in range(n*2):
        p=(aa[p]-1)
        if memo[p]==-1:
            memo[p]=i+1
        else:
            diff=(i+1)-memo[p]
            re=k-i-1
            re=re%diff
            for j in range(re):
                p=(aa[p]-1)
            print(p+1)
            return


    
    
    
    



    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):
import sys
import math
from collections import deque
import heapq
import itertools
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def tami(x, cnt):
  if visited[x-1] != -1:
    return [x, visited[x-1], cnt-visited[x-1]]
  else:
    visited[x-1] = cnt
    return tami(a[x-1], cnt+1)

def nasu1(x, cnt):
  if cnt == K:
    return x
  else:
    return nasu1(a[x-1], cnt+1)

def nasu2(x, cnt):
  if cnt == num:
    return x
  else:
    return nasu2(a[x-1], cnt+1)

N,K = IL()
a = IL()

visited = [-1]*N
i, start, roop = tami(1, 0)
if K <= start:
  print(nasu1(1, 0))
else:
  
  num = (K-start)%roop
  

  print(nasu2(i, 0))

Solution (Python):
from collections import defaultdict

graph = defaultdict(int)
N, K =list(map(int, input().split())) 
arr = list(map(int, input().split()))
for i, v in enumerate(arr):
    graph[i+1] = v

if graph[1] == 1:
    print(1)
    exit()

cnt = 1
cur = graph[1]


cnt2node, node2cnt = defaultdict(int), defaultdict(int)
cnt2node[0], cnt2node[1] = 1, cur
node2cnt[1], node2cnt[cur] = 0, 1
while True:
    cur = graph[cur]
    cnt += 1
    if cur in node2cnt:
        break
    else:
        cnt2node[cnt] = cur
        node2cnt[cur] = cnt
        
first = node2cnt[cur]
if K <= first:
    print(cnt2node[K])
else:
    mod = (K - first) % (cnt - first)
    print(cnt2node[first + mod])

Solution (Python):


n,k=map(int, input().split())
a=list(map(int, input().split()))
isUsed=[False]*n

path = [1]


i = 1
while(True):
    idx = i-1
    if isUsed[idx]: 
        
        
        start = path.index(i)
        break
    else:
        isUsed[idx] = True
        path.append(a[idx])
        
        i = a[idx]


if k >= start:
    
    
    num = len(path) - 1 - start
    lis_mod = path[start:start+num]

    mod = len(lis_mod)

    
    tmp = (k-start)%mod
    
    print(lis_mod[tmp])
else:
    print(path[k])

Solution (Python):
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction



mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]


n, k = sp()
arr = l()
answer = [1]
s = {1}
index = 1
while True:
    temp = arr[answer[-1]-1]
    if temp in s:
        index = temp
        break
    s.add(temp)
    answer.append(temp)
if k <= len(answer)-1:
    out(answer[k])
    exit()
k -= (len(answer)-1)
temp = answer.index(index)
answer = answer[temp:]
k %= len(answer)
if k == 0:
    k = len(answer)
out(answer[k-1])

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

ma = [-1] * N
ma[0] = 0
now = 1
ne = 0

hazi = -1
for i in range(K):
    ne = A[ne] - 1
    if ma[ne] >= 0:
        hazi = ma[ne]
        break

    ma[ne] = now
    now += 1

if hazi == -1:
    print(ne + 1)
    exit()

ans = (K - hazi) % (now - hazi)

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

print(ne + 1)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] -= 1
li = [False] * N
f = True
now = 0
l = []
l2 = [0] * N
i = 0
while f:
    l.append(now)
    li[now] = True
    l2[now] = i
    if li[A[now]]:
        f = False
        num = len(l) - l2[A[now]]
        n = l2[A[now]]
    i += 1
    now = A[now]
k = (K-n) % num
print(l[min(K, n+k)]+1)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
roop=[]
roop_set=set([])

a.insert(0, 0)
s=1
for i in range(k):
    s=a[s]
    if(s not in roop_set):
        roop.append(s)
        roop_set.add(s)
    else:
        s_first=roop.index(s)
        roop=roop[s_first:]
        k-=(s_first+1)

        print(roop[k%len(roop)])
        break
else:

    print(roop[k-1])

Solution (Python):
import math,sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda:int(input())
li = lambda:list(map(int,input().split()))

def case():
    n,k=li()
    a=li()
    d=defaultdict(int)
    i=0
    d[1]=1
    c=0
    ans=[1]
    while(a[i] not in d):
        d[a[i]]=1
        c+=1 
        ans.append(a[i])
        i=a[i]-1
   
    
    if(k<len(ans)):
        print(ans[k%len(ans)])
        return
    for j in range(len(ans)):
        if(ans[j]==a[i]):
            ans=ans[j:]
            k-=j
            break 

    
    print(ans[(k)%len(ans)])
    




for _ in range(1):
    case()

Solution (Python):
n, k = map(int, input().split())
lst = list(map(int, input().split()))
lst = [0] + lst
count = [0]*(n+1)
pat1 = 0
now = 1
while True:
  count[now] += 1
  if count[now] == 2:
    break
  now = lst[now]
  pat1 += 1


cur = 1
pat2 = 0
while True:
  if cur == now:
    break
  cur = lst[cur]
  pat2 += 1

    

if k > pat2:
  k -= pat2
  k = k % (pat1-pat2)
  k += pat2
  
now = 1
for i in range(k):
  now = lst[now]
  
print(now)

Solution (Python):
n,k = (int(i) for i in input().split())
A = [0]+[int(i) for i in input().split()]

if k<=n:
	ans = 1
	for i in range(k):
		ans = A[ans]
	print(ans)
	exit()

boucle = 1
vis = [False]*(n+1)
vis[1] = True
x = 1
while not vis[A[x]]:
	x = A[x]
	vis[x] = True
	boucle+=1

last = A[x]
sub = 0
d = 1
while d!=last:
	sub+=1
	d=A[d]
boucle-=sub



k-=sub
reste = k%boucle
ans = last
for i in range(reste):
	ans = A[ans]
	


print(ans)

Solution (Python):
N, K = map(int, input().split())
arr = list(map(int, input().split()))
check = [0]*N
index = 0
cnt = 0
while True:
    if cnt == K:
        print(index+1)
        exit()
    if check[index] == 1:
        break
    check[index] = 1
    index = arr[index]-1
    cnt += 1

index2 = 0
first_cnt = 0
while True:
    if index2 == index:
        break
    index2 = arr[index2]-1
    first_cnt += 1

ans_cnt = ((K-first_cnt) % (cnt-first_cnt))+first_cnt
index = 0
cnt = 0
while True:
    if cnt == ans_cnt:
        break
    index = arr[index]-1
    cnt += 1
print(index+1)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().strip()
n,k = map(int,input().split())
a = list(map(int,input().split()))
sala = set([0,a[0]-1])
f = 0
s = a[0]-1
power = 1
length = 1
while f!=s:
    if length==power:
        power*=2
        length=0
        f = s 
    s = a[s]-1
    sala.add(s)
    length+=1
start = len(sala)-length
if k<=start:
    cur = 0 
    while k:
        k-=1
        cur = a[cur]-1
    print(cur+1)
else:
    k-=start 
    k%=length
    f = 0
    s = 0 
    while length:
        s = a[s]-1
        length-=1
    while s!=f:
        s = a[s]-1
        f = a[f]-1
    cur = f 
    while k:
        cur = a[cur]-1
        k-=1
    print(cur+1)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
visited=[0 for i in range(N)]
visited[0]=1
tour=[1]

while True:
    goto=A[tour[-1]-1]
    tour.append(goto)
    if visited[goto-1]:
        break
    visited[goto-1]=1


    
before=0
shuuki=1
for i in range(len(tour)):
    if tour[i]==tour[-1]:
        before=i
        shuuki=len(tour)-i-1
        break



if K<len(tour):
    print(tour[K])
else:
    K-=before
    K%=shuuki
    print(tour[before+K])

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

counter=1
now=1
gone_set={1}
gone_list=[1]
place=1
while counter<=K:
    next=A[now]
    place=next
    if next in gone_set:
        start=gone_list.index(next)
        loop=len(gone_list)-start
        last=(K-start)%loop
        place=gone_list[start+last]
        break
    else:
        gone_set.add(next)
        gone_list.append(next)
        now=next
        counter+=1

print(place)