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: Runtime Error

Solution code:
import sys
N,K=map(int,(input().split()))
A=list(map(int,input().split()))
A.insert(0,0)
loc=1
log=[0]
loop_flg=0
i=1
while(i<=K):
  if(loc not in log):
    # print("ログに"+str(loc)+"を追加します")
    log.append(loc)
    # print(log)
    loc=A[loc]
    i+=1
  elif(loc in log and loop_flg==0):
    # print("ループを"+str(log.index(loc))+"番目から"+str(i-1)+"番目の間に発見しました")
    loop_longth=i-log.index(loc)
    K = i+((K-(log.index(loc)-1))%loop_longth)
    # print(K)
    # print(log.index(loc)-1)
    loc=log[(log.index(loc)-1)+((K-log.index(loc))%loop_longth)]
    print(log[loc])
    sys.exit()
  print(str(loc)+" "+str(i)+" "+str(K))
print(loc)

Submission status: Runtime Error

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

now = 0
a = [_-1 for _ in list(map(int,input().split()))]

cnt = 0
for i in range(k):
    now = a[now]
    if now == 0:
      cnt = i+1
      break
  
now = 0
for i in range(k % cnt):
  now = a[now]

print(now+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ittamachi =[1] + [0] * (n - 1)
zyunban = [1]
now = 1
for i in range(n):
         now = a[now - 1]
         if ittamachi[now - 1] == 1:
                  break
         else:
                  ittamachi[now - 1] = 1
         zyunban.append(now)
for i in range(len(zyunban)):
         if zyunban[i] == now:
                  roopmae = zyunban[:i]
                  roopato = zyunban[i:]
                  break
if k <= len(roopmae):
         print(roopmae[k])
else:
         print(roopato[((k - len(roopmae)) % len(roopato))])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ittamachi =[1] + [0] * (n - 1)
zyunban = [1]
now = 1
for i in range(n):
         now = a[now - 1]
         if ittamachi[now - 1] == 1:
                  break
         else:
                  ittamachi[now - 1] = 1
         zyunban.append(now)
for i in range(n):
         if zyunban[i] == now:
                  roopmae = zyunban[:i]
                  roopato = zyunban[i:]
                  break
if k <= len(roopmae):
         print(roopmae[k])
else:
         print(roopato[((k - len(roopmae)) % len(roopato))])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
ittamachi =[1] + [0] * (n - 1)
zyunban = [1]
now = 1
for i in range(n):
         now = a[now - 1]
         if ittamachi[now - 1] == 1:
                  break
         else:
                  ittamachi[now - 1] = 1
         zyunban.append(now)
for i in range(n):
         if zyunban[i] == now:
                  roopmae = zyunban[:i]
                  roopato = zyunban[i:]
                  break
print(roopmae,roopato)
if k <= len(roopmae):
         print(roopmae[k])
else:
         print(roopato[((k - len(roopmae)) % len(roopato))])

Submission status: Runtime Error

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

seen = [0] * (N+1)
cnt = 0
ans = 0

def tlp(start, limit):
  global cnt
  global seen
  if seen[start] != 0:
    init = seen[start] - 1
    return ['looped', start, init, cnt-init]
  else:
    cnt += 1
    seen[start] = cnt
    next = A[start]
    if cnt == limit:
      return ['unlooped', next]
    else:
      return tlp(next, limit)

judge = tlp(1, K)
seen = [0] * (N+1)
cnt = 0
#print(judge)
if judge[0] == 'unlooped':
  print(judge[1])
else:
  step = (K-judge[2]) % judge[3]
  #print(step)
  if step == 0:
    print(judge[1])
  else:
    ans = tlp(judge[1], step)
    print(judge[1])

Submission status: Runtime Error

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

D = 60
MAX_N = 200005
to[D][MAX_N]

for i in range(n):
    to[0][i] = int(input())
    to[0][i] -= 1

for i in range(i, D-1):
    for j in range(n):
        to[i+1][j] = to[i][to[i][j]]

v = 0
for i in range(D-1,0):
    l = 1<<i
    if (l <= k):
        v = to[i][v]
        k -= l

print(v+1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
pos = 1
z = [0]
j = 0
y = []
for x in range(K):
    pos = A[pos - 1]
    if (pos - 1) in z:
        if j > 0:
            if val == pos - 1:
                break
        if j == 0:
            val = pos - 1
            j += 1
        y.append(pos - 1)
    z.append(pos - 1)
m = 0
for x in z:
    m += 1
    if x == y[0]:
        break
K -= m
pos = K % len(y)
pos = y[pos]
print(A[pos])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
pos = 1
z = [0]
j = 0
y = []
for x in range(K):
    pos = A[pos - 1]
    if (pos - 1) in z:
        if j > 0:
            if val == pos - 1:
                break
        if j == 0:
            val = pos - 1
            j += 1
        y.append(pos - 1)
    z.append(pos - 1)
m = 0
for x in z:
    if x == y[0]:
        break
    m += 1
K -= m
pos = K % len(y)
pos = y[pos]
print(A[pos])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
pos = 1
z = [0]
j = 0
y = []
for x in range(K):
    pos = A[pos - 1]
    if (pos - 1) in z:
        if j > 0:
            if val == pos - 1:
                break
        if j == 0:
            val = pos - 1
            j += 1
        y.append(pos - 1)
    z.append(pos - 1)
m = 0
for x in z:
    if x == y[0]:
        break
    m += 1
K -= m
pos = K / len(y)
pos = y[pos]
print(A[pos])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
pos = 1
z = [0]
j = 0
y = []
for x in range(K):
    pos = A[pos - 1]
    if (pos - 1) in z:
        if j > 0:
            if val == pos - 1:
                break
        if j == 0:
            val = pos - 1
            j += 1
        y.append(pos - 1)
    z.append(pos - 1)
pos = K % len(y)
print(A[pos])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a_preorder=[1]
a_first=[]
a_cycle=[]
for i in range(n):
    next_num=a[a_preorder[i]-1]
    if(next_num in a_preorder):
        a_preorder.append(next_num)
        index=a_order.index(next_num)
        a_first=a_preorder[0:index+1]
        a_order.append(a_preorder[i])
        a_cycle=a_preorder[index+1:i+2]
        break
    else:
        a_preorder.append(next_num)
if(len(a_cycle)==0):
    a_cycle=a_first
a_first_len=len(a_first)
a_cycle_len=len(a_cycle)
if(k<=a_first_len):
    print(a_first[k])
else:
    restcycle=k-a_first_len
    num=restcycle%a_cycle_len
    print(a_cycle[num])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.buffer.readline
N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
now = 0
dict_ = {}
stack = []
for i in range(N):
    if now not in dict_:
        dict_[now] = A[now]
        stack.append(now)
        now = A[now]
    else:
        idx = stack.index(now)
        mod = i+1 - idx
        break
idx = idx + (K-idx) % mod
idx = min(K, idx)
print(stack[idx]+1)

Submission status: Runtime Error

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

cnt = [0]*(2*10**5)#何回出たか
cnt2 = [0]*(2*10**5)#何回目に出てきたか
t = []
nxt = 0
loop = 0

for i in range(10**5):
    cnt[nxt] += 1
    if cnt[nxt] >= 2:
        # print("i: " + str(i))
        # print("nxt: " + str(nxt))
        loop = i - cnt2[nxt]
        modC = cnt2[nxt]
        break
    cnt2[nxt] = i
    t.append(nxt)
    nxt = a[nxt]

modK = (k-modC)%loop
# print("-------")
# print(t)
# print("modC: " + str(modC))
# print("modK: " + str(modK))
# print("loop: " + str(loop))
# print(modK)
# print(loop)
# print("-------")

if k < modC:
    print(t[k] + 1)
else:
    print(t[modC + modK] + 1)

"""
発想はあってる
テストケース公開されたら試してみる！
"""

Submission status: Runtime Error

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

cnt = [0]*(10**5)#何回出たか
cnt2 = [0]*(10**5)#何回目に出てきたか
t = []
nxt = 0
loop = 0

for i in range(10**5):
    cnt[nxt] += 1
    if cnt[nxt] >= 2:
        # print("i: " + str(i))
        # print("nxt: " + str(nxt))
        loop = i - cnt2[nxt]
        modC = cnt2[nxt]
        break
    cnt2[nxt] = i
    t.append(nxt)
    nxt = a[nxt]

modK = (k-modC)%loop
# print("-------")
# print(t)
# print("modC: " + str(modC))
# print("modK: " + str(modK))
# print("loop: " + str(loop))
# print(modK)
# print(loop)
# print("-------")

if k < modC:
    print(t[k] + 1)
else:
    if modC + modK < len(t):
        print(t[modC + modK] + 1)
    else:
        print(-1)

"""
発想はあってる
テストケース公開されたら試してみる！
"""

Submission status: Runtime Error

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

cnt = [0]*(10**5)#何回出たか
cnt2 = [0]*(10**5)#何回目に出てきたか
t = []
nxt = 0
loop = 0

for i in range(10**5):
    cnt[nxt] += 1
    if cnt[nxt] >= 2:
        # print("i: " + str(i))
        # print("nxt: " + str(nxt))
        loop = i - cnt2[nxt]
        modC = cnt2[nxt]
        break
    cnt2[nxt] = i
    t.append(nxt)
    nxt = a[nxt]

modK = (k-modC)%loop
# print("-------")
# print(t)
# print("modC: " + str(modC))
# print("modK: " + str(modK))
# print("loop: " + str(loop))
# print(modK)
# print(loop)
# print("-------")

if k < modC:
    print(t[k] + 1)
else:
    print(t[modC + modK] + 1)

"""
発想はあってる
テストケース公開されたら試してみる！
"""

Submission status: Runtime Error

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

N, K=map(int, input().split())
A = list(map(int,input().split()))
now=0
rireki=[0]*N
for i in range(K):
    if rireki[now]==0:
        rireki[now]=1
    else:
        loop=i+1
        break

#    print("{}".format(now))
    now=A[now]-1


amari=K%loop
for i in range(amari):
    now=A[now]-1

print("{}".format(A[now]))

Submission status: Runtime Error

Solution code:
l = [int(x) for x in input().split()]
N, K = l[0], l[1]
A_list = [int(x) for x in input().split()]
place_list = []
next_place = 1

for i in range(K):
    if next_place in place_list:
        break
        
    else:
        place_list.append(next_place)
        next_place = A_list[next_place - 1]

loop_list = place_list[place_list.index(next_place):]

if len(place_list) == K:
    print(place_list(K-1))
else:
    print(loop_list[(K-len(place_list)) % len(loop_list)])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
t=list(map(int,input().split()))
g=[1]
gs=set(g)
for i in range(N-1):
  if t[g[i]-1] in gs:
    break
  g.append(t[g[i]-1])
  gs.add(t[g[i]-1])
if K>len(g):
  a=g.index(t[g[-1]-1])
  b=len(g)-a
  K=(K-a)%b+a
print(g[K])

Submission status: Runtime Error

Solution code:
#https://zoom.us/j/96904363338?pwd=dVhyazRpV29MeWVEMGJRNi9ycmxmZz09
N,M=map(int,input().split())
A=[int(x) for x in input().split()]


loop_all=[1]
num=0
for index,i in enumerate(A,start=0):
    if i not in loop_all:
        loop_all.append(A[num])
        num=A[num]-1

loop_1=[]
tmp1=A[A[len(loop_all)-1]-1]
tmp2=loop_all.index(tmp1)

num1=0
for i in loop_all:
    if num1==tmp2:
        break
    loop_1.append(i)
    num1+=1


loop_2=[]
for i in loop_all[tmp2:]:
    loop_2.append(i)

if M<=len(loop_1)-1:
    print(A[K])
else:
    print(loop_2[ (M-len(loop_1)-1)%len(loop_2)-2])

Submission status: Runtime Error

Solution code:
def main():
    n, k = [int(x) for x in raw_input().split()]
    a = [int(x) for x in raw_input().split()]
    visited = [False] * n
    ans = 1
    visited[ans-1] = True
    m = 0
    for x in xrange (k):
        ans = a[ans-1]
        if visited[ans-1]:
            break
        else:
            visited[ans-1] = True
        m += 1
    for x in xrange (k % m + 1):
        ans = a[ans-1]
    print ans
main()

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
#"tel"は既に訪れている町
tel = set()
route = []
#"town"は今いる町
town = 1
route.append(town)
tel.add(town)
time_loop = 0
for i in range(k):
    town = a[town-1]
    if town in tel:
        time_loop += 1
        break
    tel.add(town)
    route.append(town)
    #テレポートした回数をカウント
    time_loop += 1

#"start"は"route"内のループの起点のとなる町のインデックス番号
start = route.index(town)
k = (k - start) % (time_loop - start)
print(route[k+start])

Submission status: Runtime Error

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

s="0"
t=1
flag1=0
flag2=0
for i in range(1,n+1):
    if a[t] not in s:
        s+=a[t]
        t=int(a[t])
    else:
        flag1=s.find(a[t])
        flag2=i
        break

if flag1==0:print(s[(k-1)%n+1])
else:
    if k<=flag2-1-1+1:print(s[k])
    else:
        s='0'+s[flag1:flag2]  #上下保持一致，左边加上0
        #print(s)
        print(s[(k-(flag1-1))%(flag2-1-flag1+1)])  #去除非循环节部分后求余

Submission status: Runtime Error

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

s="0"
t=1
flag1=0
flag2=0
for i in range(1,n+1):
    if a[t] not in s:
        s+=a[t]
        t=int(a[t])
    else:
        flag1=s.find(a[t])
        flag2=i
        break

if flag1==0:print(s[(k-1)%n+1])
else:
    if k<=n:print(s[k])
    else:
        s='0'+s[flag1:flag2]  #上下保持一致，左边加上0
        #print(s)
        print(s[(k-(flag1-1))%(flag2-1-flag1+1)])  #去除非循环节部分后求余

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

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

cnt=0
now=1
cyc=0
visited=[0]*n
flg=False
for i in range(k):
    cnt+=1
    now=a[now-1]
    visited[now-1]+=1
    if visited[now-1]==1:
        cyc+=1
        #print(cyc)
    if visited[now-1]==2:
        flg=True
        cyc-=1
        break
    #print(visited)
#print(cyc,cnt,now,k-cnt+cyc)
if flg:
    for i in range((k-cnt+cyc)%cyc):
        now=a[now-1]
    print(now)
else:
    print(now)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
n , k = map(int , input().split())
arr = list(map(int , input().split()))
dic = defaultdict(int)
for i in range(1,n+1):
	dic[i] = arr[i-1]
cycle = []
visited = [False]*(10**6)
curr = 1
if k <= n:
	curr = 1
	count = 0
	while count != k:
		curr = dic[count]
		count+=1
	exit(print(curr))
while True:
	cycle.append(curr)
	visited[curr] = True
	curr = dic[curr]
	#k -= 1
	if visited[curr]:
		break

csize = 0
ind = 0
for i in range(len(cycle)-1,-1,-1):
	if cycle[i] == curr:
		ind = i + 1
		break
	csize += 1
'''
offset = len(cycle) - csize - 1
#print(cycle,csize+1, k , curr,offset)
k -= offset
k %= csize + 1
print(cycle[offset + k])
'''
print(cycle[ind + ((k-ind)%csize)])

Submission status: Runtime Error

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

A=list(map(int,input().split()))
TP=[1]
now=1
next=1
for k in range(6):
    next=A[now-1]
    TP.append(next)
    now=next
#6回未満なら、法則性考える必要なし
#print(TP)
if K<=N:
    print(TP[K])
#6回以上⇒パターン考慮
else :
    Lstart=0
    for i in range(1,N+1):
        if TP.count(i) > 1:
            Lstart=i
            break
    startnum=0
    nextnum=0
    count=0
    for i in range(0,N+1):
        if TP[i]==Lstart:
            if count==0:
                startnum=i
                count=1
            elif count==1:
                nextnum=i
                break
    Lstep=nextnum-startnum
    #print(K,startnum,nextnum,Lstep)
    where=(K-startnum)%Lstep
    print(TP[where+startnum])

Submission status: Runtime Error

Solution code:
dic = {}
for key, a in zip(range(1, n+1), list(map(int,input().split()))):
    dic[key] = a
 
keynum = 0
log = [-1]*(n+1)
next = dic[1]
step = 1
log[next] = step
k -= 1
while k:
    next = dic[next]
    step += 1
    k -= 1
    if log[next] != -1:
        k %= log[next] - step
    else:
        log[next] = step
print(next)

Submission status: Runtime Error

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

A=list(map(int,input().split()))
TP=[1]

now=1
next=1
for k in range(6):
    next=A[now-1]
    TP.append(next)
    now=next
#6回未満なら、法則性考える必要なし
if K<=6:
    print(TP[K])
#6回以上⇒パターン考慮
else :
    Lstart=0
    for i in range(1,7):
        if TP.count(i) > 1:
            Lstart=i
            break
    startnum=0
    nextnum=0
    count=0
    for i in range(0,7):
        if TP[i]==Lstart:
            if count==0:
                startnum=i
                count=1
            elif count==1:
                nextnum=i
                break
    Lstep=nextnum-startnum
    #print(startnum,nextnum,Lstep)
    where=(K-startnum)%Lstep
    print(TP[where+Lstart])

Submission status: Runtime Error

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

A=list(map(int,input().split()))
TP=[1]

now=1
next=1
for k in range(7):
    next=A[now-1]
    TP.append(next)
    now=next
#6回未満なら、法則性考える必要なし
if K<=6:
    print(TP[K])
#6回以上⇒パターン考慮
else :
    Lstart=0
    for i in range(1,7):
        if TP.count(i) > 1:
            Lstart=i
            break
    startnum=0
    nextnum=0
    count=0
    for i in range(0,7):
        if TP[i]==Lstart:
            if count==0:
                startnum=i
                count=1
            elif count==1:
                nextnum=i
                break
    Lstep=nextnum-startnum
    #print(startnum,nextnum,Lstep)
    where=(K-startnum)%Lstep
    print(TP[where+Lstart])

Submission status: Runtime Error

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

A=list(map(int,input().split()))
TP=[1]

now=int(1)
next=int(1)
for k in range(7):
    next=A[now-1]
    TP.append(next)
    now=next
#6回未満なら、法則性考える必要なし
if K<=7:
    print(TP[K])
#6回以上⇒パターン考慮
else :
    Lstart=0
    for i in range(1,7):
        if TP.count(i) > 1:
            Lstart=i
            break
    startnum=0
    nextnum=0
    count=0
    for i in range(0,7):
        if TP[i]==Lstart:
            if count==0:
                startnum=i
                count=1
            elif count==1:
                nextnum=i
                break
    Lstep=nextnum-startnum
    #print(startnum,nextnum,Lstep)
    where=(K-startnum)%Lstep
    print(TP[where+Lstart])

Submission status: Runtime Error

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

A=list(map(int,input().split()))
TP=[1]

now=int(1)
next=int(1)
for k in range(7):
    next=A[now-1]
    TP.append(next)
    now=next
#6回未満なら、法則性考える必要なし
if K<=7:
    print(TP[K])
#6回以上⇒パターン考慮
else :
    Lstart=0
    for i in range(1,7):
        if TP.count(i) > 1:
            Lstart=i
            break
    startnum=0
    nextnum=0
    count=0
    for i in range(0,7):
        if TP[i]==Lstart:
            if count==0:
                startnum=i
                count=1
            elif count==1:
                nextnum=i
                break
    Lstep=nextnum-startnum
    print(startnum,nextnum,Lstep)
    where=(K-startnum)%Lstep
    print(TP[where+Lstart])

Submission status: Runtime Error

Solution code:
now=int(1)
next=int(1)
for k in range(7):
    next=A[now-1]
    TP.append(next)
    now=next
#6回未満なら、法則性考える必要なし
if K<=7:
    print(TP[K])
#6回以上⇒パターン考慮
else :
    Lstart=0
    for i in range(1,7):
        if TP.count(i) > 1:
            Lstart=i
            break
    startnum=0
    nextnum=0
    count=0
    for i in range(0,7):
        if TP[i]==Lstart:
            if count==0:
                startnum=i
                count=1
            elif count==1:
                nextnum=i
                break
    Lstep=nextnum-startnum
    #print(startnum,nextnum,Lstep)
    where=(K-startnum)%Lstep
    print(TP[where+Lstart])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
n , k = map(int , input().split())
arr = list(map(int , input().split()))
dic = defaultdict(int)
for i in range(1,n+1):
	dic[i] = arr[i-1]
cycle = []
def dfs(node):
	if node in cycle:
		return node
	else:
		cycle.append(node)
		k = dfs(dic[node])
	return k
cyele = dfs(1)
ind = 0
offset = 0
for i in range(len(cycle)):
	if cycle[i] == cyele:
		ind = i
		break
	offset+=1
#rem = k - offset
csize = len(cycle[ind:])
rem=rem%csize

count = 0
curr = 1
while count != rem:
	curr = dic[curr]
	count+=1
print(curr)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
dic = {}
a = l[0]
for i in range(n):
    dic[i] = ["false",0]
dic[0] = ["true",0]

if a ==1:
    print("1")
    sys.exit()
    
for j in range(n):
    if dic[a-1][0]=="true":
        loop = j+1-dic[a-1][1]
        loopstart = j-loop+1 #何回目
        start = a #何から
        break
    
    dic[a-1]=["true",j+1]
    a = l[a-1]
    
if k < loopstart:
    a = 1
    for i in range (k):
        a = l[a-1]
    print(a)
    sys.exit()
        
    
else:
    amari =(k-loopstart)%loop
    if amari ==0:
        print(start)
    else:
        a = start
        for i in range(amari):
            a = l[a-1]
        print(a)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
dic = {}
a = l[0]
for i in range(n):
    dic[i] = ["false",0]
dic[0] = ["true",0]

for j in range(n):
    if dic[a-1][0]=="true":
        loop = j+1-dic[a-1][1]
        loopstart = j-loop+1 #何回目
        start = a #何から
        break
    
    dic[a-1]=["true",j+1]
    a = l[a-1]
    
if k <= loopstart:
    a = 1
    for i in range (k):
        a = l[a-1]
    print(a)
    sys.exit()
        
    
else:
    amari =(k-loopstart)%loop
    if amari ==0:
        print(start)
    else:
        a = start
        for i in range(amari):
            a = l[a-1]
        print(a)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
dic = {}
a = l[0]
for i in range(n):
    dic[i] = ["false",0]
dic[0] = ["true",0]

for j in range(n):
    if dic[a-1][0]=="true":
        loop = j+1-dic[a-1][1]
        loopstart = j-loop+1 #何回目
        start = a #何から
        break
    
    dic[a-1]=["true",j+1]
    a = l[a-1]
    
if k < loopstart:
    a = 1
    for i in range (k):
        a = l[a-1]
    print(a)
    sys.exit()
        
    
else:
    amari =(k-loopstart)%loop
    if amari ==0:
        print(start)
    else:
        a = start
        for i in range(amari):
            a = l[a-1]
        print(a)

Submission status: Runtime Error

Solution code:
import collections
import numpy as np
import sys
import copy

N,K=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N):
    A[i]-=1
B=[0]
visited=[0]*N
visited[0]=1
Flag=True
for i in range(0,N*2):
    next=A[B[i]]
    if visited[next]==0:
        B.append(next)
        visited[next]=1
    else:
        C=B[B.index(next):]
        num=B.index(next)
        break
#print(C)
if K>len(C):
    Ans=(K-num)%len(C)
    print(C[Ans]+1)
else:
    print(B[K])

Submission status: Runtime Error

Solution code:
import numpy as np
import functools
import math
import collections
import scipy
import fractions
import itertools
 
def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    now = 0
    hoge= []
    bit = 0
    piyo = 0
    huga = []
    while True:
        hoge.append(now)
        now = a[now]-1
        if now in hoge:
            huga = hoge[hoge.index(now):len(hoge)]
            piyo = hoge.index(now)
            bit = 1
            break
    ans = huga[(k-piyo)%len(huga)]
    print(ans+1)
    return 0
 
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val - 1] or val == t[val - 1]:
        break
    visited[val - 1] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
