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=map(int,input().split())
a=[0]+list(map(int,input().split()))
pointer=1
for i in range(k-1):
    pointer=a[pointer]
print(a[pointer])

Submission status: Time Limit Exceeded

Solution code:

n, k = map(int, raw_input().split(' '))
adj = [-1] + map(int, raw_input().split(' '))
seen = [-1] * (n+1)
h = [-1] * (n+1)
cur = 1
count = 0

while(k):
	
	if seen[cur] == -1:
		seen[cur] = count
		h[count] = cur
		count += 1
	nex = adj[cur]
	if seen[nex] == -1:
		k -= 1
		cur = nex
	else:
		if seen[nex] > seen[cur]:
			c = seen[cur] + k
			cur = h[c] 
			k = 0
			#cur = nex

		elif seen[nex] < seen[cur]:
			k %= seen[cur] - seen[nex] + 1
			if k:
				k-=1
				cur = nex
			

print cur

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
checked = [0]
next_num = a[0]-1
i = 0
while True:
    if i == 0:
        i += 1
    elif next_num in checked:
        loop = checked[checked.index(next_num):]
        break
    checked.append(next_num)
    next_num = a[next_num]-1
dif = len(checked) - len(loop)
if k <= dif:
    print(checked[k]+1)
else:
    k = k-dif
    print(loop[k%len(loop)]+1)

Submission status: Time Limit Exceeded

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

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

print(pos+1)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split( ))
A = list(map(int,input().split( )))
start = 0
count = [0]
town = [[] for i in range(N)]
town[0] = [0]
for i in range(K):
    start = A[start] - 1
    town[start].append(i+1)
    if start in count:
        count.append(start)
        town0 = town[start][0]
        town1 = town[start][1]
        ans = count[town0:town1][(K - town0)%(town1 - town0)]+1
        break
    else:
        count.append(start)
        ans = start + 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
spot=[1]
if k==1:
  print(a[0])
