Problem:


Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Time Limit Exceeded

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

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

route = [1]
current_town = 1

append = route.append

for i in range(min(K,N)):
    current_town = A[current_town-1]
    if current_town in route:
        append(current_town)
        break
    else:
        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()))
v = []
now = 0
for i in range(K):
  p = A[now]
  now = p-1
  if p in v:
    j = v.index(p)
    df = i-j
    Kn = (K-i-1) % df
    p = v[j+Kn]
    break
  else:
  	v.append(p)
print(p)

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)

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

Submission status: Time Limit Exceeded

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

for s_point in range(N):
  point = s_point
  count = 0
  i = 0
  while i < N:
    point = T_list[point]
    if point == s_point:
      count = i+1
      break
    i += 1
  count_list.append(count)

point = 0
while count_list[point] == 0:
  point = T_list[point]
  count += 1

L = (K-count) % count_list[point]
new_point = 0
i = 0

while i < L:
  point = T_list[point]
  i += 1
print(point+1)

Submission status: Time Limit Exceeded

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


loop_find_list_ = []
loop_point = -1

dp = [0] * (2 * 10**5 + 1)
dp[1] = data[1 - 1] # 1番目埋め
loop_find_list_.append( dp[1] )

for k_ in range(2, k+1):
  dp[k_] = data[dp[k_ - 1] - 1]
  if dp[k_] in loop_find_list_:
    # print("検出したので終了します")
    loop_point = dp[k_]
    break
  else:
    loop_find_list_.append( dp[k_] )

if k <= len(loop_find_list_):
  print(loop_find_list_[k - 1])
else:
  idx = loop_find_list_.index(loop_point)
  loop_len_ = len(loop_find_list_) - idx

  real_loop_list_ = loop_find_list_[idx:]
  # 先頭部分の削除
  k = ( k - ( len(loop_find_list_)-len(real_loop_list_) ) ) % loop_len_
  if k == 0:
    k = len(real_loop_list_)
  print(real_loop_list_[k-1])

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
p = 0
a = 1
ret = [1]
init_l = list()
rec_l = list()
while p < K:
    a = A[a-1]
    if a in ret:
        ind = ret.index(a)
        init_l = ret[:ind]
        rec_l = ret[ind:]
        break
    ret.append(a)
    p += 1
if len(init_l) == 0 and len(rec_l) == 0:
    print(a)
else:
    m = K - len(init_l)
    print(rec_l[m % len(rec_l)])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

N, K  = [int(x) for x in stdin.readline().rstrip().split()]
A = [0] + [int(x) for x in stdin.readline().rstrip().split()]

stack = [1]
nextt = A[1]

while True:
    if not nextt in stack:
        stack.append(nextt)
    else:
        stack.append(nextt)
        break
    nextt = A[nextt]

    if len(stack) > K:
        print(stack[K])
        exit()

num1 = stack.pop()
length = len(stack)
tmp = []
while True:
    num2 = stack.pop()
    if num1 != num2:
        tmp.append(num2)
    else:
        tmp.append(num2)
        break

tmp = tmp[::-1]

index = (K - length) % len(tmp)

print(tmp[index])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1,a[0]]
tp = a[0]-1
for i in range(10**18):
  if a[tp] not in l: 
    l.append(a[tp])
    tp = a[tp] -1
  else:
    l.append(a[tp])
    break
    
last = l[-1]

loop = len(l[l.index(last):-1])
first = l.index(last)
if len(l)>k:
  print(l[k])
  exit()
k -=first
k %=loop
if k==0:
  k+=loop
print(l[k+first])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = []
b = 1
ans = 1
while ans:
  b = A[b-1]
  B.append(b)

Submission status: Time Limit Exceeded

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

route = [1]
current_town = 1

append = route.append

for i in range(K):
    current_town = A[current_town-1]
    if current_town in route:
        append(current_town)
        break
    else:
        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()))
 
res = [0]
tmp = 0
 
