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:
inp=input
li=list
iinp=lambda : int(inp())
ent=lambda : map(int,inp().split())
lient=lambda : [int(i) for i in inp().split()]
li0= lambda x: [0 for i in range(x)]
livalor=lambda valor,x: [valor for i in range(x)]
stent=lambda : [i for i in inp()]
filldp=lambda valor,x,y: [[valor for i in range(y)] for i in range(x)]
l=len

n,k=ent()
cuidades=lient()
patron=[0]
res=0
cont=0
while True:
    tmp=cuidades[res]-1
    if tmp in patron:
        patron.append(tmp)
        cont+=1
        break
    patron.append(tmp)
    res=tmp
    cont+=1
if k<=cont:
    print(patron[k]+1)
else:
    while patron[0]!=patron[-1]:
        patron.pop(0)
    print(patron[(k-cont)%(l(patron)-1)]+1)

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
for i in range(1,k+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:
def resolve():
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, k = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    A = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    cnt = 0
    root = [1]
    for i in range(k):
        if A[root[i]-1] not in root:
            root.append(A[root[i] - 1])
            cnt += 1
            if i == (k-1):
                print(root[-1])
        else:
            loopstart = int(A[root[i]-1])
            break

    if cnt < k:
        syuki = cnt +1 - root.index(loopstart)
        SyukiList = [x for x in root[root.index(loopstart):]]
        lastpoint = (k - root.index(loopstart)) % syuki
        print(SyukiList[lastpoint])

 #       print(cnt)
 #       print(loopstart)
 #       print(syuki)
 #       print(SyukiList)
 #       print(lastpoint)

resolve()

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
aa=[]
res=0
ans=0
q=0
for i in range(1,k+1):
    aa.append(res)
    res=a[res]-1
    if res in aa:
        m=res+1
        break
k=k-i
for i in range(1,k+1):
    res=a[res]-1
    if res==m-1:
        q=i
        break
k=k-i
if q !=0:
    k=k%q
for i in range(1,k+1):
    res=a[res]-1
print(res+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

if k >= n:
    k = k-n+1

tmp = l[0]
while i < (k-1) :
    tmp = l[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()))

zumi = []
ima = 1
for i in range(K):
    # 行ったことある町を通る＝ループする
    if ima in zumi:
        zumi.append(ima)
        break
    # 同じ町へテレポートしている
    if ima == A[ima-1]:
        print(ima)
        exit(0)
    zumi.append(ima)
    ima = A[ima-1]
else:
    print(ima)
    exit(0)

loop_s = zumi.index(zumi[-1])
zan_loop = K-loop_s
loop = zumi[loop_s:-1]
amari = zan_loop % (len(zumi)-loop_s-1)
print(loop[amari])

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split(" "))
routs = {}
tereporter = list(map(int, input().split()))
for i in range(N):
  routs[i + 1] = tereporter[i]
present_location = 1
for i in range(K):
  present_location = routs[present_location]
print(present_location)

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

x=rlist.index(res)
rlist=rlist[x:]

l=len(rlist)
z=(K-x)%l
print(rlist[z])

Submission status: Time Limit Exceeded

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

for i in range(K):
    List.append(next)
    next = A[next-1]
    if len(set(List)) != len(List):
        break

if len(set(List)) != len(List):
    count = [i for i, x in enumerate(List) if x == List[-1]]
    loop = count[1] - count[0]
    afterloop = (K-count[0])%loop
    print(List[count[0]+afterloop])

else:
    print(next)

Submission status: Time Limit Exceeded

Solution code:
n,m  = map(int,input().split())
a = [int(x) for x in input().split()]

ans1 = [1]
temp = 0
count = 0
while(True):
    ans1 += [a[temp]]
    temp = a[temp]-1
    if len(ans1) !=  len(set(ans1)):
        break
    count += 1
    if count == m:
        print(ans1[len(ans1)-1])
        exit()

num1 = ans1.index(temp+1)
ans2 = ans1[num1:len(ans1)-1]

num = len(ans2)

print(ans2[(m-num1)%num])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

def main():

    # スペース区切りの整数の入力
    s = [int(x) for x in input().split(' ')]
    n = s[0]
    k = s[1]
    a_list = [int(x) - 1 for x in input().split(' ')]

    checked_list = []
    l = 0
    loop = 0
    current_index = 0
    flag = True
    while flag:
        if (current_index in checked_list) is True:
            l = checked_list.index(current_index)
            
            loop = len(checked_list) - l
            flag = False
        else:
            checked_list.append(current_index)
            current_index = a_list[current_index]

    result = 0
    if k <= l:
        result = checked_list[k]
    else:
        num = k - l
        rest = num % loop
        result = checked_list[l+rest]
        
    # 出力
    print(result+1)
    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n,k=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
 
