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:
import sys
from collections import deque

N, K = map(int, sys.stdin.readline().strip().split())
tmp = list(map(int, sys.stdin.readline().strip().split()))

A = [a-1 for a in tmp]

visited = set()
loop = []

q = deque()
q.append(0)
start = float("inf")
end = float("inf")

count = 0
while q and count < K:
    s_town = q.popleft()
    # ループを確認
    if s_town in visited:
        start = loop.index(s_town)
        end = len(loop)
        break
    visited.add(s_town)
    loop.append(s_town)
    n_town = A[s_town]
    q.append(n_town)
    count += 1

# print(start, end)
if start >= K:
    print(loop[K]+1)
else:
    res = K - start
    res %= (end - start)
    # print("res", res)
    print(loop[start + res]+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,inpur().split()))
m = 1
for i in range(k%n):
  m = a[m-1]
print(m)

Submission status: Runtime Error

Solution code:
¥from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from operator import mul


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
A = LI()
now = 0
if k < n:
    for j in range(k):
        now = A[now] - 1
    print(now + 1)
    exit()


D = {0:-1}
for i in range(n):
    now = A[now] - 1
    if now in D:
        break
    D[now] = i


k -= (i + 1)
b = now
c = k % (i - D[now])
for j in range(c):
    b = A[b] - 1

print(b + 1)

Submission status: Runtime Error

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

n = 1
rooper = -1
L = []
for i in range( K ):
  n = A[ n - 1 ]
  if n in L:
    rooper = n
    break
  else:
    L.append( n )
index = L.index( rooper )
a = ( K - index ) % ( len( L ) - index )
print( L[ index + a - 1 ] )

Submission status: Runtime Error

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

been = [0]*n

now=0
been[0]=1
for i in range(k):
    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(k):
    now=a[now]
    if been[now]==0:
        been[now]=1
    else:
        period=sum(been)
        break

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

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = [i-1 for i in A]

flag = [False]*N
from collections import deque

q = deque([])
q.append(0)
flag[0] = True
now = 0
while True:
  now = A[now]
  if flag[now]:
    break
  else:
    flag[now] = True
    q.append(now)
    
l = list(q)
if K<=len(l):
  print(l[K]+1)

else:
  for i,x in enumerate(l):
    if x==now:
      l = l[i:]
      K -= i
      break
  
  print(l[K%len(l)]+1)

Submission status: Runtime Error

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

p_list = [1]
ne_p = a[0]
for _ in range(k):
    p_list.append(ne_p)
    ne_p = a[ne_p-1]
    if ne_p in p_list:
        ind = p_list.index(ne_p)
        break
k = k - ind
print(p_list[ind + k%(len(p_list)-ind)])

Submission status: Runtime Error

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

A = list(map(int,input().split()))
t = 0
c = 0
ans = [0] * n
L = []
for _ in range(k):
    if ans[t] == 1:
        t += k%c
        t = L[t]
        break
    else:
        ans[t] = 1
        L.append(t)
        t = A[t]-1
        c += 1

print(t+1)

Submission status: Runtime Error

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

start=1

list=[1]
for i in range(k):
  start-=1
  next=a[start]
  start=next

  if start in list:
    zan=k-i
    break
  else:
    list.append(start)

zan-=1
zan%=len(list)-1
if zan==0:
  print(list[-1])
else:
  print(list[zan])

Submission status: Runtime Error

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

A = list(map(int,input().split()))
t = 0
c = 0
ans = [0] * n
L = []
for _ in range(k):
    if ans[t] == 1:
        t += k%c
        t = L[t]
        break
    else:
        ans[t] = 1
        L.append(t)
        print(L)
        t = A[t]-1
        c += 1

