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())
loop_=[1]
lst=list(map(int,input().split()))
 
for i in range(k):
    num=lst[loop_[-1]-1]
    if num in loop_:
        tar=num
        break
    else:
        loop_.append(num)
    if i==k-1:
        print(loop_[k])
        exit() 
        
loop__=loop_[loop_.index(tar):]
 
if k<=len(loop_):
    print(loop_[k])
else:
    num=k+1-len(loop_)
    print(loop__[num%len(loop__)-1])

Submission status: Time Limit Exceeded

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

# 入力を整数に変換して受け取る
def input_int():
    return int(input())


# マイナス1した値を返却
def int1(x):
    return int(x) - 1


# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
    return map(int, input().split())


# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
    return tuple(map(int, input().split()))


# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    n, k = input_to_int_map()
    a = {i + 1: int(val) for i, val in enumerate(input().split())}

    place = 1
    place_goned = [place]
    i = 0
    while i < n:
        place = a[place]
        if place in place_goned:
            break

        place_goned.append(place)
        i += 1

    if k < len(place_goned):
        return place_goned[k]

    pre_loop_cnt = place_goned.index(place)
    move_cnt = k - pre_loop_cnt
    loop_list = place_goned[pre_loop_cnt:]
    return loop_list[move_cnt % len(loop_list)]


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

Submission status: Time Limit Exceeded

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

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


circle = [1]
st = -1
while True:
  point = to[circle[-1]]
  if point in circle:
    st = point
    break
  circle.append(point)

l1 = circle.index(st)
l2 = len(circle) - l1

if k < l1:
  print(circle[k])
elif k < l1 + l2:
  print(circle[k])
else:
  div = (k - l1) % l2
  print(circle[l1+div])

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
index=[0]
index.append(A[0]-1)
flag=0
count=0
for _ in range(1,K):
    t=index[-1]
    if (A[t]-1) in index:
        index.append(A[t]-1)
        flag=1
        break
    index.append(A[t]-1)
if flag==0:
    print(index[-1]+1)
    exit()
check = index[-1]
for i,c in enumerate(index):
    if c==check:
        a_path=i
        break
#print(index)
#print("a_path",a_path)
K-=a_path
t=len(index)-1-a_path
#print("t",t)
print(index[a_path+K%t]+1)
#print(index)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
A =[0]+list(map(int,input().split()))
l= 1
a = A[1]
C = [1]
 
while (not a in C):
  C.append(a)
  a = A[a]
  l += 1
  
b=A[a]
B = [a]
o=1
if k <= l:
  print(C[k])
    
else:
  while b != a:
    B.append(b)
    b = A[b]
    o += 1
  m = (k-l) % o
  print(B[m])

Submission status: Time Limit Exceeded

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

town  = a[0]
chain = [1, a[0]]
loop  = 1

# 何回目に戻ってくるか調べる
while town != 1:
  town = a[town-1]
  chain.append(town)
  loop += 1
  
chain = chain[:-1]
print(chain[k%loop])

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
print(L + 1)

Submission status: Time Limit Exceeded

Solution code:




data = input().split()

N, k = int(data[0]), int(data[1])

tab = []

data = input().split()
for i in range(N):
    tab.append(int(data[i]) - 1)


parcour = []

position = 0

while (not (position in parcour)):
    parcour.append(position)
    position = tab[position]
i = 0
while parcour[i] != position:
    i+= 1

sizeCycle = len(parcour) - i 

#print (parcour)
if (k < len(parcour)):
    print(parcour[k] + 1)
else:
    print(parcour[i + ((k - i) % sizeCycle)] + 1)

Submission status: Time Limit Exceeded

Solution code:

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

seen = {}
cur = 1
count = 0

while(k):
	
	if cur not in seen:
		seen[cur] = count
		count += 1
	nex = adj[cur]
	if nex not in seen:
		k -= 1
		cur = nex
	elif nex in seen:
		if seen[nex] > seen[cur]:
			k -= 1
			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()))
