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

move1 = [1]
_num = move1[0]
for i in range(n*2):
    _num = al[_num-1]
    move1.append(_num)

for i in range(n):
    if move1[-i-(i):-i] == move1[-i:]:
        move2 = move1[-i:]
        num = n-i
        break
#
# print(move1)
# print(move2)
# print(num)

if k < n-num:
    print(move1[k-1])
else:
    nn = (k-n-num)%n
    print(move2[nn-1])

Submission status: Runtime Error

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
    if count == k:
        print(i + 1)
        break
    else:
        pass
if b <= k:
    b = roop_find.index(a[i]-1)
    roop = list(roop_find)[b:count+1]

    k -= b
    e = k % (count+1-b)
    print(roop[e]+1)

Submission status: Runtime Error

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

rooplist = [1]

for i in range(k):
  nexttown = alist[rooplist[i]-1]
  if nexttown in rooplist:
    for j in range(len(rooplist)):
      if rooplist[j] == nexttown:
        rooplist = rooplist[j:]
        startlist = rooplist[:j]
        break
    break
  rooplist.append(nexttown)
  
print(rooplist)

print(rooplist[(k-len(startlist))%len(rooplist)])

Submission status: Runtime Error

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

route = [1]
next = A[0]
s=0
while(True):
    if next not in route:
        route.append(next)
        next = A[next-1]
    elif A[0] == next:
        break
    else:
        s = len(route[:route.index(next)])
        route = route[route.index(next):]
        break

if s == 0:
    print( route[ K%len(route)])
else:
    print( route[K%len(route) - s])

Submission status: Runtime Error

Solution code:
import bisect
n,k = map(int,input().split())
A = list(map(int,input().split()))
memo = [0 for _ in range(n+1)]
dict = {i:0 for i in range(1,n+1)}
memo[0] = 1
dict[1] = 1
i = 1
j = 1
loopnum = 0
ans = 0
while(j <= k):
    if dict[A[i-1]] == 1:
        loopnum = A[i-1]
        break
    a = A[i-1]
    memo[j] = a
    dict[a] = 1
    i = a
    j += 1
    ans = a
if loopnum != 0:
    left = 0
    if loopnum != 1:
        left = bisect.bisect_left(memo,loopnum,hi=j+1)+1
    f = j - left
    diff = max(left,0)
    index = (k - diff) % f + diff
    ans = memo[index]

print(ans)

Submission status: Runtime Error

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

route = []
for _ in range(N)
route.append(-1)

route[0] = 0
P = 1
f = 0
for i in range(1,K+1):
  P = A[P-1] 
  if route[P-1] == -1:
    route[P-1] = i
  
  else:
    ilast = i
    loop = i-P
    f = 1

    break
    
if f == 1:

  ans = (ilast - loop )+ (K-ilast)%loop
  P = A[route.index(ans)]


print(P)

Submission status: Runtime Error

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

flag=[0]*n
place=[0]*n
p=0
for i in range(len(a_list)+1):
    if flag[p]>0:
        rep=i
        replis=place[flag[p]-1:i]
        st=p+1
        break
        
    else:
        place[i]=p+1
        flag[p]=i+1
        p=a_list[p]-1


if i>k:
    print(replis[(k-st-len(replis))%len(replis)])
else :
    print(place[k-1])

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    def main():

        k = map(int, input().split())
# 場所
        a = list(map(int, input().split()))
# 答え
        ans = []*k
        for i in [0] * (k+1):
            if i <= k:
                ans.append(a[a[i-1]])
            print(ans[k-1])
main()

Submission status: Runtime Error

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

done = [0]
for _ in range(K):
    if A[done[-1]] not in done:
        done.append(A[done[-1]])
    else:
        index = A[done[-1]]
        break

if K < len(done):
    print(done[K] + 1)
else:
    print(done[((K - len(done[:index])) % len(done[index:]))] + 1)

Submission status: Runtime Error

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


lst = []
visit_now = 1
visit_list = [0] * n

while True:
    if visit_list[visit_now - 1] == 1:
        break
    visit_list[visit_now - 1] = 1
    lst.append(visit_now)
    visit_now = a[visit_now - 1]


start = lst.index(visit_now)
loop_num = n - start - 1
lst = lst[start:]

k -= start
k %= loop_num

print(lst[k])
# print(lst, loop_num, loop_list, k)

Submission status: Runtime Error

