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())
al = list(map(int, input().split())) 
x=0
now = 1
nl =[1]
for i in range(0,k):
    now = al[now-1]
    nl.append(now)
    if len(nl) != len(set(nl)):
        loops = nl.index(now)
        loope = i+1
        break
    elif i==k-1:
        print(now)
        exit()

k = (k-loops) % (loope-loops) +loops
now =1
for i in range(0,k):
    now = al[now-1]
    nl.append(now)

print(now)

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**16:
    print(mink(k,a))
    sys.exit()

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:
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
    my_append = visited.append
    for _ in range(K):
        a = A[now]-1
        if a in visited:
            ind = visited.index(a)
            break
        else:
            my_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:
#D
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))


town = [[0 for _ in range(n)] for _ in range(n)]


for i, v in enumerate(a):
    town[i][v-1] = 1
    

result = 0
for _ in range(k):
    town_num = town[result].index(1)
    result = town_num

    
print(result+1)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=input().split()
go=[]
fin=0
now=1
loop=0

while (1):
  go.append(a[now-1])
  now=int(a[now-1])
  if str(now) in go==True:
    break;
  
fin=go.index(go[len(go)-1])
del(go[len(go)-1])
loop=len(go)-fin
k-=fin
k-=1

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
status = 1
while i < k and status:
    h = a[h-1]
    for j in range(len(gone)):
        if gone[j] == h:
            start = j
            status = 0
    if status:
        gone.append(h)
        i += 1
k = k - start - 1
print(gone[start + (k+1) % (len(gone) - start)])

Submission status: Time Limit Exceeded

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

  #行ったことある町リスト
  d=[1]
  for _ in range(K):
    T=A[d[-1]-1]
    if T in d:
      break
    d.append(T)
  F=d.index(T)
  LOOP_TOWN=d[F:]
  q=(K-F)%len(LOOP_TOWN)
  print(LOOP_TOWN[q])

if __name__ == "__main__":
    main()

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**19:
    print(mink(k,a))
    sys.exit()

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:
def main():
  N,K=map(int,input().split())
  A=list(map(int,input().split()))

  #行ったことある町リスト
  d=[1]
  for _ in range(K):
    T=A[d[-1]-1]
    if T in d:
      break
    d.append(T)
  F=d.index(T)
  LOOP_TOWN=d[F:]
  q=(K-F)%len(LOOP_TOWN)
  print(LOOP_TOWN[q])

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

route = [1]
current_town = 1

for i in range(min(K, 2*(10**5))):
    current_town = A[current_town-1]
    if current_town in route:
        route.append(current_town)
        break
    else:
        route.append(current_town)  
        
start_index = route.index(route[-1])
loop_cnt = len(route) - (start_index+1)
if loop_cnt!=0:
    K -= route.index(route[-1])
    add_num = K % loop_cnt
    print(route[start_index+add_num])
else:
    print(route[K])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
ans = 1
cir = 0
a = 0
if k < n:
  for i in range(k+1):
    sub[i] = ans
    ans = A[ans-1]
  print(sub[k])
else:
  for i in range(n):
    sub[i] = ans
    if A[ans-1] in sub:
      a = sub.index(A[ans-1])
      cir = i+1 - a
      break
    else:
      ans = A[ans-1]
  print(sub[a + (k - a) % cir])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [0]*n
seen[0] = 1
#load = []
res = 0
flag = 0
for i in range(k-1):
    #print(seen)
    #print(nxt)
    if not(nxt in seen):
        seen[i+1] = nxt
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        #print(root)
        #print(k-1-i)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = input().split()
A = [int(a) for a in A]
S = []
a = 1
number = 0
for _ in range(N + 1):
    b = A[a - 1]
    if b in S:
        c = S.index(b)
        break
    number += 1
    S.append(b)
    a = b
T = S[c:number]
if K <= N:
    print(S[K - 1])
else:
    z = K - c - 1
    y = z % (number - c)
    print(T[y])

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
A.insert(0,0)
B = [1]
count = 1
S = 1
while 1:
    if A[S] in B:
        break
    count+=1
    S = A[S]
    B.append(S)

D = B.index(A[S])
C = (K-D) % (count-D)

if len(B)>=K+1:
    print(B[K])
else:
    print(B[C+D])

Submission status: Time Limit Exceeded

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

history = []