tf = [-1 for i in range(n)]
tf[0] = 0
 
for i in range(n):
    tmp = a[tmp] - 1
    if tf[tmp] != -1:
        start = tmp
        break
    else:
        res = res + [tmp]
        tf[tmp] = i+1
 
lenret = i+1
stridx = tf[start]
 
cycle = lenret - stridx
 
if stridx >= k:
    print(res[k]+1)
    exit()
else:
    k = k - stridx
 
    tmp = k % cycle
 
    print(res[stridx+tmp]+1)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
list1 = list(map(int, input().split()))
list2 = []
now = 0
shuuki = 0
k_init = k
hantei = 0
while k >= 1:
    if now not in list2:
        list2.append(now)
        now = list1[now] - 1
        k = k - 1
    else:
        shuuki = len(list2) - list2.index(now)
        hantei = 1
        break
if hantei == 0:
  print(now + 1)
else:
  k = (k_init - list2.index(now)) % shuuki
  print(list2[list2.index(now) + k] + 1)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

N, K  = [int(x) for x in stdin.readline().rstrip().split()]
A = [0] + [int(x) for x in stdin.readline().rstrip().split()]

stack = deque([1])
nextt = A[1]

while True:
    if not nextt in stack:
        stack.append(nextt)
    else:
        stack.append(nextt)
        break
    nextt = A[nextt]

    if len(stack) > K:
        print(stack[K])
        exit()

num1 = stack.pop()
length = len(stack)
tmp = deque([])
while True:
    num2 = stack.pop()
    if num1 != num2:
        tmp.append(num2)
    else:
        tmp.append(num2)
        break

tmp = list(tmp)[::-1]

index = (K - length) % len(tmp)

print(tmp[index])

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(solve(dests,k))




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


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

        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 = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
 
where_li = []
pre = 0
for i in range(N):
	where_li.append(A[pre])
	pre = A[pre]-1

aa = sorted([x for x in set(where_li) if where_li.count(x) > 1], key=where_li.index)
if(len(aa)==0):
	indexes = [0,0]
else:
	indexes = [i for i, x in enumerate(where_li) if x == aa[0]]
where_li = where_li[:indexes[1]]
len_w = len(where_li)
mod_s = len_w-indexes[0]
if K<=len_w:
	print(where_li[K-1])
else:
	if (K-len_w)%mod_s==0:
		d = mod_s
	else:
		d = (K-len_w)%mod_s
	print(where_li[d-1+indexes[0]])

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
    q_append = que.append
    c_append = checked_list.append

    while True:
        counter += 1
        x = que.popleft()
        if counter == k:
            print(x)
            exit()
        if x in set(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:
            q_append(a[x-1])
            c_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()))
def has_duplicates(seq):
    return len(seq) != len(set(seq))
load=[1]
Y=A[0]
c=0
while(has_duplicates(load)==False and c<=K):
    load.append(Y)
    Y=A[Y-1]
    c+=1
l_s=load.index(load[len(load)-1])
if(len(load)>=K+1):
    print(load[K])
else:
    x=l_s
    loop_n=K-x
    loop=load[l_s:len(load)-1]
    place=loop[loop_n%len(loop)]
    print(place)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
def has_duplicates(seq):
    return len(seq) != len(set(seq))
load=[1]
Y=A[0]
c=0
while(has_duplicates(load)==False,c<=K):
    load.append(Y)
    Y=A[Y-1]
    c+=1
l_s=load.index(load[len(load)-1])
if(len(load)>=K+1):
    print(load[K])
else:
    x=l_s
    loop_n=K-x
    loop=load[l_s:len(load)-1]
    place=loop[loop_n%len(loop)]
    print(place)

Submission status: Time Limit Exceeded

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

route = [1]
current_town = 1

append = route.append

for i in range(min(K, 2*(10**5))):
    current_town = A[current_town-1]
    if current_town in route:
        append(current_town)
        break
    else:
        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()))
