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:
from collections import deque

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)

Submission status: Time Limit Exceeded

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

town=0
x=[]
for i in range(n):
    town=a[town]-1
    x.append(town+1)

s=[1]
for y in x:
    if y in s:
        last=y
        break
    s.append(y)
    
for i, t in enumerate(s):
    if t==last:
        index=i
  
if k<len(s):
    print(s[k])
else:
    kk=k-len(s)
    ss=s[index:]
    print(ss[kk%len(ss)])

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
numlist=[]
count=1
count2=0
num=0
for i in range(n):
    count2=a[count-1]
    if count2 in numlist =="True":
        break
    numlist.append(count2)
    count=count2
for i in range(len(numlist)):
    if numlist[i]==count2:
        num=i+1
        break
numlist2=numlist[num:]
if len(numlist)>k:
    print(numlist[k-1])
else:
    b=(k-num)%(len(numlist2))
    if b==0:
        print(numlist2[len(numlist2)-1])
    else:
        print(numlist2[b-1])

Submission status: Time Limit Exceeded

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

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

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

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

for idx, _ in enumerate(range(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] = route.index(current_node)
        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:
N, K = map(int, input().split())

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

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

while 1:
    L.append(A[f-1])
    memo.append(f)
    f = A[f-1]
    leng = len(memo)
    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())

a = [0]
a.extend(list(map(int, input().split())))
a1 = []
for i in range(0, len(a)):
    a1.append(0)
step = 1
t = 0
if k<=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 = list(map(int, input().split()))
a = list(map(int, input().split()))
idx=1
li=[]
idx_li=[1]
flag=False
for i in range(k):
    idx=a[idx-1]
    if idx in idx_li:
        flag=True
        break
    idx_li.append(idx)
if flag:
    non_loop=idx_li[:idx_li.index(idx)]
    loop = idx_li[idx_li.index(idx):]
    size=len(loop)
    print(loop[(k-len(non_loop))%size])
else:
    print(idx)

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

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

current = 1
for k in range(K):
    current = A[current - 1]

print(current)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split());
data = [input().split()];
a = 1;
c = int(1);
roop = list();
 
 
while a <= k:
    b = c - 1;
    c = int(data[0][b]);
    roop.append(c);
    if roop.count(c) == 2:
        break;
    a = a + 1;

x = len(roop);
y = roop.index(c) + 1;

if k <= x:
    print(c);
else:
    z = (k - y - 1) % (x - y);
    print(roop[y+z]);

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)
def next(x):
    return aa[x-1]
town=1
for i in range(k):
    town=next(town)
print(town)

Submission status: Time Limit Exceeded

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

nlist = []

h = 1
while not h in nlist:
    nlist.append(h)
#    print(h)
    h = A[h-1]

#print(nlist)
#print(h)
if len(nlist) > K:
    print(nlist[K])
else:
    x = nlist.index(h) 
    #print(x)
    K = K - x
    K = K % (len(nlist) - x)
    print(nlist[x + K])

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 = []
not_loop = False
for i in range(N):
    now = A[now - 1]
    if now in keiro:
        # flag = False
        start = keiro.index(now)
        loop_list = keiro[start:]
        loopN = len(loop_list)
        break
    else:
        keiro.append(now)
    if len(keiro) > K:
        not_loop = True
        break
if not_loop:
    print(keiro[K])
elif K - start >= 0:
    print(loop_list[(K - start) % loopN])
else:
    print(keiro[K])

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
count=0
lst=[]
now=1
for i in range(n):
    if now in set(lst):
        
        break
    lst.append(now)
    now=a[now-1]
ind=lst.index(now)

if k<=len(lst):
    print(lst[k])

else:
    newlst=lst[ind:]
    num=len(lst)-len(newlst)
    newk=k-num
    num2=newk%len(newlst)
    print(newlst[num2])

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=100000:
    state ,f = test, find_f[test]
    break
try:
  K = K-f[0]
  f = f[1]-f[0]
  remain = K%f

  for i in range(remain):
    state = move_list[state-1]

  print(state)
except:
  print(test)
#state = 1

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split(" "))
A=input().split(" ")
B=int(A[0])-1
for k in range(K-1):
    B=int(A[B])-1
print(B+1)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
town = 1
number = [1]
l = list(map(int, input().split()))
for i in range(k):
    town -= 1
    town = l[town]
    if town == l[town-1]:
        break
    elif town in number:
        hit = number.index(town)
        waru = i - hit + 1
        naka = (k - hit) % waru
        town = number[hit + naka]
        break
    number.append(town)