index = 0
cnt = 0
loop_start_city_index = 0
while True:
    if cnt == k:
        print(index+1)
        exit()

    if index in history:    # すでに訪れた街にきた
        loop_start_city_index = index
        break
    history.append(index)
    index = a[index] - 1
    cnt += 1

from_start_to_loop_start = history.index(loop_start_city_index)
loop_length = len(history) - from_start_to_loop_start

print(history[from_start_to_loop_start + (k-from_start_to_loop_start)%loop_length] + 1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys

def main():
    input = sys.stdin.readline
    n,k = map(int,input().split())

    a = [int(x) for x in input().split()]

    #ループを探す？

    checked_list = [1]
    que = deque([a[0]])

    counter = 0
    while True:
        counter += 1
        x = que.popleft()
        if counter == k:
            print(x)
            exit()
        if x in checked_list:
            startflg = checked_list.index(x)
            loop_length = counter - startflg
            loop_list = checked_list[startflg:counter]
            amari = (k-startflg) % loop_length
            print(loop_list[amari])
            exit()
        else:
            que.append(a[x-1])
            checked_list.append(x)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
 
next_city = 1
visited = []
x = [-1]*n
while x[next_city-1] == -1:
    visited.append(next_city)
    next_city = A[next_city-1]
idx = visited.index(next_city)
visited2 = visited[idx:]
mod = len(visited2)
x = (k%mod-idx)%mod
print(visited2[x])

Submission status: Time Limit Exceeded

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

l = [1]
m = []

X = 1

for _ in range(K):
    if A[X-1] in l:
        X = A[X-1]
        break
    l.append(A[X-1])
    X = A[X-1]

m = l[:l.index(X)]
del l[:l.index(X)]

# print(m)
# print(l)
print(l[(K-len(m))%len(l)])

Submission status: Time Limit Exceeded

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

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

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

        step1 = step
    while(True):
        t += 1
        step1 = a[step1]
        if step1 == step:
            break
    result = step
    k = (k-4) % 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()))
count = 0
spot = 1

while(count< K):
    spot = A[spot-1]
    count += 1

print(spot)

Submission status: Time Limit Exceeded

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

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

L = []
L.append(1)
f = 1
memo = []
flg = 0
leng = 0

while 1:
    L.append(A[f-1])
    memo.append(f)
    f = A[f-1]
    leng += 1
    for i in range(leng):
        if f == memo[i]:
            loop = leng - i
            flg = 1
            break
    if flg == 1:
        break

#print(L)
#NL = L[0:-1]
#print(NL)
#print(memo)
#print(loop)

#a = K % len(NL)
#print(NL[a])

cycle = memo[-loop:]
#print(cycle)

if K <= len(memo) - loop:
    print(memo[K])
else:
    K -= len(memo) - loop
    a = K % len(cycle)
    print(cycle[a])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
memo1 = [0]*(n+1)
memo2 = []
tmp = 0
t = 1
index = 0

while True:
    tmp = l[t-1]
    if memo1[t-1] != tmp:
        memo1[t-1] = tmp
        memo2.append(tmp)
        t = tmp 
    else:
        memo2.append(tmp)
        indexes = [i for i, x in enumerate(memo2) if x == memo1[t-1]]
        index = indexes[1] - indexes[0]
        exit

tmp2 = k - indexes[1]
if tmp2 <= 0:
    print(memo2[k-1])