v=1
l=[]
for i in range(k):
    v=a[v-1]
    if v in l:
        index=l.index(v)
        l2=l[index:]
        v=l2[(k-index)%len(l2)-1]
        break
    else:
        l.append(v)
print(v)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    from collections import deque
    q = deque()
    p = deque()
    q.append(1)
    buf = 0
    flg = True
    idx = 1
    cnt = 0
    while flg:
        buf = idx
        idx = a[idx-1]
        cnt += 1
        if cnt == k:
            print(idx)
            return 0
        if idx in q:
            flg = False
        else:
            q.append(idx)
    for i in range(len(q)):
        tmp = q.pop()
        p.appendleft(tmp)
        if tmp == idx:
            break
    if k < len(q):
        print(list(q)[k%len(q)])
    else:
        k -= len(q)
        print(list(p)[k%len(p)])
    return 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
havebeen=[]
current=1
for i in range(k):
  current=a[current-1]
  if current not in havebeen:
    havebeen+=[current]
  else:
    break
if i==k-1:
  print(current)
else:
  nokori=k-1-i
  havebeen=[current]
  for j in range(nokori):
    current=a[current-1]
    if current not in havebeen:
      havebeen+=[current]
    else:
      break
  if j==nokori-1:
    print(current)
  else:
    print(havebeen[nokori%len(havebeen)])

Submission status: Time Limit Exceeded

Solution code:
nk = input().split()
n , k = int(nk[0]), int(nk[1])
a = [int(i) for i in input().split()]

t = 1
tl = [1]
for i in range(len(a)):
    t  = a[t-1]
    
    if t in tl:
        break
    else:
        tl.append(t)
tl2 = tl[tl.index(t):len(tl)]
         
# print(tl)
# print(tl2)

ans = 0

if k <= len(tl):
    ans = tl[k]
else:
    k = k - len(tl)
    j = k % len(tl2)
    ans = tl2[j]

print(ans)

Submission status: Time Limit Exceeded

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

  root = [1]

  break_flag = 0
  for n in range(K):
    new = A_list[ int(root[n])-1 ]
    if new not in root:
      root.append(new)
    else:
      ind = root.index(new)
      break
  else:
    print(new)
    break_flag = 1
    
  if break_flag == 0:  
	  pre = len(root[:ind])
	  roop = len(root)-pre

	  print(root[ind:][(K-pre)%roop])
   
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

pos = 1

if K <= len(A):
    while K > 0:
        pos = A[pos-1]
        K -= 1
    print(pos)
    sys.exit()

hist = [1]

count = 0
while True:
    pos = A[pos-1] # next
    if count > 0 and pos in hist:
        prefix = hist.index(pos)
        cycle = hist[hist.index(pos):]
        break
    hist.append(pos)
    count += 1

# print(count)
# print(hist)
ans = (K - prefix) % len(cycle)
print(cycle[ans])

Submission status: Time Limit Exceeded

Solution code:
import sys
N, K = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
li = [1]
c = 1
for i in range(200001):
    cnt += 1
    c = a[c-1]
    if cnt == K:
        print(c)
        sys.exit()
    if c in li:
        break
    li.append(c)
b = li[li.index(c):]
e = (K-cnt)%len(b)
print(b[e])

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):
    count =i+1
    if(A[now_town-1] in already_town):
        cicle_start_value=A[now_town-1]
        check=True
        break
    else:
        already_town.append(A[now_town-1])
        now_town = A[now_town-1]

if(check==False):
    print(now_town)
else:
    cicle_start=already_town.index(cicle_start_value)
    roop_town=already_town[cicle_start:]
    print(roop_town[(K-cicle_start)%(count-cicle_start)])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
tmp=0
tmplist=[0]
loopinter=k+1
loopstart=0
for i in range(k):
    tmp=a[tmp]-1
    if tmp not in tmplist:
        tmplist.append(tmp)
    else:
        loopstart = tmplist.index(tmp)
        loopinter = len(tmplist)-tmplist.index(tmp)
        break
tmp=0
for i in range((k-loopstart)%loopinter+loopstart):
    tmp=a[tmp]-1
print(tmp+1)

Submission status: Time Limit Exceeded

Solution code:

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

loop = []
trace = [0] * (N+1)

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

    trace[i] = 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:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

A = [a-1 for a in A]


now = 0
past = []
while not (now in past):
    past.append(now)
    now = A[now]

i_loop = past.index(now)
loop = past[i_loop:]
if K >= i_loop:
    print(loop[(K-i_loop) % len(loop)] +1)