if hold == 0:
    print(t[val - 1])
    exit(0)
if val == t[val - 1]:
    print(1/0)
    exit(0)
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)
# print('jump', jump)
# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    # print('b', ans)
print(ans)

Submission status: Runtime Error

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

visited = [False]*(2*10**5 + 10)
val = 1
count = 0
hold = k
while hold > 0 :
    if visited[val - 1] or val == t[val - 1]:
        break
    visited[val - 1] = True
    val = t[val - 1]
    # print('a', val)
    count += 1
    hold -= 1
if hold == 0:
    print(1/0)
    exit(0)
if val == t[val - 1]:
    print(1/0)
    exit(0)
count0 = count
count = 1
newval = val
start = t[newval - 1]
while start != newval:
    count += 1
    start = t[start - 1]
# print('count', count, 'count0', count0)
if t[val - 1] == t[0]:
    temp = k
else:
    temp = k - count0
# print('temp', temp)
jump = (temp) % (count)
# print('jump', jump)
# print('val', val)
ans = val
for i in range(jump):
    ans = t[ans - 1]
    # print('b', ans)
print(ans)

Submission status: Runtime Error

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

D = 60 #10**18は60桁なので2の59乗まで見る
MAX_N = 2*10**5+5 #頂点番号
to = [[0 for _ in range(D)] for _ in range(MAX_N)]