Solution code:
# N個の町, K個のテレポート
N, K = map(int, input().split())

# 町
array = list(map(int, input().split()))

for i in range(K-1):
  if i == 0:
    ans=array[i]
  else:
    ans=array[ans]

print(ans)

Submission status: Runtime Error

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

t = [0]*(n+1)
t[1] = 1
q = [1]

for i in range(1,k):
    if t[s[i]] == 0:
        t[s[i]] = 1
        q.append(s[i])
    elif i == k:
        print(s[i])
        exit()
    elif t[s[i]] != 0:
        ans = k%(len(q))
        print(q[ans])
        exit()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int,input().split()))
P = 0
Q = [0] + [1 for _ in range(N-1)]
R = [-1] + [0 for _ in range(N-1)]
L = 0
for i in range(K):
    P = A[P]-1
    if Q[P] == 0:
        L = i - R[P]
        break
    if Q[P] == 1:
        R[P] = i
        Q[P] = 0
S = (K - R[P] - 1) % L
G = 0
for j in range(S + R[P] + 1):
    G = A[G]-1
print(G+1)

Submission status: Runtime Error

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

move1 = [1]
_num = move1[0]
for i in range(n*2):
    _num = al[_num-1]
    move1.append(_num)

for i in range(n):
    if move1[-i-(i):-i] == move1[-i:]:
        move2 = move1[-i:]
        num = n-i
        break
#
# print(move1)
# print(move2)
# print(num)

if k <= n-num:
    print(move1[k-1])
else:
    nn = (k-n-num)%n
    print(move2[nn-1])

Submission status: Runtime Error

Solution code:
import math
I = [input() for i in range(2)]
W = I[0].split()
A = list(map(int,I[1].split()))
N = int(W[0])
K = int(W[1])

n = 0
Sn = str()
for i in range(N) :
    #print(n)
    n = A[n-1]
    Sn = Sn + str(n)
    c = Sn.count(str(n))
    if c == 2 :
        j = 0
        l = i - 1
        x = Sn.find(str(n))
        break
    j = 1
    
if j == 0 :
    M = (K-x)%l
    #print (Sn)
    #print(M)
    ans = int(Sn[M-1])
elif j == 1 :
    ans = n + 1

print(ans)

Submission status: Runtime Error

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

visited = []
x=0
for i in range(n):
  x = a[x]
  if x in visited:
    for j in range(i):
      if visited[j] == x:
        first = j
        break
    T = i - j
  break
  else:
    visited.append(a[i])
mod = (k-first) % T
print(a[first+mod]+1)

Submission status: Runtime Error

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

visit = [] #訪れる街
loop_point = 0 #以後繰り返し
now = 1
if 1 in a:
  now = a.index(1) #今いる街
passport = [int(0) for j range(n)]

for i in range(n):
  if passport[now - 1] == 1:
    loop_point = a[now - 1] #もしきたことある街だったら繰り返しポイント
    break
  else:
    visit.append(a[now - 1]) #きたことなかったらvisitに足す
    now = a[now - 1]
    passport[now - 1] = 1
    continue

where = visit.index(loop_point) #繰り返しポイントの街はVISITリスト[何]？

if k <= len(visit) - where:
  print(visit[k])
else:
  print(visit[(k % (len(visit) - where)) - 1])

Submission status: Runtime Error

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

B = [0] * N
C = 0
D = 0
E = []
for i in range(K):
    if i == 0:
        B[i] += 1
        C = A[0]
        D += 1
        E.append(C)
        #print(i, C)
        #print(B)
    else:
        if B[C - 1] == 0:
            B[C - 1] += 1
            C = A[C - 1]
            D += 1
            #E.append(C)
            #print(i, C)
            #print(B)
        else:
            #print("２回目")
            #print(D, K % D)
            #print(E)
            print(E[K % D])
            exit()

print(C)

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()
    A = LIST()

    l = [A[0]]
    for i in range(N):
        l.append(A[l[-1]-1])

    for i in range(N):
        if len(l[:i+1]) != len(set(l[:i+1])):
            e_idx = i
            point = l[i]
            break
    else:
        e_idx = N
        point = l[0]

    for i in range(e_idx - 1, -1, -1):
        if l[i] == point:
            dist = e_idx - 1
            s_idx = i
            break

    if K <= e_idx:
        print(l[K])
    else:
        print(l[(K - len(l[:s_idx])) % dist])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