print(t+1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    N, K = IL(int)
    A = IL(lambda x: int(x) - 1)
    memo = []
    i = 0
    while(i < N and A[i] != 0):
        memo.append(A[i])
        i += 1
    print(memo[K % len(memo)] + 1)


solve()

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
Kn = [0]*N
l = []
p = 0
i = 1
while True:
    Kn[p] = 1
    # print(p)
    if Kn[A[p]-1] == 1:
        break
    elif i == K:
        print(p+1)
    l.append(A[p])
    p = A[p]-1
    i += 1
if N % len(l) == 0:
    print(l[len(l)-1])
else:
    print(l[N % len(l)-1])

Submission status: Runtime Error

Solution code:
def resolve():
    N,K = map(int,input().split())
    A = [int(i)-1 for i in input().split()]
    visited =[False]*N
    steps = [K for i in range(N)]

    current = 0
    while True:

        if K == 0:
			print(current+1)
            return
        if not visited[current]:
            visited[current]=True
            steps[current]=K
            current=A[current]
            K-=1
        else:
            K= K % (steps[current]-K)
            break
    
    while K != 0:

        current= A[current]
        K-=1
    print(current+1)




if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    from sys import stdin
    # from collections import defaultdict
    # from collections import deque
    # from collections import Counter
    import numpy as np
    # import math
    # import random
    input = stdin.readline


    n, k = map(int, input().split())
    aa = [0]+list(map(int, input().split()))
    arrive = [1]
    next = aa[1]
    visited = set([1])
    if next == 1:
        print(1)
        exit()
    while not (next in visited):
        arrive.append(next)
        visited.add(next)
        next = aa[next]
    num_town = len(arrive)
    start_index = arrive.index(next)
    if k <= num_town:
        print(arrive[k])
        exit()
    print(arrive[start_index+(k-start_index)%(num_town-start_index)])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,inpur().split()))
m = 1
for i in range(k):
  m = a[m-1]
print(m)

Submission status: Runtime Error

Solution code:
from collections import Counter
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))


visited = [0]*n


def dfs(v):
    if visited[v]+1 == 3:
        return 0
    visited[v] += 1
    dfs(A[v])


dfs(0)

c = Counter(visited)
loop_cnt = c[2]
init_cnt = c[1]
k -= init_cnt
k %= loop_cnt

cnt = 0
visited = [0]*n


ans = 0
for _ in range(k+init_cnt):
    ans = A[ans]
print(ans+1)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()
a = na()
dp = [-1]*n
i = 1
now = 0
loop = -1
start = -1

while True:
    now = a[now]-1
    if i == k:
        print(now+1)
        exit()
    if dp[now] == -1:
        dp[now] = i
    else:
        loop = i - dp[now]
        start = now
        break
    i+=1

print(dp.index((k-start)%loop+start)+1)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

d = defaultdict(list)
root = []
p = 1

s, e = 0, 0

for i in range(K):
    d[p].append(i)
    root.append(A[p - 1])
    if len(d[p]) == 2:
        s = d[p][0]
        e = i
        break
    p = A[p - 1]
#print(s, e)

#print(root)

n = (K - s) % (e - s)
#print(n)
print(root[s + n - 1])

Submission status: Runtime Error

Solution code:
from collections import Counter
n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))


visited = [0]*n


def dfs(v):
    if visited[v]+1 == 3:
        return 0
    visited[v] += 1
    dfs(A[v])


dfs(0)

c = Counter(visited)
loop_cnt = c[2]
init_cnt = c[1]
k -= init_cnt
k %= loop_cnt

cnt = 0
visited = [0]*n


ans = 0
for _ in range(k+init_cnt):
    ans = A[ans]
print(ans+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,inpur().split()))
k = 1
for i in range(k):
  k = a[k-1]
print(k)

Submission status: Runtime Error

Solution code:
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):
        now = A[now] - 1
        if went[now] == 1:
            ind = check.index(now) - 1
            cnt = cnt - ind - 1
            Flag = False
        else:
            check.append(now)
            went[now] = 1
            cnt += 1
            continue
    if len(check) >= K:
        print(check[K] + 1)
    else:
        K = (K - ind) % cnt
        print(check[ind + K] + 1)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

for i in range(1,min(K+1,100)):
    goto=A[tour[-1]-1]
    tour.append(goto)
    if visited[goto-1]:
        break
    visited[goto-1]=1

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