#to[i][j]:jから2^i個先の頂点
for i in range(n):
    to[0][i] = A[i]
    to[0][i] -= 1

#ダブリングの表
for i in range(D-1):
    for j in range(n):
        to[i+1][j] = to[i][to[i][j]]

v = 0
for i in range(D-1, -1, -1):
    l = 1<<i #2^i個先を見る
    if l <= k:
        v = to[i][v]
        k -= l

print(v+1)

Submission status: Runtime Error

Solution code:
import numpy as np
 
n, k = map(int, input().split())
 
a = list(map(int, input().split()))
 
route = [1]
 
end = False
i = 0
 
if a[0] == 1:
    ans = 1
else:
  while not end:
    if a[i] not in route:
      route.append(a[i])
      i = a[i] -1
      # route[1].append(1)
    else:
      position = route.index(a[i])
      # print(position)
      break
    if len(route) == n:
      overhead = 0
      end = True
 
  overhead = position
  cycle_length = len(route) - overhead
 
  # print(route)
  # print(overhead)
  # print(cycle_length)
 
  end_position = (k-overhead) % cycle_length
  # print(end_position)
 
  ans = route[overhead + end_position]
 
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**5)

def search(u, n, g, disc, k):
    if disc[u] != None:
        circles = n - disc[u]
        v = (k - disc[u]) % circles
        ans = u
        for i in range(v):
            ans = g[ans]
        return ans + 1
    disc[u] = n
    return search(g[u], n+1, g, disc, k)