count = 0
memo = [1]
his = [0]*n
his[0] += 1
index = 0
i = 0
while his[A[i]-1] == 0:
    his[A[i]-1] += 1
    memo.append(A[i])
    i = A[i]-1

index = memo.index(A[i])
roop=memo[index:]

if k>len(memo):
    k-=len(memo)
    k%=len(roop)
    print(roop[k])
else:
    print(memo[k])

Submission status: Runtime Error

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

A = deque(map(int, input().split()))
li = []
li2 = [0]*n
li.append(1)

next = 0
t=0
rep = False

for i in range(n):
    li.append(A[next])
    li2[next] = 1
    next = A[next]-1

    if li2[next] == 1:
        t = li.index(A[next])
        rep = True
        break

if rep == True:
    tmp = (k%len(li[t:])) - len(li[0:t])
    g=li[t:]
    if t==0:
        print(g[0])
    else:
        print(g[tmp])
else:
    print(li[k-1])

Submission status: Runtime Error

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

G = {}
for i in range(1, n + 1):
    G.update({i: a[i - 1]})

seen = [False] * (n + 1)
path = []

def dfs(graph, start):
    global return_ans
    seen[start] = True
    path.append(start)
    
    if seen[graph[start]]:
        return_ans = graph[start]
    else:
        dfs(graph, graph[start])
    
    return return_ans

point = dfs(G, 1)
m = path.index(point)

if m > k:
    print(path[k])
else:
    k -= m
    path = path[m:]
    k %= len(path)
    print(path[k])

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline

N,K=map(int,input().split())
T=list(map(int,input().split()))
M=0
A=[1]
for x in range(N+1):
    if T[M] in A or T[M]==M+1:
        key=T[M]
        break

    else:
        t=T[M]
        A+=t
        M=t-1
B=len(A)
C=A.index(key)
D=B-C
if K<=B-1:
    F=A[K]
    print(F)
else:
    E=(K+1-C)%D
    if E==0:
        E=D
    F=A[C+E-1]
    print(F)

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
P = 1
L =[]
while not(P in L):
    L.append(P)
    P= A[P-1]
C = L.index(P)
if NK< C:
   print(L[K])
else:
    L = L[C:]
    B=(K-C)%len(L)    
    print(L[B])

Submission status: Runtime Error

Solution code:
a=input()
a0=a.split()
n=int(a0[0])
k=int(a0[1])
b=input()
b0=b.split()
p=1
c=0
d=0
l=list(range(n))
l1=list(range(n))
for i in range(k):
    c=c+1
    l[p-1]=l[p-1]+1
    p=int(b0[p-1])
    for x in range(n):
        if l[x]==l1[x]+2:
            break
    if l[x]==l1[x]+2:
        break
for i in range(k):
    d=d+1
    p=int(b0[p-1])
    if p==d:
        break
r=c-d
s=(k-d)%r
if s<=n:
    print(b0[s+1])
else:
    print(b0[0])

Submission status: Runtime Error

Solution code:
from collections import Counter

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



def loop(city, past, A, N, K):

    city = A[city-1]
    if Counter(past).get(city):
        loopNumber = len(past) - past.index(city)
        loopAray = past[past.index(city):]
        loopStart = past.index(city)
        print(loopAray[(K - loopStart)%loopNumber])
    else:
        past.append(city)
        loop(city, past, A, N, K)

loop(1, [1], A, N, K)

Submission status: Runtime Error

Solution code:
import bisect
n,k = map(int,input().split())
A = list(map(int,input().split()))
memo = [0 for _ in range(n+1)]
dict = {i:0 for i in range(1,n+1)}
memo[0] = 1
dict[1] = 1
i = 1
j = 1
loopnum = 0
ans = 0
while(j <= k):
    if dict[A[i-1]] == 1:
        loopnum = A[i-1]
        break
    a = A[i-1]
    memo[j] = a
    dict[a] = 1
    i = a
    j += 1
    ans = a
if loopnum != 0:
    left = 0
    if loopnum != 1:
        left = bisect.bisect_left(memo,loopnum,hi=j)+1
    f = j - left
    diff = max(left,0)
    index = (k - diff) % f + diff
    ans = memo[index]

print(ans)

Submission status: Runtime Error

Solution code:
N,K= map(int, input().split())
A = list(map(int,input().split()))
num = 0
count = 0
l = []
while(count < K):
  if(l.count(num) == 0):
    l.append(num)
    num = A[num-1]
  else:
    junkai = num
    ind = l.index(num)
    le = len(l) - ind-1
  count += 1