#print(before,shuuki)

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

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]
N = []
t = 0
cnt = 0
for i in range(k):
    if t in N:
        break
    N.append(t)
    t = A[t]
    cnt += 1
k -= cnt
tmp = N.index(t)
S = N[tmp:]
s_len = len(S)
ind = k % s_len
print(S[ind] + 1)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
l=list(map(int, input().split()))
l1=[]
l1.append(1)
num=l[0]
while len(l1)<=min(2*len(l),k):
    l1.append(num)
    num=l[num-1]
m=len(l1)
val=k%m
ans=l1[val]
if 1 not in l:
    print(l1[val+1])
else:
    print(l1[val])

Submission status: Runtime Error

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

length = [-1] * n
path = [-1, -1]
route = []


def func(now, depth):
    global path, length, a, route
    route.append(now)
    if length[now] == -1:
        length[now] = depth
        func(a[now]-1, depth+1)
    else:
        path = [length[now], depth - length[now]]


func(0, 0)

print(route[path[0]+(k-path[0]) % path[1]]+1)

Submission status: Runtime Error

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

for i in range(1,100):
    goto=A[tour[-1]-1]
    tour.append(goto)
    if visited[goto-1]:
        break
    visited[goto-1]=1

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

#print(before,shuuki)

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

Submission status: Runtime Error

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

tmp = 1
tl = []

if k < n:
    for i in range(k):
        tmp = nlis[tmp - 1]

    print(tmp)
else:
    for i in range(n):
        if not nlis[tmp - 1] in tl:
            tl.append(nlis[tmp - 1])
            tmp = nlis[tmp - 1]
        else:
            tl = tl[tl.index(nlis[tmp - 1]):]
            print(tl[((k - i) % len(nlis)) - 1])
            break

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

d = defaultdict(list)
root = []
p = 1

s, e = 0, 0

for i in range(10):
    d[p].append(i)
    root.append(A[p - 1])
    if len(d[p]) == 2:
        s = d[p][0]
        e = i
        break
    p = A[p - 1]
#print(s, e)

#print(root)

n = (K - s) % (e - s)
#print(n)
print(root[s + n - 1])

Submission status: Runtime Error

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

c = []
p = 0
na = 0
st = 0
ed = 0
while True:
    c.append(p)
    p = a[p]
    na += 1
    if p in c:
        st = p
        ed = len(c)
        break
if k < len(c):
    print(c[k] + 1)
else:
    k -= st
    k %= ed - st
    print(c[k] + 1)

Submission status: Runtime Error

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n,k = iim()
A = iil()
d = {}
l = [0]
def move(num):
    if d.get(A[num]-1,False):
        return len(l)
    else:
        d[A[num]-1] = True
        l.append(A[num]-1)
        return move(A[num]-1)

rep = move(0)
mod = k%rep
#print(l,mod)
print(l[mod+1]+1)

Submission status: Runtime Error

Solution code:
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
while True:
    na = A[past]
    if na in history: 
        break
    history.append(na)
    past = na
s = history.index(na)
e = len(history) - s
loop = e - s
if K <= s: # ループ前終了
    print(history[K]+1)
else:
    k = K - s
    print(history[s+(k%loop)]+1)

Submission status: Runtime Error

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

visited_city = {1: 0}
visited_city_inv = {0: 1}

now = 1
num = 0
while True:
    next_city = arr[now - 1]
    num += 1
    if next_city not in visited_city:
        visited_city[next_city] = num
        visited_city_inv[num] = next_city
        now = next_city
    else:
        break

if k < num:
    ans = visited_city[k]
else:
    x = visited_city[next_city]
    period = num - x
    ans = visited_city_inv[x + (k - x) % period]

print(ans)
# print(visited_city)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=0
d={}
d[1]=0
dd={}
d[0]=1
now=1
while True:
    now=a[now-1]
    c+=1
    if now in d:
        break
    d[now]=c
    dd[c]=now