def resolve():
    n, k = map(int, input().split())
    g = list(map(lambda x: int(x) - 1, input().split()))
    disc = [None]*n
    v = search(0, 0, g, disc, k)
    print(v)
resolve()

Submission status: Runtime Error

Solution code:
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**5)

def search(u, n, g, disc, k):
    if disc[u] != None:
        circles = n - disc[u]
        v = (k - disc[u]) % circles
        ans = u
        for i in range(v):
            ans = g[ans]
        return ans + 1
    disc[u] = n
    return search(g[u], n+1, g, disc, k)
resolve()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
def f(x):
    return A[x-1]
c = 1
P = [1]
 
for i in range(1,k+1):
    c = f(c)
    if c in P:
        roop_start = P.index(c)
        roop_num = i- roop_start
        break
    P.append(c)
a = (k - roop_start) %  roop_num
for i in range(a):
    P[roop_start] = f(P[roop_start])
print(P[roop_start])

Submission status: Runtime Error

Solution code:
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(100**5)

def search(u, n, g, disc, k):
    if disc[u] != None:
        circles = n - disc[u]
        v = (k - disc[u]) % circles
        ans = u
        for i in range(v):
            ans = g[ans]
        return ans + 1
    disc[u] = n
    return search(g[u], n+1, g, disc, k)