already_town =[1]
now_town=1
check=False
my_append = already_town.append
for i in range(K):
    check_town=A[now_town-1]
    if(check_town in already_town):
        cicle_start_value=check_town
        check=True
        break
    my_append(check_town)
    now_town = check_town

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

Submission status: Time Limit Exceeded

Solution code:
N, K= map(int, input().split())
A = list(map(int, input().split()))
c = [x for x in set(A) if A.count(x) > 1]
cd = [1]
roop = [0] 

for i in range(len(A)):
    tmp = A[roop[i]] - 1
    if tmp+1 in c and tmp+1 not in cd:
        cd.append(tmp+1)
    elif tmp+1 in cd:
        start = roop.index(tmp)
        end = i
        break
    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:
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
n = 0
for i in range(K):
  n += 1
  present_location = routs[present_location]
  if present_location == 1:
    if n == 0 or K % n == 0:
      break
    for i in range(K % n):
      present_location = routs[present_location]
    break

print(present_location)

Submission status: Time Limit Exceeded

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

b = a[0]
i = 1
num = [1, a[0]]

while num.count(b) < 2:
  b = a[b-1]
  num.append(b)
  i += 1
if len(num) > k:
  ans = num[k]
else:
  t = num.index(b)
  x = (k - t) % (i - t)
  ans_list = num[t:i]
  ans = ans_list[x]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

N, K  = [int(x) for x in stdin.readline().rstrip().split()]
A = [0] + [int(x) for x in stdin.readline().rstrip().split()]

stack = deque([1])
nextt = A[1]

while True:
    if not nextt in stack:
        stack.append(nextt)
    else:
        stack.append(nextt)
        break
    nextt = A[nextt]

if len(stack) > K:
    print(stack[K])
    exit()
else:
    num1 = stack.pop()
    length = len(stack)
    tmp = deque([])
    while True:
        num2 = stack.pop()
        if num1 != num2:
            tmp.append(num2)
        else:
            tmp.append(num2)
            break

tmp = list(tmp)[::-1]

index = (K - length) % len(tmp)

print(tmp[index])

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**14:
    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:
N, K = map(int, input().split())
temp = list(map(int, input().split()))
warp = {}
for i in range(1,N+1):
    warp[i] = temp[i-1]

way = []
for k in range(1,K+1):
    if k == 1:
        way.append(warp[1])
    else:
        way.append(warp[way[k-2]])
print(way[-1])

Submission status: Time Limit Exceeded

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

for _ in range(k):
    number = lis[number-1]

print(number)

Submission status: Time Limit Exceeded

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

def calc(a, b):
    c = [0] * n
    for i in range(n):
        c[i] = b[a[i]]
    return c

while k:
    if k & 1 == 1:
        x = calc(x, a)
    k //= 2
    a = calc(a, a)

print(x[0]+1)

Submission status: Time Limit Exceeded

Solution code:

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

dist = {}
for idx, d in enumerate(A):
  dist[idx+1] = d

n = 1
hist = [1]
flag = True
for i in range(K):
  n = dist[n]
  if n in hist:
    flag = False
    break
  hist.append(n)
if flag:
  print(n)
else:
  start = hist.index(n)
  loop = hist[hist.index(n):]
  print(loop[(K - start) % len(loop)])

Submission status: Time Limit Exceeded

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

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 = int((k - ini) % (c - ini))
    if rest <= 0:
        print(loc + 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:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

N, K  = [int(x) for x in stdin.readline().rstrip().split()]
A = [0] + [int(x) for x in stdin.readline().rstrip().split()]

stack = deque([1])
nextt = A[1]

while True:
    if not nextt in stack:
        stack.append(nextt)
    else:
        stack.append(nextt)
        break
    nextt = A[nextt]

if len(stack) > K:
    print(stack[K])
    exit()
else:
    num1 = stack.pop()
    length = len(stack)
    tmp = deque([])
    while True:
        num2 = stack.pop()
        if num1 != num2:
            tmp.appendleft(num2)
        else:
            tmp.appendleft(num2)
            break

index = (K - length) % len(tmp)

print(tmp[index])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1,a[0]]
tp = a[0]-1
while True:  
  if a[tp] not in l: 
    l.append(a[tp])
    tp = a[tp] -1
  else:
    l.append(a[tp])
    break
    