s=d[now]
if k<=s:
    print(dd[k])
    exit()
p=c-s
k-=s
k%=p
k+=s
print(dd[k])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ok=set()
now=0
l=[]
while now not in ok and k:
    l.append(now)
    ok.add(now)
    now=a[now]-1
    k-=1
length=len(ok)-(h:=l.index(now))
l=l[h:]
print(l,h,length)
k=(k)%length

print(l[k]+1)

Submission status: Runtime Error

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

dist = A[0]
visited = set()
_v = [1]
stop = 0
while True:
    if dist in visited:
        stop = dist
        break
    visited.add(dist)
    _v.append(dist)
    dist = A[dist-1]

made = 0
for i in range(len(_v)):
    if _v[i] == stop:
        break
    made += 1

loop = len(_v) - made
_v = _v[made:]

if K <= made:
    print(_v[K])
else:
    K -= made
    K %= loop
    print(_v[K])

Submission status: Runtime Error

Solution code:
import sys

readline = sys.stdin.readline
n, k = map(int, readline().split())
A = list(map(int, readline().split()))

l = [1]
while True:
    x = A[l[-1]-1]
    if x in l:
        break
    l.append(x)
print(l[x-1:][k % len(l)])

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()
a = na()
dp = [-1]*n
i = 1
now = 0
loop = -1
start = -1

while True:
    now = a[now]-1
    if i == k:
        print(now+1)
        exit()
    if dp[now] == -1:
        dp[now] = i
    else:
        loop = i - dp[now]
        start = now
        break
    i+=1

print(dp.index(k%loop)+1)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
l = [int(x) for x in input().split()]
a_0 = 1
l_1 = [1]
for i in range(k):
  a_0 = l[a_0-1]
  if a_0 in l_1:
    k_1 = i
    k_2 = l_1.index(a_0)
    break
  else:
    l_1.append(a_0)
s = k_1 - k_2 + 1
k_3 = (k - k_2) % s
print(l_1[k_2+k_3])

Submission status: Runtime Error

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

A=list(map(int,input().split()))
L=[1]
now=1
for _ in range(20):
        now=A[now-1]
        if now  in L:
                index=L.index(now)
                break
        L.append(now)
if k<=index:
        print(L[k+1])
        exit()
R=(k-index)%(len(L)-index)

print(L[index:][R])
print(L)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
buff_readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Doubling():
    def __init__(self, a0, m):
        """
        a0 is an array-like object which contains ai, 0 <= i < N.
        ai is the next value of i.
        """
        N = len(a0)
        self.N = N
        self.nt = [[None] * N for i in range(m.bit_length()+1)]
        for i, a in enumerate(a0):
            self.nt[0][i] = a

        for i in range(1, len(self.nt)):
            for j in range(N):
                if self.nt[i-1][j] is None:
                    self.nt[i][j] = None
                else:
                    self.nt[i][j] = self.nt[i-1][self.nt[i-1][j]]

    def apply(self, i, n):
        """
        Apply n times from i
        """
        j = i
        for k in range(n.bit_length()):
            m = 1 << k
            if m & n:
                j = self.nt[k][j]
            if j is None:
                break
        return j


@mt
def slv(N, K, A):
    A = [a-1 for a in A]
    d = Doubling(A, 10**18+1)
    return d.apply(0, K) + 1


def main():
    N, K = read_int_n()
    A = read_int_n()
    print(slv(N, K, A))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

B = []
j = 0

for i in range (0,K+1):
	if B.count(A[j]) > 0:
		Value = B.index(A[j])
		C = B[B.index(A[j])::]
		break
	else:
		B.append(A[j])
		j = A[j]-1

print(C[(K-Value-1)%len(C)])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
seq=list(map(int,input().split()))
v=[False]*n
path=[]
i=seq.index(1)
while True:
        if v[seq[i]-1]==False:
                v[seq[i]-1]=True
                path.append(seq[i])
                i=seq[i]-1
                
        else:
                break