else:
    print(past[K] +1)

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:
n,k=map(int,input().split())
l=list(map(int,input().split()))
c=[0]*n
d=[0]*n
g=0
count=1
c[0]=1	
while 2 not in c:
  g=l[g]-1
  if c[g]==1:
    a=d[g]
    c[g]+=1
    cp=g
  else:
    c[g]+=1
    d[g]=count
    count+=1
    
if k-count<=0:
  kk=d.index(k)
  print(kk+1)
else:
  r=(k-a)%(count-a)
  kk=d.index(a+r)
  print(kk+1)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

former = 1
has_come = [0] * n

for i in range(k):
    next_town = tele[former - 1]
    if next_town in has_come:
        break
    has_come[i] = next_town
    former = next_town
else:
    print(former)
    sys.exit(0)


loop_start = tele[former - 1]
loop_start_idx = has_come.index(loop_start)

last_tele = k - i
loop = has_come[loop_start_idx:i]
print(loop[last_tele % len(loop) - 1])

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
now=0
l=[1]
ll=[]
for i in range(n*2):
    now=a[now]
    if now not in l:
        l.append(now)
    elif now in l and now not in ll:
        ll.append(now)
    else:
        break
    now-=1

b=len(l)
c=len(ll)
num=b-c
if k>num:
    k=(k-num)%c
    print(ll[k])
else:
    k=k%b
    print(l[k])

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
a = [int(x) for x in input().split()]
L = 0
S = 0
while S < K:
    S += 1
    L = a[L] - 1
T = L + 1
print(T)

Submission status: Time Limit Exceeded

Solution code:
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
gone = [1]
h = 1
i = 0
start = 0
while i < k:
    h = a[h-1]
    if h in gone:
        start = gone.index(h)
        gone = gone[gone.index(h):]
        break
    gone.append(h)
    i += 1
k = k - start - 1
print(gone[(k+1) % len(gone)])

Submission status: Time Limit Exceeded

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

goal = a[0]
for i in range(k - 1):
  goal = a[int(goal) - 1]
print(goal)

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]
    start=1
    i=0
    while i<k:
        i+=1
        if a[start] not in memo:
            memo.append(a[start])
            start=a[start]
        else:
            memo.append(a[start])
            break
    
    if i==k:
        print(memo[k])
    else:
        rp=[i for i,x in enumerate(memo) if x==memo[-1]]
        roop=rp[1]-rp[0]
        cnt=k-rp[0]
        cnt=cnt-cnt//roop*roop
        print(memo[rp[0]+cnt])




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
town = 1
counted = [0 for _ in range(N)]
for k in range(N):
  X += 1
  counted[k] = town
  town = A[town-1]
  if town in counted:
    roop = counted[counted.index(town):counted.index(0)]
    Q = len(roop)
    I = counted[:counted.index(town)]
    ini = X - Q
    break
if K >= ini:    
  num = (K-ini) % Q
  print(roop[num])
else:
  print(I[K])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    ans_list.append(now_town)
    before_town = now_town

if N >= K:
    print(ans_list[K])
else:
    ans_list = sorted(set(ans_list), key = ans_list.index)
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

Submission status: Time Limit Exceeded

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

aList = list(map(int, input().split()))
seen = []
tsugi = 0
flag = False
for i in range(k):
  tsugi = aList[tsugi]-1
  if tsugi not in seen:
    seen.append(tsugi)
  else:
    index = seen.index(tsugi)
    new_list = seen[index:]
    flag = True
    break
if flag:
  print(new_list[(k-index)%len(new_list)-1]+1)
else:
  print(tsugi+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N, K = list(map(int, input().split()))
K -= 1
A = list(map(lambda x: int(x) - 1, input().split()))
seen = deque()
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 = seen.index(now, loop_s + 1)
size = loop_end - loop_s
if K <= loop_s:
    print(seen[K+1] + 1)
else:
    K -= loop_s
    print(seen[loop_s + 1 + K % size] + 1)

Submission status: Time Limit Exceeded

Solution code:
def move(city, A):
	return A[city - 1]

n, k = map(int, input().split())
A = list(map(int, input().split()))
count = 0
city = 1
already = []
already.append(city)

while count < k:
	next_city = move(city, A)
	city = next_city
	count += 1
	if city in already:
		already.append(city)
		break
	already.append(city)

if count == k:
	print(city)

else:
	start = already.index(city)
	circular = len(already) - start - 1
	k = k - start
	goal = already[k % circular + start]
	print(goal)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

route = [1]
index = k
for i in range(n+1):
    _next = a[route[-1]]
    if _next in route:
        route.append(_next)
        index -= (i+1)
        break
    route.append(_next)

if index <= 0:
    print(route[k])
else:
    route = route[route.index(route[-1]):-1]
    print(route[index % len(route)])

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int,input().split()))
T = 1
cnt = 0
if N > K:
    for k in range(K):
        T = A[T-1]
    print(T)