last = l[-1]

loop = len(l[l.index(last):-1])
first = l.index(last)
if len(l)>k:
  print(l[k])
  exit()
k -=first
k %=loop
if k==0:
  k+=loop
print(l[k+first])

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    NK=input().split(" ")
    N=int(NK[0])
    K=int(NK[1])
    res=1
    n=0
    tem=[1]
    temp=1
    A=list(map(int,input().split(" ")))
    dic={}
    for i in range(0,N):
        dic[i+1]=A[i]
    for i in range(N):
        res=dic[res]
        n+=1
        if (res in tem):
            temp=res
            break
        tem.append(res)
    if K<=N:
        res=1
        for i in range(K):
            res=dic[res]
        print(res)
    else:
        m=0
        tem=[res]
        for i in range(N):
            res=dic[res]
            m+=1
            if (res in tem):
                temp=res
                break
            tem.append(res)
        # if n==N:
        #     res=dic[K%N]
        # else:
        for i in range(0, (K-n)%m):
            res=dic[res]
        print(res)

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()
    else:
        P.append(now)

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

print(P[y+c])

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
    q_append = que.append
    c_append = checked_list.append

    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:
            q_append(a[x-1])
            c_append(x)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,k = map(int, input().split())
    town = list(map(int, input().split()))
    set_t = set()
    list_t = []
    current = 1
    is_loop = False
    counter = {}
    for i in range(k):
        current = town[current-1]
        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()))
n = 0
for i in range(k):
  n = a[n]-1
print(n+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()]
 
v=1
l=[0 for i in range(n)]
for i in range(k):
    v=a[v-1]
    if v in l:
        index=l.index(v)
        l2=l[index:i]
        v=l2[(k-index)%len(l2)-1]
        break
    else:
        l[i]=v
print(v)

Submission status: Time Limit Exceeded

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

def get_idx(lst, value):
    return [i for i, x in enumerate(lst) if x == value]


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

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

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

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

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

print(route[loop_index[0]:loop_index[1]+1][ans_idx])

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
    q_append = que.append

    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:
            q_append(a[x-1])
            checked_list.append(x)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
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]
h_append = hist.append

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

# print(count)
# print(hist)
ans = (K - prefix) % cycle_len
print(hist[prefix+ans])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

c=0
p=1
s = set()
Q = 0
while True:
  s.add(p)
  if(Q + 1 == len(s)):
    Q = Q + 1
  else:
    K = K % Q
  p = L[p-1]
  c = c+1
  if(c==K):
    print(p)
    sys.exit()

Submission status: Time Limit Exceeded

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

loc = 1
loclog=[1]
flag = 0

for i in range(K):
    loc = A[loc-1]
    if loc in loclog:
        roopstart = loclog.index(loc)
        roopend = len(loclog)
        flag = 1
        break
    loclog.append(loc)

if flag == 0:
    ans = loc