now = 1
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()))

visited = [1]
last = 1

for i in range(K):
    t = A[last - 1]
    if t in visited:
        z = K - i - 1
        r = visited[visited.index(t):]
        l = len(r)
        print(r[z % l])
        exit()
    else:
        visited.append(t)
        last = t

print(visited[-1])

Submission status: Time Limit Exceeded

Solution code:

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

route = [0]
now_i = 0
loop_len = 0
for i in range(1, N):
    next_i = A[now_i]-1
    if next_i in route:
        loop_len = i - route.index(next_i)
        break
    route.append(next_i)
    now_i = next_i


base = len(route) - loop_len

if K < len(route):
    print(route[K]+1)
else:
    K -= base
    m = K % loop_len
    idx = route[base:][m]
    print(idx+1)

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
(N,K) = map(int,input().split())
A = list(map(int,input().split()))
route = [1]
past = 1
for i in range(K):
    past = A[past-1]
    if past in route:
        loop = (route.index(past),i+1)
        flag = 0
        break
    else:
        flag = 1
        route.append(past)
if flag == 1:
    print(route[-1])
else:
    print(route[loop[0]+(K-loop[0])%(loop[1]-loop[0])])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
A = list(map(int, input().split()))
import collections
res = collections.deque('1')
rep = [0,0]
for i in range(n):
    #print(res)
    if str(A[int(res[-1])-1]) not in res:
        res.append(str(A[int(res[-1])-1]))
    else:
        rep[0] = res.index(str(A[int(res[-1])-1]))
        rep[1] = len(res)
        break

#print(res)
#print(rep)
if k < len(res):
    print(res[k])
else:
    rep_num = rep[1] - rep[0]
    remainder = (k-rep[0])%rep_num
    print(res[rep[0] +remainder])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

cl=[]
city=1
count=0
while city not in cl:
    count+=1
    cl.append(city)
    city=a[city-1]
#arrive at city in count, len(cl)=count
if k<count:
    print(cl[k])
    sys.exit()
#print(cl)
    
loop=0
for i in range(len(cl)):
    if cl[i]==city:
        loop=len(cl)-i
        break
#print(loop)
d=(k-count)%loop

for i in range(d):
    city=a[city-1]

print(city)

Submission status: Time Limit Exceeded

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

keiro = [1]

flag = True
now = 1
start = 0
loopN = 0
loop_list = []
while flag:
    if A[now-1] in keiro:
        flag = False
        start = keiro.index(A[now-1])
        loopN = len(keiro[start:])
        loop_list = keiro[start:]
    else:
        keiro.append(A[now-1])
        now = A[now-1]

if K - start >= 0:
    print(loop_list[(K - start) % loopN])
else:
    print(keiro[K])

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
    while flg:
        buf = idx
        idx = a[idx-1]
        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:
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
  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:
import sys
n,k = map(int,input().split())
ls = list(map(int,input().split()))
new = []
def sub(k,s):
    return k-s
p = [1]
i = 0
a = 0
t = 0
ans1 = 0
ans2 = 0
while i<k-1:
    if i ==0:
        a = ls[i]
        p.append(a)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            print(p[((k+1-ans1)%ans2)-1])
            sys.exit()
        b = ls[a-1]
        p.append(b)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            print(p[((k+1-ans1)%ans2)-1])
            sys.exit()
        i += 1
    else:
        a = b
        b = ls[a-1]
        p.append(b)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            ansl = p[t:]
            print(ansl[((k+1-ans1)%ans2)-1])
            sys.exit()
        i += 1
print(b)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
now = 1
for i in range(1, k+1):
    now = a[now-1]
    if now == 1:
        roop = i
        k = k % roop
        for j 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()))

current = 1
path = [1]

while K > 0:
    if A[current - 1] in path:
        loop = path[path.index(A[current - 1]):]
        path += [loop[(K-1) % len(loop)]]
        break

    path += [A[current - 1]]
    current = A[current - 1]
    K -= 1