print(town)

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(1,K+1):
    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:
n, k  = map(int, input().split())
A = list(map(int, input().split()))

# リストを扱いやすいように変形
def subtraction(x):
    return x - 1
A = list(map(subtraction, A))
# print(A)

# 通ったことのある場所を持っておく
ROUTE = [0]
t = A[0]
while not (t in ROUTE):
    ROUTE.append(t)
    t = A[t]


x=ROUTE.index(t) # ループのスタート地点
length = len(ROUTE) - x
# print(ROUTE, length)

if k < x:
    print(ROUTE[k]+1)
else:
    k = k - x
    s = k % length
    print(ROUTE[x + s]+1)


# 再び町1に戻ってくるまでの経路を調べる
# ROUTE = [0]
# t = A[0] # 現在地
# while t != 0:
#     ROUTE.append(t)
#     t = A[t]
# # print(ROUTE)

# length = len(ROUTE)
# print(ROUTE[k % length]+1)

Submission status: Time Limit Exceeded

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

ans = deque()
ans.appendleft(1)
count = 1
s = -1

while True:
    if a[count-1] in ans:
        s = a[count-1]
        break
    else:
        ans.append(a[count-1])
        count = a[count-1]

ans = list(ans)
if s == -1:
    print(ans[k])
else:
    t = ans.index(s)
    if t >= k:
        print(ans[k])
    else:
        ans = ans[t:]
        print(ans[(k-t)%len(ans)])

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
temp = [""]*n
temp[0] = town

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

for i in range(n):
    if cycle[i] == "":
        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()))

#T = [0] * (n + 1)
T = [1]

for i in range(1, n + 1):
    if A[T[i - 1] - 1] in T:
        last = i
        break
    else:
        T.append(A[T[i - 1] - 1])

start = T.index(A[T[last - 1] - 1])

if k <= last:
    print(T[k])
else:
    print(T[start + (k - start) % (last - start)])

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

def move(A,now):
    return A[now-1]

N, K = map(int,input().split())
A = list(map(int,input().split()))
now = 1
dp  = [deepcopy([0 for _ in range(N)]) for _ in range(N)]
history = []
count = 0
while True:
    history.append(now)
    next_pos = move(A,now)
    if dp[now-1][next_pos-1] == 0:
        dp[now-1][next_pos-1] = 1
    else:
        remainder_array = history[history.index(next_pos):]
        print(remainder_array[(K - history.index(next_pos))%len(remainder_array)])
        break
    if count == K:
        print(now)
        break
    now = next_pos

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 = len(l[: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()))
current = 1

for _ in range(0,k):
    current = a[current - 1]

print(current)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
pos = 1-1
sum = 0
ago = [0]*n
p_cnt = 0
cnt_flg = False
for i in range(k):
    pos = a[pos]-1
    ago[pos]+=1
    max_num = max(ago)  
    if cnt_flg:
        p_cnt += 1
    if max_num == 3:
        cnt_flg = False
        sum+=1
        break
    if  max_num== 2:
        if not cnt_flg:
            start_pos = pos
        cnt_flg = True
        
    sum += 1
if sum == k:
    print(pos+1)
else:
    c = (k-sum)%p_cnt
    pos = start_pos
    for i in range(c):
        pos = a[pos]-1
    print(pos+1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:


def main():
    args = input().rstrip().split(' ')
    N, K = int(args[0]), int(args[1])
    d = {}
    args = input().rstrip().split(' ')
    for i in range(len(args)):
        d[i+1] = int(args[i])
    
    spot = 1
    for i in range(K):
        spot = d[spot]
    print(spot)
        
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

#行ったことある町リスト
d=[1]
i=0
for i 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:
N, K = map(int, input().split())
A = list(map(int, input().split()))

loop_checker = [1]
before_loop_len = 0
loop_len = 0

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

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
 
A = [int(x) for x in input().split()]
 
here = 1
towns = []
while(True):
  towns.append(here)
  next_town = A[here-1]
  here = next_town
  if next_town in towns:
    towns.append(next_town)
    break

start = towns.index(towns[-1])
    
if k <= start:
  print(towns[k])
else:
  idx = (k - start) % (len(towns)-1-start) + start
  print(towns[idx])

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)

Q=P[y:]

print(Q[c])

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]
#load = []
res = 0
flag = 0
for i in range(k-1):
    #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:
def resolve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    roop=0
    iti=0
    rirekin=0
    rireki=[]
    while roop==0:
        iti=a[iti]-1
        if iti in rireki:
            mae=rireki.index(iti)
            Mae=rireki[:mae]
            del rireki[:mae]
            roop=rirekin-mae
            break
        rireki.append(iti)
        rirekin+=1
    if k <= mae:
        print(Mae[k-1]+1)
    else:
        k-=mae
        print(rireki[(k%roop)-1]+1)
resolve()

Submission status: Time Limit Exceeded

Solution code:

import numpy as np
N, K = map(int, input().split())
dst = np.array(list(map(int, input().split())),dtype=int)

# N = 200000
# K = 10**18
# dst = np.array(list(range(2, N+1))+[1], dtype=int)

log = np.full(N, -1, dtype=int)
log[0] = 1
current = 1
for i in range(1, K+1):
    current = dst[current-1]
    if np.count_nonzero(log == current) != 0:
        loop_start_ind = np.where(log == current)[0][0]
        loop_len = i-loop_start_ind
        current = log[loop_start_ind+(K-i) % loop_len]
        break
    else:
        log[i] = current
print(current)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
List = list(map(int, input().split()))
List.insert(0, 0)
Jokyo = 0
count = 0
Now = 1
Loop = [1]
Loop2 = []
for i in range(3*N):
  Now = List[Now]
  Loop.append(Now)
Loop
for i in range(1, N+1):
  if Loop.count(i) == 1:
    Loop.remove(i)
    Jokyo += 1
  elif Loop.count(i) > 1 :
    count += 1

Calc = (K-Jokyo)%count

LoopList = []
for i in range(count):
  LoopList.append(Loop[i])

print(LoopList[Calc])

Submission status: Time Limit Exceeded

Solution code:
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
# 循環を調べる
tmp_city=[]
c=0
count=0
init_city=0

while (1):
    count+=1
    tmp=a[c]
    if k==count:
        tmp_city.append(tmp)
        tmp_city=[1]+tmp_city
        break
    if tmp == 1:
        init_city=0
        tmp_city=[1]+tmp_city
        break
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        break
    tmp_city.append(tmp)
    c=tmp-1
if k==count:
    print(tmp_city[-1])
else:
    print(tmp_city[init_city:][(k-(init_city))%len(tmp_city[init_city:])])

Submission status: Time Limit Exceeded

Solution code:
def func(x):
    return int(x)-1
N, K = list(map(int, input().split()))
A = list(map(func, input().split()))


now = 0
past = []

for i in range(N):
    if now in past:
        break
    past.append(now)
    now = A[now]



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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
dict={}
for i in range(n):
    dict[i]=a[i]-1 #i番目の町からどこにつくか
tmp=0#現在知
num2city={}
city2num={}
city2num[0]=0#どのまちが何回目か
num2city[0]=0#j回目にどこにいるか
for j in range(1,k+1):
    if tmp==dict[tmp]:
        break
    tmp=dict[tmp]#現在地更新
    if tmp not in num2city.values():#現在地がまだきたことない
        num2city[j]=tmp
        city2num[tmp]=j
    else:
        mae=city2num[tmp]
        term=j-mae 
        res=k-j
        r=res%term
  #      print(mae,term,res,r,mae+r,num2city[mae+r])
        tmp=num2city[mae+r]
 #       print(tmp)
        break
   # print(tmp+1)
    
#print(num2city)
#print(city2num)
print(tmp+1)

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

here = 1
log = [1]
i = K
while i > 0:
    if i >= 2:
        here = A[here - 1]
        log.append(here)
        here = A[here - 1]

        if here in log:
            loopuntil = log.index(here)
            mod = len(log[loopuntil:])
            i = (K - loopuntil) % mod
            log = []


        else:
            log.append(here)
            i = i - 2

    if i == 1:
        here = A[here - 1]
        i = 0

print(here)

Submission status: Time Limit Exceeded

Solution code:
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
idx=1
li=[]
idx_li=[1]
for i in range(k):
    idx=a[idx-1]
    if idx in idx_li:
        break
    idx_li.append(idx)
non_loop=idx_li[:idx_li.index(idx)]
loop = idx_li[idx_li.index(idx):]
size=len(loop)
print(loop[(k-len(non_loop))%size])

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
list = list(map(int, input().split()))
val = list[0]
startcnt = 1
roopList = []
roopList.append(1)
for x in range(n):
    if val in roopList: break
    roopList.append(val)
    val = list[val - 1]
    startcnt += 1
roopCount = len(roopList) - roopList.index(val)
if k <= startcnt:
    print(roopList[k])
else:
    roopList = roopList[roopList.index(val):]   
    print(roopList[(k - startcnt) % roopCount])

Submission status: Time Limit Exceeded

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

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

