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: Time Limit Exceeded

Solution code:


n, k = [0] * 2
a = []

def format_input(filename = None):
	global n, k
	global a
	if filename == None:
		n, k = list(map(int, input().split()))
		a = list(map(int, input().split()))

	elif filename == '__random__':
		from random import randint as rng
		n = rng(2, 2 * 10**5)
		k = rng(1, 10**18)
		a = [rng(1, n) for i in range(n)]
		print(n, k)
		print(' '.join(list(map(str, a))))

def get_answer():
	loop = [1]
	start = -1
	while start == -1:
		node = a[loop[-1]-1]
		if loop.count(node) > 0:
			start = loop.index(node)
		else:
			loop.append(node)

	l = len(loop)
	if k >= l:
		answer = loop[(k - l) % (l - start) + start]
	else:
		answer = loop[k]

	return answer

if __name__ == '__main__':
	format_input()

	ans = get_answer()
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
a = [0] + list(map(int, input().split()))
r = [1]
k += 1
now = 1
for i in range(n):
   now = a[now]
   if now in r:break
   r.append(now)
I = r.index(now)

if len(r) > k: print(r[k-1])
else:
  if I != 0: 
    k -= I
    r = r[I:]
  print(r[-1] if k%len(r)==0 else r[k%len(r)-1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
if __name__ == "__main__":
    N, K = map(int, input().split())
    A = list(map(int, input().rstrip().split(' ')))
    visit = {1:[]}
    a_now = 1
    step = 0
    for i in range(K):
        a_now = A[a_now - 1]
        step += 1
        #print(a_now)
        self_loop = visit.get(a_now)
        if self_loop is None: # まだ訪れたことがない
            for v in visit.values():
                v.append(a_now)
            visit[a_now] = []
        else:
            self_loop.append(a_now)
            break
        #print(visit)
        if step == K:
            #print("K:")
            print(a_now)
            sys.exit()
    ## self_loop が見つかった場合
    #print("a_now:{}".format(a_now))
    #print("step:{}".format(step))
    #print(self_loop)
    n = len(self_loop)
    left = K-step
    left -= (left // n) * n
    #print(step)
    print(self_loop[left - 1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

tel_list = []

next_city = 0

city_have_been_to = set([0])

way = [0]

while k > 0:
    k -= 1
    temp = city_have_been_to.copy()

    next_city = a[next_city] - 1
    city_have_been_to.add(next_city)
    way.append(next_city)

    if temp == city_have_been_to:
        break

if k == 0:
    print(way[-1] + 1)
else:
    temp_index = way.index(next_city)
    print(way[temp_index:-1][k % len(way[temp_index:-1])] + 1)

Submission status: Time Limit Exceeded

Solution code:
O=list(map(int, input().split()))
N=O[0]
K=O[1]

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

c=0
n = 1

for i in range(K):
    c+=1
    n = A[n-1]
    if n == 1:
        break

for i in range(K-c*int(K/c)):
    n = A[n-1]
    
print(n)

Submission status: Time Limit Exceeded

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

    A = [0]
    A = A + list(map(int, input().split(' ')))

    a = 1
    b = A[a]
    interval = 0
    checked_list = [1]
    for i in range(K):
        a = b
        b = A[a]
        interval += 1
        if a not in checked_list:
            checked_list.append(a)
        else:
            # print(checked_list, a)
            # print(checked_list.index(a), len(checked_list))
            break

    interval = len(checked_list) - checked_list.index(a)
    for i in range((K-checked_list.index(a)) % interval):
        b = A[a]
        a = b
    print(a)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

visitcount = dict()
n_point = list()

x = 1
count = 0
while(x not in set(visitcount)):
  visitcount[x] = count
  n_point.append(x)
  x = A[x-1]
  count += 1
  
period = count - visitcount[x]

if (K < visitcount[x]):
  print(n_point[K])
else:
  y = (K-visitcount[x]) % period
  print(n_point[visitcount[x] + y])

Submission status: Time Limit Exceeded

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

current_loc = 1
next_loc = A[0]

for i in range(K):
    current_loc = next_loc
    next_loc = A[current_loc-1]

print(current_loc)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
d = {}
for num,i in enumerate(a):
  d[num+1] =  i
t = []
i = 1
j = 1
while(i not in t):
  t.append(i)
  i = d[i]
t_ = []
b = {}
while(i not in t_):
  t_.append(i)
  i = d[i]
i = 1
while(k>0):
  if i == t_[0]:
    k = k%len(t_)
    ans = t_[k]
    break
  else:
    i = d[i]
    ans = i
    k-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def next(n,k,arr):
	
	visited = [0]
	cur = arr[0]-1
	while True:

		if cur in visited:
			rep = visited.index(cur)
			break
		else:
			visited.append(cur)
			cur = arr[cur] - 1
	
	rep_visit = visited[rep:]
	k += 1
	if k <= len(visited):
		print(visited[k-1]+1)
	else:
		k -= len(visited)
		rem = k%len(rep_visit)
		if rem == 0:
			print(rep_visit[-1]+1)
		else:
			print(rep_visit[rem-1]+1)
	return


    
#m = int(input())

#for t in range(m):
#	n = int(input())
a,b = map(int,input().split())
#s = input()
#t = input()

arr = list(map(int,input().split()))
next(a,b,arr)
'''
qarr = []
for i in range(b):
	qarr.append(int(input()))

#k = input()
#n = int(input())
Sol(a,b,arr,qarr)'''
#for i in range(n):
#	next(int(input()))

Submission status: Time Limit Exceeded

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

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

visited = [1]
now = 1
step = 0
for i in range(k):
    now = a[now-1]
    step += 1
    if now not in visited:
        visited.append(now)
    else:
        ind = visited.index(now)
        loop = visited[ind:]
        tmp = (k-step) % len(loop)
        now = loop[tmp]
        break

print(now)

Submission status: Time Limit Exceeded

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

i = 1
head = []
used = []
roop = []

inte=False
for _ in range(k):
    i = a[i]
    if i in used:
        used.append(i)
        head = used[0: used.index(i)]
        roop = used[used.index(i): -1]
        inte =True
        break
    else:
        used.append(i)

if inte:
    if len(head) >= k:
        print(head[k-1])
    else:
        x = (k - 1 - len(head)) % len(roop)
        print(roop[x])
else:
    print(used[k-1])

Submission status: Time Limit Exceeded

Solution code:
num  = input().split()
town = input().split()

N_1 = town.index(min(town))

NN = int(num[1]) #1
next = 1 

while NN != 0:
    next = int(town[next -1])
    #print(next)
    NN = NN -1

print(next)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    dic = {}
    for i,t in enumerate(town):
        dic[i+1] = t
    set_t = set()
    list_t = []
    current = 1
    is_loop = False
    for i in range(k):
        current = dic[current]
        set_t.add(current)
        list_t.append(current)
        if len(list(set_t)) != len(list_t):
            loop_start = list_t[-1]
            is_loop = True
            break
    if not is_loop:
        print(list_t[-1])
    else:
        loop_list = list_t[list_t.index(loop_start):-1]
        am = (k-i) % len(loop_list)
        print(loop_list[am-1])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split(' '))
A = [None] + list(map(int, input().split(' ')))

v = []
l = 1
i = 0
loop = None

while (i < K):
    v.append(l)
    if A[l] in v:
        loop = v[v.index(A[l]):]
        break
    l = A[l]
    i += 1

if loop is None:
    print(l)
else:
    print(
        loop[(K - i - 1) % len(loop)]
    )

Submission status: Time Limit Exceeded

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

a = 0
n = A[0]

while a < K - 1:
    a = a + 1
    n = A[n-1]
print(n)

Submission status: Time Limit Exceeded

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

for i, a in enumerate(a_list, 1):
    if k < i:
        start = i
        break
    next_a = a_list[loop_dict[i - 1] - 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] = a_list[loop_dict[i-1]-1]

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

Submission status: Time Limit Exceeded

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

# N, K = map(int, "6 727202214173249351".split())
# A = list(map(int,"6 5 2 5 3 2".split(" ")))

has_been = [0]

dest = 0
loop = 0
# print (dest)
breakflag = 0
for i in range (K):
    
    # print (has_been)
    dest = A[dest]-1
    if (dest) not in has_been:
        has_been.append((dest))
    else:
        loop_start = has_been.index(dest)
        loop_end = i
        breakflag = 1
        break
if breakflag:
    print (has_been[(K-loop_start)%(loop_end - loop_start + 1)  + loop_start]+1)
else:
    print (dest + 1)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def main():
    n, k = list(map(int, input().split()))
    A = list(map(int, stdin.readline().split()))
    roots = [1]
    position = 0
    while True:
        position = A[roots[-1]-1]
        if position in roots:
            break
        roots.append(position)

    x = roots.index(position)
    y = len(roots) - x

    if k <= x:
        print(roots[k])
    elif k > x:
        print(roots[x + ((k-x) % y)])





if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

# process
l = [1]
now = A[0]
while not now in l:
    l.append(now)
    now = A[now-1]

ti = l.index(now)

ans = 0
if K < ti:
    ans = l[K]
else:
    k = (K-ti)%(len(l)-ti)
    ans = l[ti+k]

# output
print(ans)

Submission status: Time Limit Exceeded

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

rute = list()
city = A[0]
while city not in rute:
    rute.append(city)
    city = A[city]

loopbegin = rute.index(city)
rute_nonloop = rute[0:loopbegin]
rute_loop = rute[loopbegin:]

if K < len(rute_nonloop):
    print(rute[K-1]+1)
else:
    K -= len(rute_nonloop)
    K %= len(rute_loop)
    print(rute_loop[K-1]+1)

Submission status: Time Limit Exceeded

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

roop = [0] 
flag = 1
i = 1
for i in range(len(A)):
    if A[roop[i]] - 1 in roop:
        start = roop.index(A[roop[i]] - 1)
        end = i
        break
    else:
        roop.append(A[roop[i]] - 1)

if K <= end:
    print(roop[K] + 1)
else:
    print(roop[(K - start) % (end - start + 1)  + start] + 1)

Submission status: Time Limit Exceeded

Solution code:
def main():
  N, K = map(int, input().split())
  route = list(map(int, input().split()))
  n_place = 1
  trails = [1]
  flg = 0
  for i in range(K):
    n_place = route[n_place - 1]
    if n_place in trails:
      start = trails.index(n_place)
      kyori = i - trails.index(n_place) + 1
      loop = trails[start : start + kyori]
      flg = 1
      break
    else:
      trails.append(n_place)
    if i == K - 1:
      print(n_place)
  if flg == 1:
    print(loop[(K - start) % kyori])
    
main()

Submission status: Time Limit Exceeded

Solution code:
date=input().rstrip().split(" ")
N=int(date[0])
K=int(date[1])
datex=input().rstrip().split(" ")
a=1
b=0
list0=[1]
if K>=100000:
    for i in range(K):
       a=int(datex[a-1])
       list0.append(a)
       if list0.count(list0[len(list0)-1])==2:
           b=1
           break
else:
    for i in range(K):
       a=int(datex[a-1])
if b==1:
    list1=[]
    for i in range(K):
        a=int(datex[a-1])
        list1.append(a)
        if list1.count(list1[len(list1)-1])==2:
            del list1[len(list1)-1]
            break
    R=len(list1)
    ONE=len(list0)-R-1
    K=K-ONE
    K=K%R
    a=list1[K-1]
print(a)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
i = 0
j = 0
his = []
length = 0

#ループに入ったら偶奇見て処理

if K >= N:
  for j in range(N):
    i = A[i]-1
    his.append(i)
    K -= 1
  
#loop or not
  for j in range(0, N):
    for k in range(j+1, N):
      if his[j] == his[k]:
        length = k-j
        K = K % length
      
while K > 0:
  i = A[i]-1
  K -= 1
    
print(i+1)

Submission status: Time Limit Exceeded

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

portals = [0] + list(map(int, input().split()))

before = 1

for _ in range(K):
    target_index = portals[before]
    before = target_index

print(before)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
v = []
now = 0
for i in range(K):
  p = A[now]
  now = p-1
  if p in v:
    j = v.index(p)
    df = i-j
    Kn = (K-i-1) % df
    for k in range(Kn):
      p = A[now]
      now = p-1
    break
  else:
    v.append(p)
print(p)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, K = map(int,input().split())
A = list(map(int,input().split()))
ind = list([i+1 for i in range(N)])
tmp = np.array([[0] * N for i in range(N)])
tmp2 = np.array([0]*N)
tmp2[0] =1
for i in range(N):
	tmp[i][A[i]-1]=1
tmp = np.matrix(tmp)
tmp3 = np.dot(tmp2,tmp**K)
result = np.matmul(tmp3,ind)          
print(int(result[0][0]))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [[i - 1, j - 1] for i, j in enumerate(a)]
g = [[0 for _ in range(n)] for _ in range(n)]
x = [0]

i = 0
j = a[0][1]
while g[i][j] != 1:
    x.append(j)
    g[i][j] = 1
    i = j
    j = a[j][1]

tmp = x.index(x[-1])
if tmp >= k:
    print(x[k] + 1)
else:
    x = x[tmp:-1]
    k = (k - tmp) % len(x)
    print(x[k] + 1)

Submission status: Time Limit Exceeded

Solution code:
def ans(n, k, list_a):
    i=0
    list_memo=[1]
    while i<k:
        next = list_a[list_memo[i]-1]
        if next not in list_memo:
            list_memo.append(next)
        else:
            num=list_memo.index(next)
            len_list=len(list_memo)
            i+=1
            x = len_list-num
            k=k-num
            
            #print(k)
            k%=x
            return list_memo[num+k]
            #return 50000
            
        i+=1
    return list_memo[i]
        




n, k = (int(x) for x in input().split())
list_a = list(map(int, input().split()))
print(ans(n, k, list_a))

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
Al = [0]
Al.extend(list(map(int, input().split())))

l = [1]
l_append = l.append
i = 1
for _ in range(len(Al)-1):
    if Al[i] not in l:
        l_append(Al[i])
        i = Al[i]
    else:
        i = Al[i]
        break

if K<=(l.index(i)+1):
    print(l[K])
else:
    l_new = l[l.index(i):]
    print(l_new[(K-l.index(i))%len(l_new)])

Submission status: Time Limit Exceeded

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

now = A[0]

for i in range(K-1):
    if now == A[now-1]:
      break
    now = A[now-1]
    
print(now)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split(' '))
arr= list(map(int,input().split(' ')))
dest=[]
start=0
count=0
loop= 0
while(start not in dest and count<k):
    dest.append(start)
    start= arr[start]-1
    count+=1

if count==k:
    print(start+1)
else:
    dev= dest.index(start)
    loop= len(dest)-dev

    idx= (k-dev)%loop
    print(dest[dev+idx]+1)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A=list(map(int,input().split()))
cur_pos = 1
while K > 0:
  K -= 1
  cur_pos = A[cur_pos -  1]
print(cur_pos)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, k = tuple([int(e) for e in input().split()])
    arr = [0] + [int(e) for e in input().split()]

    seen = set()
    actual = 1
    seen.add(1)
    while k > 0:
        actual = arr[actual]
        k -= 1
        if actual in seen:
            break
        seen.add(actual)

    c = 1
    ac = arr[actual]
    while actual != ac:
        ac = arr[ac]
        c += 1

    k = k % c
    while k > 0:
        actual = arr[actual]
        k -= 1
    print(actual)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

X = 0
for i in range(K):
  X = A[X]-1

print(X + 1)

Submission status: Time Limit Exceeded

Solution code:
date=input().rstrip().split(" ")
N=int(date[0])
K=int(date[1])
datex=input().rstrip().split(" ")
a=1
b=0
list0=[1]
if K>=10000000:
    for i in range(K):
       a=int(datex[a-1])
       list0.append(a)
       if list0.count(list0[len(list0)-1])==2:
           b=1
           break
else:
    for i in range(K):
       a=int(datex[a-1])
if b==1:
    list1=[]
    for i in range(K):
        a=int(datex[a-1])
        list1.append(a)
        if list1.count(list1[len(list1)-1])==2:
            del list1[len(list1)-1]
            break
    R=len(list1)
    ONE=len(list0)-R-1
    K=K-ONE
    K=K%R
    a=list1[K-1]
print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = lambda: sys.stdin.readline()


def cin_int_list():
    return [int(x) for x in input().split()]


def cin_int_iter():
    return (int(x) for x in input().split())


def cin_int():
    return int(input())


def cout_int_iter(a):
    print(' '.join(map(str, a)))


def iota(n, start=0):
    return list(range(start, n))


def cin_digits_list():
    return [ord(x) - ord('0') for x in input()]


def main():
    n, k = cin_int_iter()

    g = {}

    cycle_starts = None
    for u, x in enumerate(cin_int_list()):
        v = x - 1
        g[u] = v
        if v in g:
            cycle_starts = v

    if cycle_starts is not None:
        cycle_len = 0

        u = cycle_starts
        cycle = []
        while True:
            cycle.append(u)
            u = g[u]

            if u == cycle_starts:
                break

        before_cycle = []
        u = 0

        while u != cycle_starts:
            before_cycle.append(u)
            u = g[u]

        if k < len(before_cycle):
            print(before_cycle[k] + 1)
            return

        if before_cycle:
            k -= len(before_cycle)

        print(cycle[k % len(cycle)] + 1)
        return

    u = 0
    while k:
        u = g[u]
        k -= 1

    print(u + 1)




main()


"""
6 1
6 5 2 5 3 2

6 2
6 5 2 5 3 2

6 3
6 5 2 5 3 2

6 4
6 5 2 5 3 2

6 6
6 5 2 5 3 2

6 5
6 5 2 5 3 2


no cycle

4 1
2 3 4 5

4 111111111111111111
2 3 4 1
"""

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
#x = int(input())
n, k = map(int, sys.stdin.readline().split())
#s = input()
a = list(map(int, sys.stdin.readline().split()))

l = [1]
i = 0
while (True):
    if a[l[i]-1] in l:
        p = l.index(a[l[i]-1])
        break

    l.append(a[l[i]-1])
    i += 1

if k < p:
    print(l[k])
else:
    k -= p
    x = k % (len(l)-p)
    print(l[x+p])

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=[list(map(int,input().split()))]
def tele(n):
  a=1
  for i in range(1,n):
      a=A[0][a-1]
  return a
print(tele(K+1))

Submission status: Time Limit Exceeded

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

l = []
place = 0
while True:
    if K==0:
        print(place+1)
        exit()
    l.append(place)
    place = A[place]
    K -= 1
    if place in l:
        ind = l.index(place)
        count = len(l) - ind
        print(l[ind + K%count]+1)
        exit()

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A_list = list(map(int,input().split()))
location = 1

for k in range(K):
    location = A_list[location-1]
    
print(location)

Submission status: Time Limit Exceeded

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

for i, a in enumerate(a_list, 1):

    next_a = a_list[loop_dict[i - 1] - 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] = a_list[loop_dict[i-1]-1]

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

Submission status: Time Limit Exceeded

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

K = NK[1]

town = 1
for i in range(K):
  town = A[town-1]
  
print(str(town))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

n = a[0]
k = a[1]

src = 0

for i in range(k):
  dest = b[src]
  src = dest - 1
  
print(dest)

Submission status: Time Limit Exceeded

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

town = 0
visited = [town]
loop = None
while K > 0:
    K -= 1
    town = A[town]
    if town in visited:
        loop = visited[visited.index(town):]
        break
    visited.append(town)

if loop:
    K %= len(loop)
    print(loop[K]+1)
else:
    print(town+1)

Submission status: Time Limit Exceeded

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

S=A[0]

for i in range(int(K)-1):
  
  S=A[int(S)-1]
  
print(S)

Submission status: Time Limit Exceeded

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

if k <= n:
    takahashi = 0
    for _ in range(k):
        takahashi = a[takahashi] - 1
    print(takahashi + 1)
else:
    start = 0
    takahashi = start
    trajectory = [takahashi]
    loop_start = -1
    loop_end = -1
    for i in range(n):
        takahashi = a[takahashi] - 1
        if takahashi in trajectory:
            loop_start = trajectory.index(takahashi)
            break
        trajectory.append(takahashi)
    print(trajectory[loop_start:][(k - loop_start) % (i + 1 - loop_start)] + 1)

Submission status: Time Limit Exceeded

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

count = 0
location = 0
footprints = []
while(True):
    k -= 1
    footprints += [location]
    location = a[location]
    
    if (k == 0):
        print(location+1)
        break
    
    if (location in footprints):
        x = footprints.index(location)
        loop = footprints[x:]
        n = len(loop)
        print(loop[k%n]+1)
        break

Submission status: Time Limit Exceeded

Solution code:
N,K= map(int,input().split())
A=list(map(int,input().split()))
count=0
now=1
while  count < K:
    now=A[now-1]
    count+=1
print(now)

Submission status: Time Limit Exceeded

Solution code:
nums = [int(s) for s in input().split()]
telep = [int(s) for s in input().split()]
route = [0]
for j in range(nums[1]):
  n_t = telep[route[-1]] - 1
  if n_t in route:
    d = len(route[route.index(n_t):])
    z = (nums[1] - j - 1) % d
    n_t = route[route.index(n_t):][z]
    break
  else:
    route.append(n_t) 
print(n_t + 1)

Submission status: Time Limit Exceeded

Solution code:
n, k = [int(x) for x in input().strip().split()]
a = [int(x)-1 for x in input().strip().split()]
assert len(a) == n
current = 0
way = [0]
for i in range(k):
  current = a[current]
  if current in way:
    li = way.index(current)
    loop = way[li:]
    print(loop[(k - li) % len(loop)]+1)
    break
  else:
    way.append(current)
else:
  print(current+1)

Submission status: Time Limit Exceeded

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

l = []
c = 1
for _ in range(n):
    c = a[c - 1]
    if len(l) + 1 == k:
        print(c)
        break
    elif c in l:
        i = l.index(c)
        l2 = l[i:]
        print(l2[(k - i) % len(l2) - 1])
        break
    else:
        l.append(c)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N,K = map(int, stdin.readline().rstrip().split())
A = list(map(int, stdin.readline().rstrip().split()))
current = 0
for i in range(K):
    current = A[current] - 1

print(current + 1)

Submission status: Time Limit Exceeded

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

# 戻ってくるまでのリスト
R=[]
Now=1
cnt=0
for i in range(N):
    if Now in R:
        Start=R.index(Now)
        End = cnt
        break
    else:
        R.append(Now)
        Now=A[Now-1]
        cnt+=1

if K < End:
    ans=R[K]
else:
    key=Start+(K-Start)%(End-Start)
    ans=R[key]

print(ans)
#print(Start,End)
#print(R)
#Start+K(End-Start)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
loop_=[1]
lst=list(map(int,input().split()))

Loop=True
while Loop:
    num=lst[loop_[-1]-1]
    if num in loop_:
        Loop=False
        tar=num
    else:
        loop_.append(num)
count=0
for i in loop_:
    if i==tar:
        break
    else:
        count+=1    

loop__=loop_[count:]

if k<=len(loop_):
    print(loop_[k])
else:
    num=k+1-len(loop_)
    print(loop__[num%len(loop__)-1])

Submission status: Time Limit Exceeded

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

MyList = [int(n) for n in MyList]

ANS = 0

def Myfunc(A):
    return MyList[A] - 1

for i in range(K):
    ANS = Myfunc(ANS)

print(ANS+1)

Submission status: Time Limit Exceeded

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

s=1
r=[1]
for i in range(k):
  s=l[s-1]
  if s not in r:
    r.append(s)
    if i==k-1:
      print(s)
  else:
    x=r.index(s)
    mod=len(r)-x
    t=(k-i-1)%mod
    print(r[x+t])
    break

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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

C = 1
Clist = []
while True:
    Clist.append(C)
    C = A[C-1]
    if C in Clist:
        break

looplist = Clist[Clist.index(C):]
#print(C,Clist)
#print(len(Clist[:Clist.index(C)]),len(Clist[Clist.index(C):]))
if K <= len(Clist[:Clist.index(C)]):
    print(Clist[K])
    quit()
modindex=(K-len(Clist[:Clist.index(C)]))%len(Clist[Clist.index(C):])

print(looplist[modindex])

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
dst = list(map(int, input().split()))
log = [1]
current = 1
for i in range(1, K+1):
    current = dst[current-1]
    if current in log:
        loop_start_ind = log.index(current)
        loop_len = i-loop_start_ind
        current = log[loop_start_ind+(K-i) % loop_len]
        break
    else:
        log.append(current)
print(current)

Submission status: Time Limit Exceeded

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

num = K
memo = []
town = 1
for _ in range(K):
    memo.append(town)
    if not A[town-1] in memo:
        town = A[town-1]
        num -= 1
    else:
        index = memo.index(A[town-1])
        l = len(memo) - index
        if num % l == 0:
            town = memo[-1]
        else:
            town = memo[index + (num % l) - 1]
        break
print(town)

Submission status: Time Limit Exceeded

Solution code:
n,k=[int(x) for x in input().split()]
p=[(int(x)-1) for x in input().split()]
a=[0]
i=0
while p[i] not in a:
  a.append(p[i])
  i=p[i]
l=a.index(p[i])
if k<=l: print(1+a[k])  
else: print(1+a[l+(k-l)%(len(a)-l)])

Submission status: Time Limit Exceeded

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


def main() -> None:
    N, K = rmi()
    A = rmi()
    road = []
    current = 1
    # route map make
    for n in range(N):
        if not current in road:
            road.append(current)
            current = A[current - 1]
        else:
            road.append(current)
            break
    # route map check
    dup = [x for x in set(road) if road.count(x) > 1][0]
    if dup == 1:
        loop = road[:-1]
        rem = K % len(loop)
        w(loop[rem])
    else:
        indexes = [i for i, x in enumerate(road) if x == dup]
        loop = road[indexes[0]: indexes[1]]
        if K < indexes[0]:
            w(road[K])
        else:
            K -= indexes[0]
            rem = K % len(loop)
            w(loop[rem])


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10 ** 9)
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
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)