print(path[-1])

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 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 = map(int,input().split())
alist = list(map(int,input().split()))

town = 1
for i in range(k):
    town = alist[town-1]
print(town)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
posi = 1
tran = [1]
for i in range(k):
    posi = A[posi-1]
    if posi in tran:
        break
    tran.append(posi)
roop = tran[tran.index(posi):]
div = len(tran) - len(roop)
k -= div
p = len(roop)
q = k%p
print(roop[q])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

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):
        if A[now]-1 in visited:
            ind = visited.index(A[now]-1)
            break
        else:
            visited.append(A[now]-1)
            now = A[now]-1

    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:
# -*- 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]
    if now_town in ans_list:
        loop_start_town = now_town
        break
    ans_list.append(now_town)
    before_town = now_town

loop_len = len(ans_list)
if loop_len >= K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(loop_start_town)
    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:
import sys 
N,K = map(int,input().split())
l = list(map(int,input().split()))
m = [0 for i in range(N)]
location = 0
count = 0
for i in range(K):
    location = l[location] -1
    m[location] += 1
    if m[location] == 2:
        start = location
        for j in range(K-i-1):
            location = l[location] -1
            count += 1
            if location == start:
                print(count)
                for k in range((K-i-j-2)//count):
                    location = l[location] -1
                print(location + 1)
                sys.exit()
        print(location + 1)
        sys.exit()
print(location + 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:
n,m=map(int,input().split())
tel=list(map(int,input().split()))
nex=0
for i in range(m-1):
    nex=tel[nex]-1
print(tel[nex])

Submission status: Time Limit Exceeded

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

flag = True
while flag:
    i = A[i-1]
    if i in B:
        flag = False
        I = A[i-1]
    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:
N,K=map(int,input().split())
*A,=map(int,input().split())

point=[0]*N
count_X=0
count_Y=0
i=0
while all(j<3 for j in point):
    if all(j<2 for j in point):
        count_X+=1
    point[i]+=1
    i=A[i]-1
    if any(k>1 for k in point):
        count_Y+=1
count_Y-=1

if K>=count_X:
    Z=count_X+(K-count_X)%count_Y
else:
    Z=K

i=0
ans=1
while i<Z:
    ans=A[ans-1]
    i+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

l = [1]
n = 1
while True:
    n = A[n-1]
    if n in l:
        mod = len(l) - l.index(n)
        init_len = l.index(n)
        break
    else:
        l.append(n)

if K < init_len:
    print(l[K])
else:
    rest = ((K-init_len) % mod)
    print(l[init_len+rest])

Submission status: Time Limit Exceeded

Solution code:
a,b=map(int,input().split())
l=list(map(int,input().split()))
val=1
ind=0
for i in range(b):
    val=l[ind]
    ind=l.index(l[val-1])
    #print(val,end=" ")
print(val)

Submission status: Time Limit Exceeded

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


#loop
c = 0
loc = 0
locs = [0]
ini = 0
while True:
    c += 1
    loc = a[loc] - 1
    
    if c == k:
        print(loc + 1)
        break
    if loc in locs:
        ini = locs.index(loc)
        break
    locs.append(loc)

if c != k:
    rest = (k - ini) % (c - ini)
    if rest == 0:
        print(locs[ini] + 1)
    else:
        d = 0
        while True:
            d += 1
            loc = a[loc] - 1
            if d == rest:
                print(loc + 1)
                break

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
l=[int(_) for _ in input().split()]

cycle=[1]
ctr=1
while True:
  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:
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])

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import itertools
import collections

def solve(N,K,A):
    ans = [1]
    i = 0
    while N != 0:
        if(A[i] in ans):
            if(K <= len(ans)):
                return ans[K]
            else:
                index = ans.index(A[i])
                K -= len(ans[:index])
                ans = ans[index:]
                break
        else:
            ans.append(A[i])
            i = A[i] - 1
            N -= 1
    return ans[K % len(ans)]