j=l.index(x)
d=len(l)
mod=(k-d)%(d-j)
for i in range(j):
    l.popleft()
print(l[mod])

Submission status: Time Limit Exceeded

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

num = K
memo = []
town = 1
if K > N:
    for _ in range(N):
        memo.append(town)
        if not A[town-1] in memo:
            town = A[town-1]
            num -= 1
        else:
            index = memo.index(A[town-1])
            l = len(memo) - index
            if num % l == 0:
                town = memo[-1]
            else:
                town = memo[index + (num % l) - 1]
            break
else:
    for _ in range(K):
        town = A[town - 1]

print(town)

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()]

i = 0 #現在地
m = 0
mem = [0]
while (m<k) and (a[i] != i) and (a[i] not in mem):
    mem.append(a[i])
    i = a[i]
    m += 1
else:
    if m == k:
        print(i+1)
    elif a[i] == i:
        print(a[i]+1)
    elif a[i] in mem:
        s = mem.index(a[i])
        r = len(mem[s:])
        print(mem[s+(k-m-1)%r]+1)
    else:
        print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


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


p=1
h=[]
go=0
for i in range(1,k+1):
    
    
    
    

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

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

print(p)

Submission status: Time Limit Exceeded

Solution code:
#D
n, k = map(int, input().split())
a = list(map(int, input().split()))
town = 1
for i in range(k):
    town = a[town-1]
print(town)

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()
kiseki = [1]
count = 0
now = A[0]
while now not in kiseki:
    kiseki.append(now)
    count += 1
    now = A[now - 1]
    if count == K:
        print(kiseki[-1])
        sys.exit()
ind = kiseki.index(now)
roop = len(kiseki) - ind
nokori = (K - ind) % roop
ans = kiseki[ind + nokori]
print(ans)

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 = []
not_loop = False
while flag:
    now = A[now - 1]
    if now in keiro:
        flag = False
        start = keiro.index(now)
        loop_list = keiro[start:]
        loopN = len(loop_list)
    else:
        keiro.append(now)
    if len(keiro) > K:
        flag = False
        not_loop = True
if not_loop:
    print(keiro[K])
elif K - start >= 0:
    print(loop_list[(K - start) % loopN])
else:
    print(keiro[K])

Submission status: Time Limit Exceeded

Solution code:
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
idx=1
li=[]
idx_li=[1]
for i in range(k):
    idx=a[idx-1]
    if idx in idx_li:
        break
    idx_li.append(idx)
non_loop=idx_li[:idx_li.index(idx)]
loop = idx_li[idx_li.index(idx):]
size=len(loop)
print(loop[(k-len(non_loop))%size])

Submission status: Time Limit Exceeded

Solution code:

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

while count < k:
	next_city = A[city - 1]
	city = next_city
	count += 1
	if city in already:
		already.append(city)
		break
	already.append(city)

if count == k:
	print(city)

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

Submission status: Time Limit Exceeded

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

#行ったことある町リスト
d=[1]
i=0
while i<K:
  T=A[d[-1]-1]
  if T in d:
    break
  d.append(T)
  i+=1
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:
N,K=map(int,input().split())
A=list(map(int,input().split()))
town=1
list=[]
while A[town-1] not in list:
    town=A[town-1]
    list.append(town)
list.append(A[list[len(list)-1]-1])
rlist=list[list.index(list[len(list)-1]):len(list)-1]
num=(K-list.index(list[len(list)-1]))%len(rlist)
if K-1<list.index(list[len(list)-1]):
    print(list[K-1])
else:
    if num==0:
        print(rlist[len(rlist)-1])
    else:
        print(rlist[num-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
A = list(map(int,input().split(" ")))
go = A[0]
B = [1]
while go not in B:
	B.append(go)
	go = A[go-1]
D = B.index(go)
saisho = B[0:D]
mawarutoko = B[D:]
C = (K-len(saisho)) % len(mawarutoko)
if  K > len(B):
	print(mawarutoko[C])
else:
	print(B[K])

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:
import sys
sys.setrecursionlimit(10**9)

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

log = []

def func(x, iter):
    x = A[x-1]

    if iter+1 == K:
        return x
    if x in log:
        ix = log.index(x)
        loop = log[ix:]
        tmp = (K-iter-1)%len(loop)
        return loop[tmp]
    else:
        log.append(x)
        iter += 1
        return func(x, iter)

print(func(1, 0))

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 len(a)<=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:
import sys

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

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

if K <= end:
    print(roop[K] + 1)
else:
    print(roop[(K - start) % (end - start + 1)  + start] + 1)