ans = K - le
ans = ans%(len(l)-1)
print(A[ans-1])

Submission status: Runtime Error

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

a=1
Ans1=[]
Ans2=0

for k in range(K):
    a=A[a-1]
    if a in Ans1 :
        Ans4=a
        break
    else:
        if a==1:
            Ans1.append(a)
            Ans4=1
            break
        else:
            Ans1.append(a)

Ans5=Ans1.index(Ans4)

Ans3=[]
if Ans4==1:
    for num in range(len(Ans1)):
        Ans3.append(Ans1[num])
else:
    for num in range(Ans5,len(Ans1)):
        Ans3.append(Ans1[num])

if K < len(Ans1) :
    print(Ans1[K-1])
else:
    if len(Ans3)==0:
        K1=K//len(Ans1)
        K2=K-K1*len(Ans1)
        print(Ans1[K2-1])
    else:
        if Ans4==1:
            K2=K1//len(Ans3)
            K3=K1-len(Ans3)*K2
            print(Ans3[K3-1])
        else:
            K1=K-Ans5
            K2=K1//len(Ans3)
            K3=K1-len(Ans3)*K2
            if K3==0:
                print(Ans3[len(Ans3)])
            else:
                print(Ans3[K3-1])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
s_d2 = set()
d2 = []
cur = 1
d2.append(1)
s_d2.add(1)
st_idx = 0
rp_lgt = 0
for i in range(n):
  cur = a[cur-1]
  if cur in s_d2:
    for j in range(len(d2)):
      if cur == d2[j] :
        st_idx = j - 1 
        rp_lgt = i - j + 1
        break
  else:
    d2.append(cur)
    s_d2.add(cur)
wk = k - st_idx
md = wk % rp_lgt
if k < st_idx:
  ans = d2[k]
else:
  ans = d2[md+st_idx]
#print(d2)
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=[q-1 for q in LI()]

visited=[-1]*n
now=0
visited[now]=0

if k<=2*10**5:
    for i in range(k):
        nxt=a[now]
        now=nxt
    print(now)
    exit()

for i in range(k):
    nxt=a[now]
    now=nxt
    if visited[nxt]==-1:
        visited[nxt]=i
    else:
        m=i-visited[nxt]
        break
now=0
for i in range(k%m+m):
    nxt=a[now]
    now=nxt
print(now)
exit()

Submission status: Runtime Error

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

#loop check
gone = []
pos = 0
i = 0
while pos not in gone:
    gone.append(pos)
    pos = A[pos] - 1
    i += 1
    if (pos in gone):
        loop_flag = True
        loop_pos = gone.index(pos)
        loop_len = len(gone)-loop_pos