def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))

    # solve()
    print(solve(N,K,A))

if __name__ == "__main__":
    main()

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
num=len(l)-len(ll)
if k>num:
    k-=num
    k=k%len(ll)
    print(ll[k])
else:
    k=k%len(l)
    print(l[k])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n,k= list(map(int, input().split()))
li= list(map(int, input().split()))
c=[1]
nex=1
first=0
flag=0
for i in range(k):
    nex=li[nex-1]
    if nex in c:
        first=c.index(nex)
        flag=1
        break
    c.append(nex)
if flag:
    k-=first
    k%=len(c)-first
    print(c[first+k])
else:
    print(c[-1])

Submission status: Time Limit Exceeded

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

visit = [0] * N
turn = [-1] * N

visit[0] = 1
turn[0] = 0

def judge(v):
    for i, _v in enumerate(v):
        if _v > 1:
            return (True, i)
    return (False, 0)

cursor = 0
turn_count = 0
while True:
    cursor = A[cursor]
    turn_count += 1

    if turn_count == K:
        print(cursor+1)
        exit()

    visit[cursor] += 1

    flag, i = judge(visit)
    if flag:
        loop_length = turn_count - turn[i]
        break

    turn[cursor] = turn_count

K -= turn[i]
K %= loop_length

cursor = i
for _ in range(K):
    cursor = A[cursor]

print(cursor + 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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:
Town = map(int, input().split())
seq = map(int, input().split())
list1=[]
list2=[]
list3=[1]
for i in Town:
    list1.append(i)
for x in seq:
    list2.append(x)
for y in range(list1[1]):
    for z in list3:
        list3.append(list2[z-1])
print(list3[-1])

Submission status: Time Limit Exceeded

Solution code:
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.insert(0,0)
start = 1
now = start
c = 0
flag = [0]*(n+1)
while c <= k and flag[now] == 0:
    #print(now)
    flag[now] = 1
    c+= 1
    now = a[now]
#print(c, now)
if c == k:
    print(now)
else:
    k-=c
    moto = now
    now = a[now]
    cc = 1
    while now != moto:
        now = a[now]
        cc+=1
    nokori = k % cc
    for i in range(nokori):
        now = a[now]
    print(now)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
z = 0
b = [1]
for i in range(n*3):
  j = [a[b[i]-1]]
  b.extend(j)
for i in range(n):
  if b.count(i+1)>=3:
    z = z + 1
if k <= n:
  y = b[k]
else:
  l = (k-n)%z
  y = b[n+l]
print(y)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = [0] + A
stack = [A[1]]
while True:
    last = A[stack[-1]]
    if last in stack:
        ind = stack.index(last)
        if ind == stack[-1]:
            break
    stack.append(last)
    if len(stack) == K:
        print(stack[-1])
        exit(0)
mod = len(stack[ind:])
idx = ind + (K-ind-1)%mod
print(stack[idx])

Submission status: Time Limit Exceeded

Solution code:
N,K= map(int,input().split())
A=list(map(int,input().split()))
count=1
now=A[0]
while  now != 1 and count<K:
    now=A[now-1]
    count+=1
if now ==1:
    x=K%count
    for i in range(x):
        now=A[now-1]
print(now)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
li = list(map(int,input().split()))
move=[1]
now=1
while li[now-1] not in move:
    move.append(li[now-1])
    now=li[now-1]  
move.append(li[now-1])    
now=li[now-1]
start=move.index(now)
roope=move[start:-1]
move.pop(-1)
Q=len(move)-len(roope)
check=k-Q
if len(move)>=k:
    print(move[k])
else:
    print(roope[check%len(roope)])

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10**6)

root = list()


def dfs(A, num, N):
    if A[num] in root:
        return root.index(A[num])
    else:
        root.append(A[num])
        return dfs(A, A[num]-1, N)


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

    start = dfs(A, 0, N)

    if K <= len(root):
        print(root[K-1])
    else:
        index = (K - start) % (len(root)-start)
        loop = root[start:]
        print(loop[index-1])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