def resolve():
    n, k = map(int, input().split())
    g = list(map(lambda x: int(x) - 1, input().split()))
    disc = [None]*n
    v = search(0, 0, g, disc, k)
    print(v)
resolve()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
def f(x):
    return A[x-1]
c = 1
P = [1]
 
for i in range(1,k+1):
    c = f(c)
    if c in P:
        roop_start = P.index(c)
        roop_num = i- roop_start
        break
    P.append(c)
a = (k - roop_start) %  roop_num
for i in range(a):
    P[roop_start] = f(P[roop_start])
print(roop_start)

Submission status: Runtime Error

Solution code:
def search(u, n, g, disc, k):
    if disc[u] != None:
        circles = n - disc[u]
        v = (k - disc[u]) % circles
        ans = u
        for i in range(v):
            ans = g[ans]
        return ans + 1
    disc[u] = n
    return search(g[u], n+1, g, disc, k)

def resolve():
    n, k = map(int, input().split())
    g = list(map(lambda x: int(x) - 1, input().split()))
    disc = [None]*n
    v = search(0, 0, g, disc, k)
    print(v)
resolve()

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
li = [1,]
ans = [0] * n 
tmp = 1
ind = []
count = 0
for i in range(10**5):
    li.append(a[tmp-1])
    ans[tmp-1] += 1
    tmp = a[tmp-1]
    for j in range(len(ans)):
        if ans[j] == 2:
            count = sum(ans) -1
            ind.append(j)
            ans[j] = 3
    if any(item > 3 for item in ans):
        break
s = count-2*len(ind)-1

if k <= s:
    print(a[k])
else:
    print(a[ind[(k-s+1) % (s+1) -2]])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
sub = [0]*n
city = 0
all = []
roop = []
while sub[city] != 2:
  if sub[city] == 0:
    all.append(city+1)
  else:
    roop.append(city+1)
  sub[city] += 1
  city = A[city] -1
if len(roop)<k:
  print(roop[(k-(len(all)-len(roop)))%len(roop)])
else:
  print(all[k])

Submission status: Runtime Error

Solution code:
import numpy as np
import itertools
n, k = map(int, input().split())
a = list(map(int, input().split()))

now = 1
town_list = [0]*n
town_list[0] = 1
tn_list = [1]

while(1):
    next = a[now-1]
    now = next
    if town_list[now-1] == 0:
        town_list[now-1] = 1
        tn_list.append(now)

    else:
        idx = tn_list.index(now)
        initial = tn_list[:idx]
        cycle = tn_list[idx:]
        break

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