else:
    tmp2 = tmp2%index
    if tmp2 == 0:
        print(memo1[indexes[0]])
    else:
        print(memo2[indexes[0]+tmp2-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()]

now = 0
next = a[0]

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

Submission status: Time Limit Exceeded

Solution code:
def main():
  n,k,*a = map(int,open(0).read().split())
  c = [0]
  visit = [False]*n
  visit[0] = True
  for i in range(n):
    nt = a[c[-1]]-1
    if visit[nt]:
      ws = c.index(nt)
      wst = c[:ws]
      wrt = c[ws:]
      wr = len(wrt)
    else:
      c.append(nt)
      visit[nt] = True
  if ws > k:
    ans = wst[k]
  else:
    ans = wrt[(k-ws)%wr]
  print(ans+1)
main()

Submission status: Time Limit Exceeded

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

#行ったことある町リスト
d=[1]
for _ in range(K):
  T=A[d[-1]-1]
  if T in d:
    break
  d.append(T)
F=d.index(T)
LOOP_TOWN=d[F:]
q=(K-F)%len(LOOP_TOWN)
print(LOOP_TOWN[q])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def main():
    n,k = map(int,input().split())

    a = [int(x) for x in input().split()]

    #ループを探す？

    checked_list = [1]
    que = deque([a[0]])

    counter = 0
    while True:
        counter += 1
        x = que.popleft()
        if counter == k:
            print(x)
            exit()
        if x in checked_list:
            startflg = checked_list.index(x)
            loop_length = counter - startflg
            loop_list = checked_list[startflg:counter]
            amari = (k-startflg) % loop_length
            print(loop_list[amari])
            exit()
        else:
            que.append(a[x-1])
            checked_list.append(x)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
r = [None] * N
i = 0
cur = 0
m = None
while True:
    if r[cur] is not None:
        m = i - r[cur]
        K -= ((K-i)//m)*m
    r[cur] = i
    i += 1
    if i == K:
        print(A[cur])
        exit()
    cur = A[cur] - 1

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**10:
    print(mink(k,a))
    sys.exit()

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:
nk=input()
nk=nk.split( )
n,k=int(nk[0]),int(nk[1])
aa=input()
aa=aa.split( )
aa=[int(i) for i in aa]
count=len(aa)
town=1
for i in range(k):
    town=aa[town-1]
print(town)

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import factorial
from collections import Counter
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, k = MAP()
a = LIST()
tugi = [0]+a
town  =1
route = [0]*n
route[0] = town

for i in range(n):
    town = tugi[town]
    if town in route:
        i = route.index(town)
        notcycle = route[:i]
        cycle = route[i:]
        break
    route[i+1] = town

for i in range(n):
    if cycle[i] == 0:
        cycle = cycle[:i]
        break

if len(notcycle) >k:
    print(notcycle[k])
else:
    k -= len(notcycle)
    print(cycle[k%len(cycle)])

Submission status: Time Limit Exceeded

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

rs = 0
p = -1
r = []
d = 1
# i回目
if N > K:
  for i in range(K+1):
    r.append(d)
    d = A[d - 1]
else:
  for i in range(N+1):
    r.append(d)
    d = A[d - 1]
    for j in range(len(r)):
      if r[j] == d:
        rs = j
        p = i - j + 1
        break
if p == -1:
  print(r[-1])
else:
  print(r[rs+(K-rs)%p])

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
al=list(map(int,input().split()))
pos=0
for val in range(len(al)):
   al[val]-=1
while(k!=0):
  i=pos
  pos=al[i]
  k-=1
print(pos+1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, k = map(int, input().split(" "))
    a_list = list(map(int, input().split(" ")))
    flag = False

    loop_list = [1]
    i = 0
    cnt = 0
    while not flag:
        # a_list[i] は cnt 回目の移動で移動する先
        cnt += 1
        if cnt == k:
            return a_list[i]

        if a_list[i] in loop_list:
            loop_list = loop_list[loop_list.index(a_list[i]) :]
            break
        loop_list.append(a_list[i])
        i = a_list[i] - 1

    return loop_list[(k - cnt) % len(loop_list)]


if __name__ == "__main__":
    print(main())

Submission status: Time Limit Exceeded

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

counters = [0] * N
now = 0
hoge = []
hoge.append(now)

counters[now] += 1
now = l[now] - 1

while True:
    if 2 in counters:
        break
    else:
        hoge.append(now)
    counters[now] += 1
    now = l[now] - 1

start = hoge[-1]
start_idx = hoge[:len(hoge)-1].index(start)
ans = hoge[start_idx:len(hoge)-1]

kaisuu = len(hoge[:len(hoge)-1]) - len(ans)

if K > kaisuu:
    print(ans[(K - kaisuu) % len(ans)] + 1)
else:
    print(hoge[K] + 1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

import sys
input = sys.stdin.readline

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

A = [int(a) for a in input().split()]

Jump = [None] * (n)
Jump[0] = 1
next = 0
for i in range(n):
    to = A[next]
    if to in Jump:
        start = Jump.index(to)
        end = i
        dis = end - start + 1

        roop = (k-start) % dis +1
        if roop == 0:
            print(Jump[end])
            break
        else:
            print(Jump[start+roop-1])
            break

    else:
        Jump[i+1] = to
        next = to-1
    if i ==k-1:
        print(to)
        break

Submission status: Time Limit Exceeded

Solution code:
n, k = (int(each) for each in input().split())
a = [int(each) for each in input().split()]
where = 1
record = []
counter = 1
boost_flg = False
while counter <= k:
    where = a[where-1]
    record.append(where)
    try:
        i = record.index(where)
    except:
        i = 0
    # print('record', record, 'where', where, 'i', i)
    if len(record)-i >= 2 and boost_flg == False:
        # print(len(record)-i-1, '周期で',where ,'をサイクルしている')
        counter += (len(record)-i-1)*((k-counter)//(len(record)-i-1))
        boost_flg = True
    # print(counter)
    counter += 1
print(where)

Submission status: Time Limit Exceeded

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

now = 1
toLoop = 0 #1の町からループに入るまでのワープ数
towns = [n+1 for n in range(1,N)]
for i in range(1,K+1):
	now = A[now-1]
	loop_num = i
	if now in towns:
		towns.remove(now)
	else:
		start = now
		count = 1
		now = A[now-1]
		while start != now:
			count += 1
			now = A[now-1]
		toLoop = loop_num - count
		loop_num = count
		break

if K <= loop_num:
	print(now)
else:
	warp_num = (K-toLoop)%loop_num
	for _ in range(warp_num): now = A[now-1]
	print(now)

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
for i in range(N):
  if i >= K:
    ans = current + 1
    flag = 1
    break
  a = A[current]-1 
  if a in B:
    flag = 2
    break
  B.append(a)
  current = a
    
if flag == 1:
  print(ans)
elif flag == 2:
  first = B.index(a)
  rep = B[first:]
  r = (K-i-1)%(len(rep))
  print(rep[r]+1)
else:
  print(current+1)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def main():

    input = stdin.readline

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

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

    itta_list = []
    idou_num = 0

    pos = 0

    for i in range(K):
        # itta_list記憶
        if pos not in set(itta_list):
            itta_list.append(pos)
        else:
            idou_num = i
            break

        # 次の街へ
        pos = A[pos] - 1
    else:
        ans_num = pos + 1
        print(ans_num)
        return

    roop_list = itta_list[itta_list.index(pos):]

    amari = (K - idou_num) % len(roop_list)

    ans_pos = roop_list[amari] + 1

    print(ans_pos)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

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

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

former = 1
has_come = [0] * n

def fun():
    global former
    for i in range(k):
        next_town = tele[former - 1]
        if next_town in has_come:
            return i
        has_come[i] = next_town
        former = next_town
    else:
        print(former)
        return None

rtn = fun()
if rtn is not None:
    loop_start = tele[former - 1]
    loop_start_idx = has_come.index(loop_start)

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

Submission status: Time Limit Exceeded

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

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [1]
#load = []
res = 0
flag = 0
for i in range(k-1):
    #print(seen)
    #print(nxt)
    if not(nxt in seen):
        seen.append(nxt)
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        #print(root)
        #print(k-1-i)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]
    order_dict = dict(zip(range(1,N+1),A_list))

    foot_print = []

    goto = 1
    for i in range(N):
        if goto in foot_print:
            break
        else:
            foot_print.append(goto)
            goto = order_dict[goto]
    else:
        goto = 1

    foot_print2 = []
    for i in range(N):
        if goto in foot_print2:
            break
        else:
            foot_print2.append(goto)
            goto = order_dict[goto]
    else:
        goto = 1

    if K > len(foot_print):
        temp_num = K -len(foot_print)
        temp_num = temp_num % len(foot_print2)
        for i in range(temp_num):
            goto = order_dict[goto]

    else:
        goto = 1
        for i in range(K):
            goto = order_dict[goto]

    print(goto)
if __name__ == "__main__":
    resolve()

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 
ii=0
C=[]
for i in range(N):#i:1~N
  j=A[j-1]
  if j in C:
    ii=C.index(j)+1
    T=i-ii+1
    break
  C.append(j)
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 = list(map(int, input().split()))

num = 1
l = []
l.append(num)
i = 0
while i < K:
  num = A[num-1]
  if num not in l:
    l.append(num)
    i += 1
  else:
    i = K - (K-1-i)%(i+1-(l.index(num)))
    break;
    
while i < K:
  num = A[num-1]
  i += 1
  
print(num)

Submission status: Time Limit Exceeded

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

# print(N,K,A)

target = 1
l = [1]
lorp_on = False
for i in range(1,K+1):
    target = A[target-1]
    # print([target])

    if target in l:
        idx = l.index(target)
        lorp_on = True
        break
    l.append(target)
# print(l)

if lorp_on:

    lorp_length = len(l)-idx
    lorp = l[idx:]

    # print(lorp,lorp_length)

    ans_idx = (K-idx)%lorp_length
    # print(ans_idx)
    print(lorp[ans_idx])

else:

    print(l[K])

Submission status: Time Limit Exceeded

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

# １周の周期を求める
place = 1
term = 1
i = 0
history = [1]

for x in range(K):
    place = A[place - 1]
    if place in history:
        i = history.index(place)
        term = x + 1 - i
        break
    else:
        history.append(place)

K = (K - i) % term
for x in range(K):
    place = A[place - 1]

print(place)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    next = a[0]
    for i in range(k-1):
        next = a[next-1]
    print(next)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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
    if len(ans1) == m+1:
        print(ans1[len(ans1)-1])
        exit()

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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=deque()
trace.append(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)
    for _ in range(ans[0]):
      trace.popleft()
    #circle=trace[ans[0]:]
    circle.append(1)
    #print(circle)
    break
  trace.append(temp)
  #print("temp",temp)
#print((K-i)%len(circle)-1)
if len(circle)!=0:
  amari=(K-i)%len(trace)-1
  print(trace[amari])
else:
  print(temp)

Submission status: Time Limit Exceeded

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

while c.count(c[-1]) == 1:
    c.append(a[c[-1]])

t = c.index(c[-1])
if k <= t:
    ans = c[k]
else:
    ans = c[t + ((k-t) % (len(c)-1-t))]

print(ans)

Submission status: Time Limit Exceeded

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

for j in range(2*N):
    i = A[i-1]
    if i in B:
        I = A[i-1]
        break
    else:
        B.append(i)
        
count = 0
flag = True
i = 1
while flag:
    if I == A[i-1]:
        flag = False
        CT = count
    else:
        count += 1
        i = A[i-1]
        
flag = True
i = I
C = [I]

while flag:
    if A[i-1] == I:
        flag = False
    else:
        i = A[i-1]
        C.append(i)

if K >= CT:
    D = C[(K-CT-1)%(len(C))]
    print(str(D))
else:
    print(str(B[K]))

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 = []
    checked_dict = {}
    l = 0
    loop = 0
    current_index = 0
    flag = True
    while flag:
        if (current_index in checked_dict.values()) is True:
            l = checked_list.index(current_index)
            
            loop = len(checked_list) - l
            flag = False
        else:
            checked_list.append(current_index)
            checked_dict[current_index] = 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 = map(int,input().split())
A = list(map(int,input().split()))
already_town =[1]
now_town=1
check=False
for i in range(K):
    if(A[now_town-1] in already_town):
        cicle_start_value=A[now_town-1]
        check=True
        break
    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)%((i+1)-cicle_start)])

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()]
D = {}
C = 1
for i in range(1,N+1):
    l =A[i-1]
    D.update({i:l})
for i in range(1,K+1):
    C = D[C]
print(C)

Submission status: Time Limit Exceeded

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

n, k = map(int, input().split())
A = list(map(int, input().split()))
a = [0]*n
b = A[0]
per = []
tes = -1

if n >= k:
    for _ in range(k-1):
        b = A[b-1]
    print(b)
    exit()

for _ in range(k*k):
    a[b-1] += 1
    tes += 1
    if 3 in a:
        tes -= len(per)*2
        break
    if 2 in a:
        per.append(b)
    b = A[b-1]

_len = len(per)

ans = (k - tes) % _len

print(per[ans-1])

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 and len(loop) < k + 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:
import numpy as np
n,k=map(int,input().split(" "))
line=input().split(" ")
a=[]
h=[0 for i in range(n)]
for i in range(n):
  a.append(int(line[i]))
r=0
g=1
i=0
while g<k and r==0:
  if h[i]==0:
    h[i]=g
    i=int(a[i])-1
    g=g+1
  else:
    r=(k-g)%(g-h[i])
g=0
while g<r:
  i=int(a[i])-1
  g=g+1
print(a[i])

Submission status: Time Limit Exceeded

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

dic = dict()
for i, j in enumerate(a, 1):
    dic[i] = j

pos = 1
for l in range(k):
    pos = dic[pos]
print(pos)