else:
  place=0
  for i in range(k):
    t=a[place]
    if t in spot:
      print(spot[spot.index(t)+(k-i-1)%(len(spot)-spot.index(t))])
      break
    else:
      place=t-1
      spot.append(t)
  else:
    print(spot[-1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

i = 0

l = list(range(1,n+1)) + l

l = np.array(l).reshape(2, n)

tmp = l[1,0]
while i < (k-1) :
    tmp = l[1,tmp-1]
    i += 1
    
print(tmp)

Submission status: Time Limit Exceeded

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

j=s.index(s[i])
x=i-j
y=(k-j)%x
if k>=j:
  print(s[j+y])
else:
  print(s[k])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [i - 1 for i in a]
    ac = Counter(a)
    kaiten = [0] * n

    for i1 in range(n):
        if ac[i1] >= 1:
            kikan = n
            current = i1
            count = 0
            while kikan > 0:
                current = a[current]
                count += 1
                if current == i1:
                    kaiten[i1] = count
                    break
                kikan -= 1
    #print(kaiten)
    koko = 0
    go = 0
    while True:
        go += 1
        koko = a[koko]
        if kaiten[koko] > 0:
            break

    remain_go = (k - go) % kaiten[koko]
    while remain_go > 0:
        koko = a[koko]
        remain_go -= 1

    print(koko + 1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
NK = [int(x) for x in input().split()]
N=NK[0]
K=NK[1]
A = [int(x) for x in input().split()]
    
j=1 
C=[]
for i in range(N):#i:1~N
  j=A[j-1]
  C.append(j)
ii=0
for i in range(0,len(C)):
  for j in range(i+1,len(C),1):
    if C[i]==C[j]:
      T=j-i
      ii=i+1
      break
  if ii!=0:
    break
if K<ii:
  print(C[K-1])
else:
  print(C[(K-ii)%T+ii-1])

"""
6 727202214173249351
6 5 2 5 3 2
2
"""

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input('').split(' '))
a=input('')
a=a.split(' ')
o=[]
for p in range(n):
    a[p]=int(a[p])
    o.append(0)

p=1
h=[]
h.append(1)
j=k
while j>0:
    p=a[p-1]
    o[p-1]+=1
    if o[p-1]>=2:
        j=j-1
        c=len(h)-h.index(p)
        j=j%c

    else:
        h.append(p)
        j=j-1

print(p)

Submission status: Time Limit Exceeded

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

now = 1
nowlist = [1]

if N < K:
    for i in range(K):
        if  nowlist.count(A[now-1]) == 0:
            now = A[now-1]
            nowlist.append(now)

        else:
            p = len(nowlist) - nowlist.index(A[now-1])
            break

    mae = nowlist.index(A[now-1])
    Q = (K-mae) % p
    print(nowlist[Q + mae])

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

Submission status: Time Limit Exceeded

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

loop_checker = [1]
before_loop_len = 0
loop_len = 0

now = 0
while True:
    if A[now] in loop_checker:
        index_val = loop_checker.index(A[now])
        loop_slice = loop_checker[index_val:]
        before_loop_len = loop_checker.index(A[now])
        loop_len = len(loop_slice)
        break
    else:
        loop_checker.append(A[now])
        now = A[now] - 1
if K <= before_loop_len:
    print(loop_checker[K])
else:
    checkK_prep = (K - before_loop_len)
    checkK = checkK_prep % loop_len
    print(loop_slice[checkK])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

N,K=(int(x) for x in input().rstrip('\n').split())
As = [int(x)-1 for x in input().rstrip('\n').split()]
now=0
pas = [0]
for i in range(N):
  now = As[now]
  pas.append(now)
  if now in pas[:-1]:
    break
arrive = pas.index(now)
loop = len(pas)-arrive-1
if K<=arrive:
  print(pas[K]+1)
else:
  les = K-arrive
  les = les%loop
  res = arrive+les
  print(pas[res]+1)

Submission status: Time Limit Exceeded

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

St = b[0]
for i in range(K-1):
    St = b[St-1]
print(St)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    visit = [1]
    s = 0
    for i in range(1,n):
        if a[s] in visit:
            l = len(visit)
            ind = visit.index(a[s])
            loop = visit[ind:]
            break
        visit.append(a[s])
        s = a[s]-1
    if l>k:
        print(visit[k])
    else:
        k = k-l
        print(loop[k%len(loop)])

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

A_list = list(map(int, input().split()))
A_list = [A - 1 for A in A_list]

path = [0]
town = 0
k = K
j = 0
while k > 0:
  town = A_list[town]
                     
  if town in path:
    #print("town is {}".format(town))
    #print("path is {}".format(path))
    first_visit = path.index(town)
    loop_size = j - first_visit + 1
    #print("loop_size is {}".format(loop_size))
    #print("k is {}".format(k))
    k = (k-1) % loop_size 
    #print("k is now {}".format(k))
    path = []
    continue
    
  path.append(town)
    
  k = k - 1
  j = j + 1

print(town+1)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
idx = A[0]
move = []
move.append(idx)
cycle = []
for _ in range(K-1):
  idx = A[idx-1]
  if idx in move:
    cycle = move[move.index(idx):]
    break
  move.append(idx)

if cycle:
  print(cycle[(K - 1- (len(move)-len(cycle)))%len(cycle)])
else:
  print(move[K-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

def input():
    return sys.stdin.readline()[:-1]

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


def func(A, town):
    return A[town]

att = 1

visited = [1]
loopend = 0
loopstart = 0
if K > N:
    for i in range(K):
        att = func(A, att-1)
        if att in visited:
            loopstart = visited.index(att)
            break
        else:
            visited.append(att)
    loop = visited[loopstart:] # 周回する街
    rem = (K - loopstart) % len(loop)
    att = loop[rem]
else:
    for _ in range(K):
        att = func(A, att-1)

print(att)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8
from collections import defaultdict

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

for f, t in enumerate(A):
    to_next[f+1] = t

route = [1]
current_node = 1
loop_index = [-1, -1]

for idx, _ in enumerate(range(K)):
    route.append(to_next[current_node])
    current_node = route[-1]
    if len(route) != len(set(route)):
        loop_index[0] = route.index(current_node)
        loop_index[1] = idx
        break
    if idx+1 == K:
        print(route[-1])
        exit(0)

# print(to_next)
# print(loop_index)
# print(route)

if loop_index[0] != 0:
    K = K - loop_index[0]
ans_idx = K % (loop_index[1] - loop_index[0] + 1)
# print(ans_idx)
print(route[loop_index[0]:loop_index[1]+1][ans_idx])

Submission status: Time Limit Exceeded

Solution code:

n, k = map(int, raw_input().split(' '))
adj = [-1] + map(int, raw_input().split(' '))

seen = {}
h = {}
cur = 1
count = 0

while(k):
	
	if cur not in seen:
		seen[cur] = count
		h[count] = cur
		count += 1
	nex = adj[cur]
	if nex not in seen:
		k -= 1
		cur = nex
	elif nex in seen:
		if seen[nex] > seen[cur]:
			c = seen[cur] + k
			cur = h[c] 
			k = 0
			#cur = nex

		elif seen[nex] < seen[cur]:
			k %= seen[cur] - seen[nex] + 1
			if k:
				k-=1
				cur = nex
			

print cur

Submission status: Time Limit Exceeded

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


if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline

N,K=map(int,input().split())
T=list(map(int,input().split()))
M=0
A=[1]
for x in range(N+1):
    if T[M] in A or T[M]==M+1:
        key=T[M]
        break

    else:
        t=T[M]
        A.append(t)
        M=t-1
B=len(A)
C=A.index(key)
D=B-C
if K<=B-1:
    F=A[K]
    print(F)
else:
    E=(K+1-C)%D
    if E==0:
        E=D
    F=A[C+E-1]
    print(F)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
As = list(map(int,input().split()))
route = [0]
pre = 0
i = 0
for k in range(K):
    i = As[i]-1
    if i in route:
        pre = route.index(i)
        route = route[pre:]
        break
    route.append(i)
#print(route)
mod = len(route)
print(route[(K-pre)%mod]+1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [0]
current = 0
flag = 0
init = 0
for i in range(N):
  if i >= K:
    ans = current + 1
    flag = 1
    break
  if A[current]-1 in B:
    flag = 2
    break
  B.append(A[current]-1)
  current = A[current]-1
  init += 1
    
if flag == 1:
  print(ans)
elif flag == 2:
  first = B.index(A[current]-1)
  rep = B[first:]
  r = (K-init-1)%(len(rep))
  print(rep[r]+1)
else:
  print(current+1)

Submission status: Time Limit Exceeded

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

if K >= N:
  for j in range(N):
    i = A[i]-1
    his.append(i)
    K -= 1

    for k in range(0, j):
      if his[j] == his[k]:
        length = j-k
        K = K % length
        break
      
while K > 0:
  i = A[i]-1
  K -= 1
    
print(i+1)

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

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

    loop_dict[i] = next_a

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

Submission status: Time Limit Exceeded

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

s = 0

for i in range(K):
	s = l[s] - 1

print(s+1)

Submission status: Time Limit Exceeded

Solution code:
cin = open(0).read().strip().split('\n')

n,k = map(int, cin[0].split(' '))
a = list(map(int, cin[1].split(' ')))

place = 1
history = []
rule = []
while len(rule) == 0:
    history.append(place)
    place = a[place-1]
    for i in range(1,len(history)//2+1):
        aset = set(history[len(history)-i*2:len(history)-i])
        bset = set(history[len(history)-i:])
        # print(i, history, aset, bset)
        if aset == bset:
            rule = history[len(history)-i:]
    if len(history) == (k+1):
        break

if len(rule) == 0:
    print(history[k])
else:
    pre = len(history)-len(rule)*2
    mod = (k-(pre-1)) % len(rule)
    print(rule[mod-1])

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
K -= 1
A = list(map(lambda x: int(x) - 1, input().split()))
seen = []
now = 0
for i in range(N):
    seen.append(now)
    to = A[now]
    now = to
    if now in seen:
        seen.append(now)
        break
loop_s = seen.index(now)
loop_end = loop_s + seen[loop_s + 1:].index(now) + 1
size = loop_end - loop_s
if K <= loop_s:
    print(seen[K+1] + 1)
else:
    K -= loop_s
    seen = seen[loop_s + 1:]
    print(seen[K%size] + 1)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(lambda x: int(x)-1, input().split()))
n = a[0]
his = [-1, n]
while True:
    n = a[n]
    if n in his:
        his.append(n)
        break
    his.append(n)
idx = his.index(his[-1])
loop = len(his[his.index(his[-1]):-1])
if len(his) > k:
    print(his[k]+1)
else:
    t = (k - idx) % loop
    g = his[idx:-1]
    print(g[t]+1)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict,Counter

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

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

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int, input().split())
import numpy as np
a_ls=np.array(list(map(int, input().split())))


t=1
track=[]
while not(t in set(track)):
	track+=[t]
	t=a_ls[t-1]

#print(track)
#print(t)
#print(track.index(t)) #dokokara loop ga hajimaruka

if track.index(t)>K:
	print(track[K])
else:
	s=(K-track.index(t))%(len(track)-track.index(t))
	print(track[s+track.index(t)])

Submission status: Time Limit Exceeded

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

x = 1
flg = 0
list1 = list()
list1.append(1)
for i in range(K):
    x = A[x-1]
    if not x in list1:
        list1.append(x)
    else:
        idx = list1.index(x)
        j = (K - idx) % (i + 1 - idx)
        print(list1[idx + j])
        flg = 1
        break
if flg == 0:
    print(x)

Submission status: Time Limit Exceeded

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

a=input().split()

l=len(a)

for i in range(0,l):
    a[i]=int(a[i])

p=1
h=[]
go=0
i=0
while(go==0):
    i+=1
    
    

    h.append(p)
    p=a[p-1]
    if i==k:
        break
    if p in h:
        j=h.index(p)
        amari=(k-i-1)%(i-j)
        p=h[j+amari]
        go=1
        break

if go==1:
    p=a[p-1]

print(p)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
start_num = 0
cycle_list = [1]
i_now = 1
count = 0
while count < K:
  i_next = A[i_now-1]
  if i_next in cycle_list:
    break
  else:
    cycle_list.append(i_next)
    i_now = i_next
  count += 1

start_num = cycle_list.index(i_next)
cycle_list = cycle_list[cycle_list.index(i_next):len(cycle_list)]    
#print(start_num,cycle_list,K-start_num,len(cycle_list))
if K<=start_num:
  print(i_now)
else:
  print(cycle_list[(K-start_num)%len(cycle_list)])

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = [-1] + [int(i) for i in input().split()]
i = 1
tele = 1
telelist = [1]
while not A[i] in telelist:
    telelist.append(A[i])
    i = A[i]

k = 0
for s in telelist:
    if s == A[i]:
        break
    k += 1
if K<=k:
    print(telelist[K])
else:
    l = len(telelist)-k
    K -= k
    s = K%l
    print(telelist[k+s])

Submission status: Time Limit Exceeded

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

L = 2
P = 0

P = A[0]

while L <= K:
   P = A[P-1]
   L = L + 1

print(P)

Submission status: Time Limit Exceeded

Solution code:
import sys
inp = input().split(" ")
n = int(inp[0])
k = int(inp[1])

pos=0
go=input().split(" ")
for i in range(len(go)):
  go[i]=int(go[i])-1
list=[0]
for i in range(k):
  pos=go[pos]
  if pos in list:
    list.append(pos)
    roopind=list.index(pos)
    rps=list[(roopind+1):]
    rest=((k-1-i)%len(rps))-1
    print(rps[rest]+1)
    sys.exit()
  list.append(pos)
print(list[-1]+1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,K = map(int,input().split())
A = list(map(int,input().split()))
already_town =[1]
now_town=1
count = 0
check=False
for i in range(K):
    already_town.append(A[now_town-1])
    now_town = A[now_town-1]
    count =i+1
    if(len(set(already_town))!=len(already_town)):
        check=True
        break
if(check==False):
    print(now_town)
else:
    beark_len=len(already_town)-1
    cicle_start=already_town.index(already_town[-1])
    roop_town=already_town[cicle_start:-1]
    print(roop_town[(K-cicle_start)%(count-cicle_start)])

Submission status: Time Limit Exceeded

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

a = [0]
a.extend(list(map(int, input().split())))
a1 = []
for i in range(0, len(a)):
    a1.append(0)
step = 1
t = 0
if k<=12:
    for i in range(0, k):
        step = a[step]
    print(step)
else:

    for i in range(0, 10):
        step = a[step]

        step1 = step
    while(True):
        t += 1
        step1 = a[step1]
        if step1 == step:
            break
    result = step
    k = (k-10) % t
    for i in range(0, k):
        result = a[result]

    print(result)

Submission status: Time Limit Exceeded

Solution code:
def main():
  from sys import stdin
  N, K = map(int, input().split())
  route = list(map(int, stdin.readline().split()))
  n_place = 1
  trails = [1]
  flg = 0
  if N < K:
    for i in range(N):
      n_place = route[n_place - 1]
      if n_place in trails:
        j = trails.index(n_place)
        start = j
        kyori = i - j + 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])
  else:
    for i in range(K):
      n_place = route[n_place - 1]
      if n_place in trails:
        j = trails.index(n_place)
        start = j
        kyori = i - j + 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:
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
g=a[0]

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

Submission status: Time Limit Exceeded

Solution code:
N,K=(int(x) for x in input().split())
T=list((int(x) for x in input().split()))
P=T[0]
count=1
while 1 :
    count+=1
    P=T[P-1]
    if(count==K):
        break
print(P)

Submission status: Time Limit Exceeded

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

init = [1]
len_loop = -1

while len(init) <= k:
    nxt = a[init[-1]-1]
    if nxt in init:
        idx_nxt = init.index(nxt)
        len_init = idx_nxt
        len_loop = len(init) - idx_nxt
        break
    else:
        init.append(nxt)

if len_loop == -1:
    print(init[k])
else:
    idx = (k-len_init)%len_loop
    print(init[len_init+idx])

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
rep = []
rep.append(1)
i = 0
cnt = 0
if n>k:
    end =k
else:
    end =n
nof = 0
while True:
    cnt+=1
    if cnt>end:
        nof=1
        break
    go = a[i]-1
    if a[i] not in rep:
        rep.append(a[i])
        i = go
    else:
        rep.append(a[i])
        break
if nof==1:
    print(rep[k])
else:
    lr = len(rep)
    for i in range(lr):
        if rep[i]==rep[lr-1]:
            start=i
            rep2 = rep[i:lr-1]
            break
    mod = len(rep2)
    modL = [i%mod for i in range(start,start+mod)]
    resM = k%mod
    for i in range(mod):
        if resM == modL[i]:
            print(rep2[i])

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
i=1
S=[1]
count=0
while(1):
	S.append(L[i-1])
	i=S[-1]
	count+=1
	if len(set(S))!=len(S):
		break
	if count==K:
		print(S[-1])
		exit()
for i in range(len(S)):
	if S[-1]==S[i]:
		K=K-i
		K=K%(len(S)-i-1)
		print(S[K+i])
		exit()

Submission status: Time Limit Exceeded

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

loop = []
trace = []
ai = 1
for i in range(N+1):
    if ai in trace:
        loop = trace[trace.index(ai):]
        trace = trace[:trace.index(ai)]
        break
    trace.append(ai)
    ai = A_list[ai-1]

if len(trace) > K:
    print(trace[K])
else:
    ind = K - len(trace)
    ind = ind % len(loop)
    print(loop[ind])

Submission status: Time Limit Exceeded

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    g = [1]
    for i in range(len(A)-1):
        prev_p = g[-1]
        g.append(A[prev_p-1])
        
    if K <= N - 1:
        print(g[K])
    else:
        prev_ = g[-1]
        new_g = []
        while True:
            if new_g[:len(new_g)//2] == new_g[len(new_g)//2:] and len(new_g) > 0:
                break
            new_g.append(A[prev_-1])
            prev_ = new_g[-1]
        new_g = new_g[:len(new_g)//2]
        
        K_ = (K - N) % len(new_g)
        print(new_g[K_])
        


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
i2p = {0:1}
p2i = {1:0}
cur = A[0]
i = 1
while True:
    i2p[i] = cur
    p2i[cur] = i
    if A[cur-1] in i2p.values():
        break
    cur = A[cur-1]
    i+=1
i = p2i[A[cur-1]]
num_loop = len(i2p) - i 
if K < i:
    print(i2p[K])
else:
    print(i2p[i+(K-i)%num_loop])

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k= map(int,input().split())
a= list(map(int, input().split()))
d=[1]
cur=1
while True:
    if a[cur-1] in d:
        roop= d.index(a[cur-1])
        break
    else:
        cur= a[cur-1]
        d.append(cur)



w=len(d)
if k<=w:
    print(d[k])
else:
    k= ((k-roop)%(w-roop)) + roop
    print(d[k])

Submission status: Time Limit Exceeded

Solution code:
N,K =map(int, input().split())
A=list(map(int, input().split()))
now = 1
memo = [1]
while(K > 0):
    now = A[now-1]
    #loop check
    if now in memo:
        loop = memo[memo.index(now):]
        rest = K % len(loop)
        now = loop[rest-1]
        break
    memo.append(now)
    K-=1
print(now)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
T=list(map(int,input().split()))
M=0
A=[1]
for x in range(N+1):
    if T[M] in A or T[M]==M+1:
        key=T[M]
        break

    else:
        t=T[M]
        A.append(t)
        M=t-1
B=len(A)
C=A.index(key)
D=B-C
if K<=B-1:
    F=A[K]
    print(F)
else:
    E=(K+1-C)%D
    if E==0:
        E=D
    F=A[C+E-1]
    print(F)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
i = 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:
import itertools
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
x=1
y=0
z=0
save = []
loop = []
while(1):
    y=A[x-1]
    if y in save:
        z=save.index(y)
        #print(z)
        if K<=z:
            #print(z)
            print(save[K-1])
            break
        else:
            K -= z
        loop=save[z:len(save)+1]
        print(loop[K%len(loop)-1])
        break
    else:
        save.append(y)
        x = y

Submission status: Time Limit Exceeded

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

x=1
l=[1]
for i in range(k):
    y=a[x-1]
    if y not in l:
        l.append(y)
        x=y
    else:
        x=y
        break

j=l.index(x)
mod=(k-len(l))%(len(l)-j)
li=l[j:]
print(li[mod])

Submission status: Time Limit Exceeded

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

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


nowT = 1
n = 0
nP = [1]
while True:
  nowT = A[nowT]
  n+=1
  if nowT in nP:
  	break
  if n==K:
    print(nowT)
    exit()
  nP.append(nowT)

pos = nP.index(nowT)

pat = len(nP)-pos

#print( nowT , nP)
#print( pos , pat )

print( nP[ (K-pos)%pat + pos ] )

Submission status: Time Limit Exceeded

Solution code:
def D():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    next = a[0]
    path = [0]
    while not next in path:
        k -= 1
        if k==0:
            print(next)    
            break
        path.append(next)
        next = a[next - 1]
    if k!=0:
        cyclic = path[path.index(next):]
        print(cyclic[k%(len(cyclic))-1])

D()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

N,K=(int(x) for x in input().rstrip('\n').split())
As = [int(x)-1 for x in input().rstrip('\n').split()]
now=0
pas = [0]
for i in range(N):
  now = As[now]
  pas.append(now)
  if now in pas[:-1]:
    break
arrive = pas.index(now)
loop = len(pas)-arrive-1
if K<=arrive:
  print(pas[K]+1)
else:
  les = K-arrive
  les = les%loop
  res = arrive+les
  print(pas[res]+1)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
i=0
list = list(map(int, input().split()))
list.insert(0,1)
j=0
res=1
rlist=[1]
while(j<K):
  res=list[res]
  if(res in rlist):
    break
  else:
    rlist.append(res)
  j+=1
l=len(rlist)
k=0
while(k<l):
  if(rlist[k]==res):
    break
  k+=1
rlist=rlist[k:]
n=len(rlist)
z=(K-k)%n
print(rlist[z])

Submission status: Time Limit Exceeded

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

p=1
h=[]
h.append(1)
j=k
while j>0:
    p=a[p-1]
    if p in h:
        j=j-1
        c=len(h)-h.index(p)
        j=j%c
        h=[]

    else:
        h.append(p)
        j=j-1

print(p)

Submission status: Time Limit Exceeded

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

c = list()
i = 1
while True:
    if a[i] in c:
        break
    else:
        c.append(a[i])
        i = a[i]

s = c.index(a[i])

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    visited = [0]
    now = 0
    ind = 0
    for _ in range(K):
        a = A[now]-1
        if a in visited:
            ind = visited.index(a)
            break
        else:
            visited.append(a)
            now = a

    loop_visited = visited[ind:]
    ans_ind = (K-ind) % len(loop_visited)

    print(loop_visited[ans_ind]+1)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

# ループの値を取得
work = 1
a_set = set()
loop_start_num = 0
loop_start_count = 0
for _ in range(K):
    work = A[work - 1]
    if work in a_set:
        loop_start_num = work
        break
    else:
        a_set.add(work)
        loop_start_count += 1

if K <= loop_start_count:
    print(work)
else:
    work = loop_start_num
    count = 1
    a_list = [loop_start_num]
    for _ in range(K):
        work = A[work - 1]
        if work in a_list:
            break
        else:
            a_list.append(work)
            count += 1
    print(a_list[(K - loop_start_count - 1) % count])

Submission status: Time Limit Exceeded

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

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

res = [0]
tmp = 0

tf = [-1 for i in range(n)]
tf[0] = 0

for i in range(n):
    tmp = a[tmp] - 1
    if tf[tmp] != -1:
        start = tmp
        break
    else:
        res = res + [tmp]
        tf[tmp] = i+1

lenret = i+1
stridx = tf[start]

cycle = lenret - stridx

if stridx >= k:
    print(res[k]+1)
    exit()
else:
    k = k - stridx

    tmp = k % cycle

    print(res[stridx+tmp]+1)