else:
    roopidx = (K+1-roopstart)%(roopend-roopstart)
    if roopidx == 0:
        ans = loclog[roopend-1]
    else:
        ans = loclog[roopstart+roopidx-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
N, K = map(int, input().split())
A = list(map(int, input().split()))
if K == 1:
    print(A[0])
    sys.exit()
tel = dict()
for i in range(N):
    tel[i + 1] = A[i]
kiseki = [1]
count = 0
now = A[0]
while now not in kiseki:
    kiseki.append(now)
    count += 1
    now = tel[now]
    if count == K:
        print(kiseki[-1])
        sys.exit()
ind = kiseki.index(now)
roop = count + 1 - ind
nokori = (K - ind) % roop
ans = kiseki[ind + nokori]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
 
h = []
i = 0
s = 1
 
ans = 0
while i < k: 
  g = a[s-1]
  if g not in h:
    h.append(g)
    s = g
    i+=1
  else:
    c = h.index(g)
    l = (k-1-c) % (i-c)
    ans = h[c+l]
    break
    
if ans == 0:
  ans = h[-1]
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
def has_duplicates(seq):
    return len(seq) != len(set(seq))
load=[1]
Y=A[0]
while(has_duplicates(load)==False):
    load.append(Y)
    Y=A[Y-1]
l_s=load.index(load[len(load)-1])

if(len(load)>=K+1):
    print(load[K])
else:
    x=l_s
    loop_n=K-x
    loop=load[l_s:len(load)-1]
    place=loop[loop_n%len(loop)]
    print(place)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
array = list(map(int,input().split()))
ans = 1
tmp = [1]
flag = 0
count = 0
for i in range(k):
    ans = array[ans-1]
    count = count+1
    if ans in tmp and flag==1:
        p = tmp.index(ans)
        tmp = tmp[p:]
        k = k-count
        break
    elif flag ==0:
        flag =1
        tmp.append(ans)
    else:
        tmp.append(ans)
        pass
# tmp2 = tmp[-1]
# tmp.insert(0,tmp2)
# tmp.pop(-1)
# print(tmp)
ans = k%len(tmp)
print(tmp[ans])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import collections
import sys

sys.setrecursionlimit(2*(10**5)+1)

class Args(object):
    def __init__(self, N):
        self.num_of_transition = 0
        self.path = []
        self.count_visit_node_i = [0] * N

def solve(N, K, A):
    args = Args(N)

    def walk(p):
        if max(args.count_visit_node_i) > 2:
            return True
        args.num_of_transition += 1
        args.path.append(p)
        args.count_visit_node_i[p] += 1
        walk(A[p]-1)

    if walk(0):
        pass
#    print(args.num_of_transition)
#    print(args.path)
#    print(args.count_visit_node_i)

    freq = collections.Counter(args.count_visit_node_i)
#    print(freq)
    const_step = 0
    if 1 in freq.keys():
        const_step += freq[1]

    roop_step = 0
    if 2 in freq.keys():
        roop_step += freq[2]
    if 3 in freq.keys():
        roop_step += freq[3]

    if K < const_step:
        ans_step = K
    else:
        ans_step = (K-const_step) % roop_step + const_step
#    print(const_step, roop_step, ans_step)

    ans_args = Args(N)

    def walk_ans(p):
        if ans_args.num_of_transition > ans_step:
            return True
        ans_args.num_of_transition += 1
        ans_args.path.append(p)
        walk_ans(A[p]-1)

    if walk_ans(0):
        pass
#    print(ans_args.num_of_transition)
#    print(ans_args.path)
#    print(ans_args.path[-1])
    print(ans_args.path[-1]+1)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

a=list()
a=input().split()
i=0
while k>0:
  k=k-1
  rs=a[i]
  i=rs
  i=int(i)-1
  
print(rs)

Submission status: Time Limit Exceeded

Solution code:
N,K=input().split()
N=int(N)
K=int(K)
A=list(map(int, input().split()))
i=1
a=[1]
d=A[0]
if (2<=N<=2*10**5)and(1<=K<=10**18):
    a.append(A[0])
    while i<=N:
        d=A[d-1]
        if d in a:
            i+=1
            break
        a.append(d)
        i+=1
    e=a.index(d)
    f=len(a)
    g=f-e
    if K<=e:
        print(a[K])
    if K>e:
        K=K-e
        h=K%g
        h=e+h
        print(a[h])

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
my_append = already_town.append
for i in range(K):
    if(A[now_town-1] in already_town):
        cicle_start_value=A[now_town-1]
        check=True
        break
    my_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)])