def check(n, x, A):
    count = 0
    p = x
    for i in range(n):
        count += 1
        p = A[p-1]
        if p == x:
            return count
    return -1

def main():
    n, k = map(int, input().split())
    A = list(map(int,input().split()))
    p = 1
    count = k
    while k > 0:
        k -= 1
        p = A[p-1]
        N = check(n,p,A)
        if N != -1:
            k = k % N
    print(p)
            
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def serch(cur):
    if A[cur] == A[0]:
        return 

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

serch(A[1])

i2p = {
    0: 1
}
cur = A[0]

i = 1
remove_list = []
while True:
    i2p[i] = cur
    remove_list.append(cur)
    if A[cur-1] in i2p.values():
        break
    cur = A[cur-1]
    i+=1

for i in i2p.keys():
    if A[cur-1] == i2p[i]:
        break


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 = list(map(int,input().split()))
T = 1
 
for i in range(K):
  T = A[T-1]
  
print(T)

Submission status: Time Limit Exceeded

Solution code:
from itertools import count

N, K = map(int, input().split())
As = [int(n) for n in input().split()]

visited = []
now = 0
num = 0
idx = 0
for i in count():
    if now in visited:
        idx = visited.index(now)
        num = len(visited) - idx
        break
    visited.append(now)
    now = As[now] - 1

if K <= idx:
    print(visited[K] + 1)
else:
    print(visited[idx + (K - idx) % num] + 1)

Submission status: Time Limit Exceeded

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

counter = 0
city = 1
tf = 0
list_city = [1]
citys = [0]*n

while tf == 0 and counter < k:
    temp = A[city-1]
    city = temp
    if citys[city-1]==1:
        tf = 1
    else:
        list_city.append(temp)
        citys[city-1]==1
        counter+=1

if tf == 1:
    print(list_city[list_city.index(city)+(k-list_city.index(city))%(counter-list_city.index(city)+1)])
else:
    print(city)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(m)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
mp = list(map(int,input().split()))
save = []

s_queue = [[1,0]]
while(len(s_queue)>0):
    i,cnt = s_queue.pop()
    if(cnt==k):
        break
    if(mp[i-1] in save):
        last=mp[i-1]
        break
    save.append(mp[i-1])
    s_queue.append([mp[i-1],cnt+1])


ls = len(save)

if(k<=ls):
    print(save[k-1])
else:
    for i in range(ls):
        if(save[i]==last):
            si = i
    k -= si
    save = save[si:]
    ls = len(save)
    amari = k%ls
    print(save[amari-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

L = [1]
tmp = 1
ct = 0
check = 0
while True:
    x = A[tmp - 1]
    ct += 1
    if x in L or ct > k:
        check = x
        break
    else:
        L.append(x)
        tmp = x
if ct > k:
    print(L[k])
    sys.exit()
p = L.index(check)
l = len(L)
roop = l - p
if k < p:
    print(L[k])
else:
    k -= p
    k %= roop
    print(L[p + k])

Submission status: Time Limit Exceeded

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

loop = 0
use = [0] * n
now = 0
use[now] = 1
x = [1]
for _ in range(n):
   next = a[now] - 1
   x.append(next+1)
   if use[next] == 0:
      use[next] = 1
      now = next
   else:
      break

y = []
while x.count(x[0]) == 1:
   tmp = x.pop(0)
   y.append(tmp)

x.pop(-1)

if k < len(y):
   kk = k % len(y)
   print(y[kk])
else:
   k -= len(y)
   kk = k % len(x)
   print(x[kk])

Submission status: Time Limit Exceeded

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

visited = [1]

for i in range(K):
    t = A[visited[-1] - 1]
    if t in visited:
        z = K - i - 1
        r = visited[visited.index(t):]
        l = len(r)
        print(r[z % l])
        exit()
    else:
        visited.append(t)

print(visited[-1])