if (loop_flag):
    pos = loop_pos
    for i in range(((K - loop_pos) % loop_len)-1:
        pos = A[pos]-1
    
else:
    pos = 0
    for i in range(K):
        pos = A[pos]-1

print(pos+1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
way = A[0]
List = [0 for n in range(N)]
List[way-1] += 1
count = 0
string = f"{way}"
#print(f"string:{string}")
#print(f"way:{way}")

for i in range(10**7):
    way = A[way-1]
    List[way-1] += 1 
    count += 1
    #print(f"way:{way}")
    #print(f"count:{count}")
    #print(List)
    if max(List) == 3:
        break
    else:
        string += f"{way}"
        #print(f"string:{string}")
        #print()

#print(List)
#print()
new_list = [i for i in List if i == 1]
length = len(new_list)
n_list = [j for j in List if j >= 2]
#print(new_list)
#print(n_list)
amari = (K - length)% len(n_list)

print(int(n_list[amari+1]))

Submission status: Runtime Error

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

loopcount=0
tmpPoint=1
vis={1:0}
for i in range(N):
    Next=A[tmpPoint-1]
    if Next in vis:
        startat=i
        #print(startat)
        #print(tmpPoint)
        #print(vis)
        #print(Next)
        #print(vis[Next]+1)
        loopcount=i - vis[Next]+1
        break
    vis[Next]=i+1
    tmpPoint=Next

if loopcount==0:
    print(tmpPoint)
    exit()
if startat+nokori < K:
    nokori=(K-startat) % loopcount

tmpPoint=1
for i in range(startat+nokori):
    Next=A[tmpPoint-1]
    tmpPoint=Next
print(tmpPoint)

Submission status: Runtime Error

Solution code:
#スペース区切りの整数の入力
#N,K = map(int, input().split())
#A_list = list(map(int, input().split()))
j_list = []
num = 1

owari  = 0
hajimari = 0
amari = 0
ans = 0

if(K<10):
    for i in range(K):
        num = A_list[num-1]
    print(num)
else:
    for i in range(N):
        num = A_list[num-1]
        if((num in j_list)==True):
            owari = num - 1
            hajimari = j_list.index(num)
            continue
        j_list.append(num)
    amari = (K - (hajimari-1) )%(owari-hajimari+1)
    ans = (hajimari-1)+amari
    print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
next = 1
a = list(map(int, input().split()))
b = [1]
len = k
while True:
  
  next = a[next - 1]
  len -= 1
  if next in b or len == 0:
    break
  b.append(next)
mama = 0
if len == 0:
  next = 1
  for j in range(k):
    next = a[next - 1]
    print(next)
else:
  for i in b:
    if i != next:
      b.pop(i)
      mama += 1
    else:
      break
 
  count = len(b)
  k = k - mama
  k = k % count
  next = 1
  for j in range(mama):
    next = a[next - 1]
  for l in range(k):
    next = a[next - 1]
  print(next)

Submission status: Runtime Error

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

rireki = [0] * (n+10)
curr_c = 1
rireki[1] = 0

no_loop_count = k
loop_cnt = 0

mugen = False
for i in range(n):
    next_c = a_list[curr_c-1]
    # print(i, curr_c, next_c)
    if next_c == curr_c:
        if k-1 >= i:
            mugen = True
            mugen_num = next_c
    if next_c == 1:
        no_loop_count = 0
        loop_cnt = i + 1
        break
    elif rireki[next_c] == 0:
        rireki[next_c] = i+1
    else:
        # print(next_c)
        no_loop_count = rireki[next_c] -1
        loop_cnt = i - no_loop_count
        break
    curr_c = next_c

if loop_cnt ==0:
    loop_cnt = 1


if mugen:
    print(mugen_num)


if not mugen:
    if k <= no_loop_count or loop_cnt == 0:
        ans = rireki.index(k)
    else:
        # print('k',k)
        # print('no', no_loop_count)
        # print('lo', loop_cnt)
        ind = (k-1-no_loop_count)%loop_cnt +1
        rireki_fix = [i-no_loop_count for i in rireki]
        # print(ind, 'ind')
        ans = rireki_fix.index(ind)
        # print(rireki_fix)
        print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
dist = [[] for i in range(N)]
A = list(map(int, input().split()))
for i in range(N):
    dist[i].append(A[i] - 1)
dp = [[0, 0] for i in range(N)]
 
def dfs(pos, depth):
    # 初めての訪問なら
    if dp[pos][0] == 0:
        dp[pos][0] = depth
        for i in dist[pos]:
            dfs(i, depth + 1)
    elif dp[pos][1] == 0:
        dp[pos][1] = depth
        for i in dist[pos]:
            dfs(i, depth + 1)
dfs(0, 0)
for i in range(N):
    if dp[i][0] == K or dp[i][1] == K:
        print(i + 1)
        break
    else:
        split = dp[i][1] - dp[i][0]
        if split > 0:
            if (K - dp[i][0]) % split == 0:
                print(i + 1)
                break

Submission status: Runtime Error

Solution code:
N,K=(int(x) for x in input().split())
A=list(map(int, input().split()))
if K<=N:
    s=1
    for i in range(K):
        s=A[s-1]
    print(s)
else:
    s=1
    S={1}
    iloop=1
    while A[s-1] not in S:
        S.add(A[s-1])
        s=A[s-1]
        iloop=iloop+1
    p=s
    loop=0
    while A[s-1] != p:
        loop=loop+1
        s=A[s-1]
    g=(K-iloop)%loop+iloop
    s=1
    for i in range(g):
        s=A[s-1]
    print(s)

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
A =[0]
B=list(map(int, input().split()))
for m in range(N):
    A.append(B[m])
place=set([])
i=1
count=0
while i not in place:
    place.add(i)
    i=A[i]
    count+=1
if count>=K:
    print(place[K])
else:
    loop=[i]
    start=i
    j=A[i]
    count+=1
    while j!=start:
        loop.append(j)
        j=A[j]
        count+=1
    if count>=K:
        print(loop[K-len(place)-1])
    else:
        num=(K-count)%len(loop)
        print(loop[num])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
loc=0
x=[-1]*N
y=0
distance=0
flag=False
for i in range(min(N,K+1)):
  if(x[loc]>=0):
    distance=i-x[loc]
    y=x[loc]
    flag=True
    #x[loc]=i
    v=loc
    break
  else:
    x[loc]=i
  loc=A[loc]-1
if(flag==True and y+distance<K):
  print(x.index((K-y)%distance+y)+1)
else:
  #print(x)
  print(v+1)

Submission status: Runtime Error

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

print(B[k-1])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.insert(0,0)
#print(a)
count=[0,a[1]]
#print(count)
for i in range(2,k+1):
    if a[count[i-1]] in count:
        count.append(a[count[i-1]])
        break
    count.append(a[count[i-1]])
count.pop(0)
#print(count)
f=count.index(count[-1])
d=len(count)-1-f
#print(f,d)
k-=f
k%=d
#print(k)
#print(count[f+k])
if k!=0:
    print(count[f+k-1])
else:
    print(count[f+d-1])

Submission status: Runtime Error

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n,k = map(int,input().split())
a = list(map(int,input().split()))
for i in range(len(a)):
    a[i] -= 1
pre = 0
d = []
for i in range(2*n+10):
    d.append(pre)
    pre = a[pre]
cnt = [0]*(n)
s = ""
for i in range(len(d)):
    cnt[d[i]]+=1
    if cnt[d[i]]>=2 and cnt[d[i]]<3:
        s += str(d[i])
ff = int(s[0])
check = 0
for i in range(len(d)):
    if ff == d[i]:
        check = i
        break
if check == 0:
    k %= len(s)
    print(a[k]+1)
else:
    k-= check
    k%=len(s)
    print(int(s[k])+1)

Submission status: Runtime Error

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

mod = [0]
flag = [0 for i in range(n)]
flag[0] = 1
idx = A[0]

for i in range(n):
    if flag[idx] == 1:
        idx = mod.index(idx)
        break
    flag[idx] = 1
    mod.append(idx)
    idx = A[idx]

if k > n:
    r = len(mod) - idx
    k -= idx
    mod = mod[idx:]
    print(mod[k % r] + 1)
else:
    print(mod[k]+1)

Submission status: Runtime Error

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

L = []
V = [False] * N
now = 0

for i in range(K):
    if V[now]:
        break
    else:
        L.append(now)
        V[now] = True
        if now == A[now] - 1:
            print(now+1)
            exit()
        else:
            now = A[now] - 1

Bl = L[:L.index(now)]
Br = L[L.index(now):]

cnt = len(Bl)
K -= cnt
idx = K % len(Br)

print(Br[idx]+1)

Submission status: Runtime Error

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

s = 0
m = []

for i in range(K):
	s = l[s] - 1
    m.append(s)
    if len(m) != len(list(set(m))):
      int = m.index(s) - len(m)
      x = (K - m.index(s)) // int
      y = K - m.index(s) - x*int
      result = m[y]
      break

print(result + 1)

Submission status: Runtime Error

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

route = [-1] * N
route[0] = 0
P = 1
f = 0
for i in range(1,K+1):
  P = A[P-1] 
  if route[P-1] == -1:
    route[P-1] = i
  
  else:
    ilast = i
    loop = i-P
    f = 1

    break
    
if f == 1:

  ans = (ilast - loop )+ (K-ilast)%loop
  P = A[route.index(ans)]


print(P)

Submission status: Runtime Error

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

loopcount=0
tmpPoint=1
vis={1:0}
for i in range(N):
    Next=A[tmpPoint-1]
    if Next in vis:
        startat=i
        #print(startat)
        #print(tmpPoint)
        #print(vis)
        #print(Next)
        #print(vis[Next]+1)
        loopcount=i - vis[Next]+1
        break
    vis[Next]=i+1
    tmpPoint=Next

if loopcount==0:
    print(tmpPoint)
    exit()
if startat+loopcount < K:
    nokori=(K-startat) % loopcount

tmpPoint=1
for i in range(startat+nokori):
    Next=A[tmpPoint-1]
    tmpPoint=Next
print(tmpPoint)

Submission status: Runtime Error

Solution code:
n,k = [int(e) for e in input().split()]
a = [int(e) for e in input().split()]
tele = [1,a[0]]
def teleport(b,j):
    tele.append(a[b-1])
    if j >= n-2:
        return None
    teleport(a[b-1],j+1)
teleport(a[0],0)

ans = sorted(tele,key = tele.index)
same = 0
for i in range(n):
    if ans[i] == ans[i+1]:
        same = ans[i]
        break
index1 = -1
index2 = -1
for i in range(n+1):
    if tele[i] == same and index1 == -1:
        index1 = i
    elif tele[i] == same:
        index2 = i
        break

res = tele[:index2]
if k < index2:
    print(res[k])
else:
    print(res[index1])

Submission status: Runtime Error

Solution code:
n,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
vis=[False for i in range(n)]
a=[]
start=curr=0
vis[0]=True
a.append(start)
for i in range(k):
    curr=a[-1]
    if vis[arr[curr-1]-1]:
        a.append(arr[curr-1] - 1)
        break
    else:
        vis[arr[curr-1]-1]=True
        a.append(arr[curr-1]-1)
#now i have req cycle
if 3>5 and a[0]==a[-1]:
    a.pop()
    #print("helo")
    #print(len(a),a)
    print(a[k%(len(a))]+1)
else:
    count=0
    #print(a,"a")
    for i in range(len(a)):
        if a[i]==a[-1]:
            new=a[i:-1]
            #print(new,a[i:-1])
            break
        else:
            count+=1
    #print(len(new),new)
    #rint(count,"c")
    print(new[(k-count)%(len(new))]+1)

Submission status: Runtime Error

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

current_loc = 1
next_loc = A[0]
check = [0]*N
check_dif = 0


for i in range(K):
    #現在地を次の場所にする
    current_loc = next_loc
    #次の行き先をテレポータの値から出す
    next_loc = A[current_loc-1]
    #はじめてきたのなら、そのときの回数iを記録し、2回目にきたらbreak
    if check[current_loc-1] == 0:
        check[current_loc-1] = i
    else:
        check_dif=check[current_loc-1]-i
        break

K_new = (K-i)%check_dif

for i in range(K_new):
    current_loc = next_loc
    next_loc = A[current_loc-1]

print(current_loc)

Submission status: Runtime Error

Solution code:
def teleport(n):
    global flag
    if flag:
        return 0
    else:
        if n in ans_list:
            flag=True
        ans_list.append(n)
        teleport(A[n])
n=0
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.insert(0,0)
ans_list=[]
flag=False
teleport(A[1])
x=ans_list.index(ans_list[-1])
loop_list=ans_list[x:-1]
y=len(ans_list)-1
K=K-y
loop_y=len(loop_list)
print(loop_list[(K-1)%(len(loop_list)-1)])

Submission status: Runtime Error

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

town_check = {0}
town_order = [0]
town = 0
check1 = True
while check1:
    next_town = A[town] - 1
    if next_town not in town_check:
        town = next_town 
        town_check.add(town)
        town_order.append(town)
    else:
        loop = (next_town, town)
        check1 = False
if K-2 <= len(town_order):
    print(town_order[K]+1)
else:
    a = town_order.index(loop[0])
    b = len(town_order) - 1
    c = b - a + 1
    print(town_order[a+(K-a)%(c)]+1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = [0] + list(map(int,input().split()))
vcnt = [0]*(n+1)
vcnt[1] += 1
v = 1
sentou = 0
slist = []
vlist = [1]
hajime = 10**10
for i in range(1,k+1):
    v = a[v]
    vlist.append(v)
    vcnt[v] += 1
    if sentou > 0:
        syuuki += 1
    if vcnt[v] == 2:
        syuuki = 1
        sentou = a[v]
        slist.append(v)
    if vcnt[v] == 3:
        syuuki += 1
        hajime = i - syuuki*2
        break
else:
    print(v)
    exit()
#print(syuuki,hajime,sentou)
#print(slist)
k -= hajime
#print(slist)
slist = [0] + slist
if (k+1)%syuuki == 0:
    print(slist[-1])
else:
    print(slist[(k+1)%syuuki])

Submission status: Runtime Error

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

lst = []
visit_now = 1

while True:
    if visit_list[visit_now - 1] == 1:
        break
    visit_list[visit_now - 1] = 1
    lst.append(visit_now)
    visit_now = a[visit_now - 1]


start = lst.index(visit_now)
loop_num = n - start - 1
lst = lst[start:]

k -= start
k %= loop_num

print(lst[k])
# print(lst, loop_num, loop_list, k)