else:
    for k in range(K):
        T = A[T-1]
        cnt += 1
        if T == 1:
            break
    for k in range(K%cnt):
        T = A[T-1]
    print(T)

Submission status: Time Limit Exceeded

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

roop = [0] 

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

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 resolve():
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, k = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    A = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    cnt = 0
    root = [1]
    next = 1
    for i in range(k):
        if A[root[i]-1] not in root:
            root.append(A[root[i] - 1])
            cnt += 1
            if i == (k-1):
                print(root[-1])
        else:
            loopstart = int(A[root[i]-1])
            loopend = A[-1]
            break

    if cnt < k:
        syuki = cnt +1 - root.index(loopstart)
        SyukiList = [x for x in root[root.index(loopstart):]]
        lastpoint = (k - root.index(loopstart)) % syuki
        print(SyukiList[lastpoint])

 #       print(cnt)
 #       print(loopstart)
 #       print(syuki)
 #       print(SyukiList)
 #       print(lastpoint)

resolve()

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
L= list(map(int, input().split()))
S=[]
S.append(1)
p=0
cnt=0
for i in range(n):
	if L[p] in S:
		cnt+=1
		S.append(L[p])
		break
	else:
		cnt+=1
		S.append(L[p])
		p=L[p]-1

cnt2=0
for i in range(n):
	if S[i]!=L[p]:
		cnt2+=1
	else:
		S2=S[i:]
		break
loop=len(S)-cnt2-1


if cnt>=k:
	print(S[k])
else:
	print(S2[(k-cnt)%loop])

Submission status: Time Limit Exceeded

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

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

    for k in range(1, j):
      if his[j] == his[j-k]:
        K = K % k
        break
      
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())
l=[int(_) for _ in input().split()]

cycle=[1]
ctr=1
for i in range(N):
  cycle.append(l[ctr-1])
  ctr=l[ctr-1]
  if l[ctr-1] in cycle:
    cycle.append(l[ctr-1])
    break
start= cycle.index(cycle[-1])
shuuki=len(cycle[start:-1])
if len(cycle) > K:
  print(cycle[K])
  
else:
  print(cycle[start+(K-start)%shuuki])

Submission status: Time Limit Exceeded

Solution code:
import sys

def mink(numk,l):
    x = 1
    for _ in range(k):
        x = a[x-1]
    return x

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

# kが小さい時
if k <= 10**5:
    print(mink(k,a))
    sys.exit()

# ans = 0

z = 1
p = [1]
for _ in range(k):
    z = a[z-1]
    if z in p:
        p.append(z)
        break
    p.append(z)

# 周期の来る前の回数
num = p.index(p[-1])

p = p[num:]

# 周期
pn = len(p)-1

k -= num

# 回数/周期のあまり
anum = k%pn
print(p[anum])

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [1]
i = 1
n = 0
l = 1
while 1:
  i = A[i]
  if i not in B:
    B.append(i)
    l = l + 1
    if l == K + 1:
      print(i)
      break
  else:
    n = B.index(i)
    print(B[n + (K-n) % (l-n)])
    break

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))
    town = path[first_visit + k]
    break
    
  path.append(town)
    
  k = k - 1
  j = j + 1

print(town+1)

Submission status: Time Limit Exceeded

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

machi = [0] * (N + 1)
next = 0

for x in range(1, len(machi)):
    machi[x] = A[x-1]

next = machi[1]

for i in range(K-1):
    next = machi[next]

print(next)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

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

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]
  
temp=1
trace=[1]
circle=[]
for i in range(K):
  temp=A[temp-1]
  ans=func1(trace,temp)
  if len(ans)==1:
    #print(temp)
    #print(trace.index(temp))
    #start=trace.index(temp)
    circle=trace[ans[0]:]
    #print(circle)
    break
  trace.append(temp)
  #print("temp",temp)
#print((K-i)%len(circle)-1)
if len(circle)!=0:
  amari=(K-i)%len(circle)-1
  print(circle[amari])
else:
  print(temp)

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<=11:
    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:
n,k = map(int,input().split())
A = list(map(int,input().split()))
m = 1
M = 0
i = 0
while M!=1:
    m = A[m-1]
    M = m
    i+=1

j = -1
K = k
while k>0:
    k = k-i
    j+=1