print(path[(k%len(path))])
print(path)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
n = 1
B = [0]*N
for i in range(K):
    B[n-1] = i+1
    n = A[n-1]
    if B[n-1] != 0:
        t = i+2
        break
c = t-B[n-1]
s = (K+1-B[n-1])%c
for i in range(s):
    n = A[n-1]
print(n)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
n,k = map(int, input().split())
a = list(map(lambda z:int(z)-1, input().split()))
if k <= n:
    now = 0
    for i in range(k):
        now = a[now]
    print(now+1)
    exit()
root = [0]
use = defaultdict(int)
now = 0
use[0] = 1
for i in range(n):
    now = a[now]
    if use[now] == 0:
        use[now] += i
        root.append(now)
    else:
        k -= use[now]
        root = root[use[now]:]
        break

k %= len(root)
print(root[k-1]+1)

Submission status: Runtime Error

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

stack = [0]
def dfs(a, k):
    global stack
    if k == 0:
        return a
    if k == -1:
        return stack[-1]
    next_a = A[a]-1
    if next_a in stack:
        idx = stack.index(next_a)
        n = len(stack)
        k %= (n - idx)
        res = dfs(next_a, k - 1)
    else:
        stack.append(next_a)
        res = dfs(next_a, k-1)
    return res

print(dfs(0, K) + 1)

Submission status: Runtime Error

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

index = 0
keiro = []

for i in range(k):
  index = al[index] - 1
  keiro.append(index)
  if keiro.count(index) == 2:
    start = keiro.index(index)
    mod = i - start
    break

print(keiro[k%mod-1]+1)

Submission status: Runtime Error

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

town = 1
count = 0
town_visited = set()
while count < k:
    town_visited.add(town)
    town = a_l[town-1]
    count += 1
    if town in town_visited:
        break

k -= count
if k = 0:
    print(town)
    exit()
count = 0
town_visited = set()
while count < k:
    town_visited.add(town)
    town = a_l[town-1]
    count += 1
    if town in town_visited:
        break

remain = k%count
count = 0
while count < remain:
    town = a_l[town-1]
    count += 1
   
print(town)

Submission status: Runtime Error

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

same = 0
for i in range(1, n + 1):
	if A[i] == i:
		same += 1

k = k % (n - same)

pos = 1
for i in range(1, k + 1):
	pos = A[pos]

print(pos)

Submission status: Runtime Error

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

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

ansl = []
used = [0 for i in range(n)]
now = 0
while True:
    if used[now] == 1:
        break
    ansl.append(now)
    used[now] = 1
    now = al[now] -1

l = len(ansl)
last = ansl.index(now)
loop = ansl[last:]
if k <= len(ansl):
    print(ansl[k])
else:
    k -= len(ansl)
    print(loop[k%len(loop)]+1)

Submission status: Runtime Error

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

A=list(map(int,input().split()))
L=[1]
now=1
for _ in range(20):
        now=A[now-1]
        if now  in L:
                index=L.index(now)
                break
        L.append(now)
        
R=(k-index)%(len(L)-index)

print(L[index:][R])

Submission status: Runtime Error

Solution code:
N,K = input().split()
A = []
locate = 1
for i in range(N):
  A.append(int(input()))
for i in range(K):
  locate = A[locate-1]
print(locate)

Submission status: Runtime Error

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

town = 1
count = 0
town_visited = set()
while count < k:
    town_visited.add(town)
    town = a_l[town-1]
    count += 1
    if town in town_visited:
        break

k -= count
count = 0
town_visited = set()
while count < k:
    town_visited.add(town)
    town = a_l[town-1]
    count += 1
    if town in town_visited:
        break

remain = k%count
count = 0
while count < remain:
    town = a_l[town-1]
    count += 1
   
print(town)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
now = 1
used = [0] * (n + 191)
used[1] = 1
for _ in range(k):
    now = A[now]
    if used[now] != 0:
        cn = _+1-used[now]+1
        break
    used[now] = _ + 1
for i in range(k % cn):
    now = A[now]
print(now)