l = 0
while l != (K-i*j):
    m = A[m-1]
    l += 1

print(m)

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  A[now-1] not in nowlist:
            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()))

n = 1
ls = [1]
lps = 0
lpn = 0
for i in range(K):
    n = A[n-1]
    ls += [n]
    if A[n-1] in ls:
        lps = ls.index(A[n-1])
        lpn = i+2-lps
        break
if lpn != 0:
    print(ls[lps:][(K-lps)%lpn])
else:
    print(ls[lps:][K])

Submission status: Time Limit Exceeded

Solution code:

def main():
    n,k = map(int,input().split())
    dests =[0 for _ in range(n)]
    telepos = list(map(int,input().split()))
    for i,t in enumerate(telepos):
        dests[i] = t-1
    print(dfs(dests,k))




def dfs(dests,k,here = 0):
    here = 0
    telepo_times = 0
    cycle_start = -1
    cycle_end = -1
    cycle_length = -1
    visited = []


    #循環検出！同じ町にいつ戻ってくるのか
    while telepo_times < k:
        #print("現在地は",here)
        if here in visited:
            cycle_start = visited.index(here)
            #print(cycle_start,"でループが始まった")
            cycle_end = telepo_times - 1
            #print(cycle_end,"でループが終わった")
            cycle_length = cycle_end - cycle_start + 1
            #print("サイクルの長さは",cycle_length,"だ")
            break

        visited.append(here)
        telepo_times += 1
        here = dests[here]
    if cycle_length == -1:
        return here + 1

    cycle_index = (k-cycle_start) % cycle_length
    return visited[cycle_start + cycle_index] + 1




    #telepo_times = 0
    #while telepo_times < (k - cycle_start)%cycle_length:
    #    telepo_times += 1
    #    here = dests[here]







if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

x = True

while x:
    count += 1
    c.append(a[c[-1]])
    if count == k:
        ans = c[-1]
        x = False
    if c[-1] in c[0:-1]:
        s = c.pop()
        t = c.index(s)
        u = len(c) - t
        x = False
        m = (k-t) %  u
        ans = c[t+m]
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

P=[1]
now=1

for i in range(K):
    now=A[now-1]
    if now in P:
        x = i+1
        y = P.index(now)
        break
    elif i == K-1:
        print(now)
        exit()

c=(K-y)%(x-y)

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

print(now)

Submission status: Time Limit Exceeded

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

while count < K:
    now = where_go[now] - 1
    count += 1

print(now + 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import itertools

NK = input()
NK = NK.split()
N = int(NK[0])
K = int(NK[1])

As = input()
As = As.split()
As = np.array(As, dtype = int)

x = 1
ls = [1]
bflag = False
for i in range(K):
    x = As[x-1]
    if x in ls:
        bflag = True
        k = K - i - 1
        break
    ls.append(x)

if bflag:
    idx = ls.index(x)
    ls = ls[idx:]
    perc = len(ls)
    print(ls[k%perc])
    

else:
    print(x)

Submission status: Time Limit Exceeded

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

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

if K==0:
  print(1)
  exit()

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:
n, k = [int(i) for i in input().split()]
A = [0] + [int(i) for i in input().split()]
# first study the cycle of A
cycle = [A[1]]
i = A[1]
while A[i] not in cycle:
    cycle.append(A[i])
    i = A[i]
cycle.append(A[i])
interest_ind = cycle.index(cycle[-1])
first_part = cycle[:interest_ind]
second_part = cycle[interest_ind:-1]
p = len(second_part)
k -= 1
if k < interest_ind:
    print(first_part[k])
else:
    k -= interest_ind
    k %= p
    print(second_part[k])

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]
f = his.append
while True:
    n = a[n]
    if n in his:
        f(n)
        break
    f(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:
from collections import deque
import numpy as np
n,k = map(int,input().split())
a = list(map(int,input().split()))
roop_find = deque([0])
i = 0
count = 0
while a[i]-1 not in roop_find:
    roop_find.append(a[i]-1)
    i = a[i] - 1
    count += 1
b = roop_find.index(a[i]-1)
roop = list(np.array(roop_find)[b:count+1])

if k < b:
    print(list(roop_find)[k]+1)
else:
    k-=b
    e = k % (count+1-b)
    print(roop[e]+1)

Submission status: Time Limit Exceeded

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

moves = [1]
i = 1

while True:
    next_i = A[i-1]
    if next_i in moves:
        break
    moves.append(next_i)
    i = next_i

p = moves.index(next_i)
l = len(moves) - p

ans = moves[p+(K-p)%l]

if K < p:
    ans = moves[K]

print(ans)

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)