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: Wrong Answer

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

route =[1]
cur = 1
for i in range(k):
    goto = a[cur - 1]
    if goto in route:
        pre_goto = route.index(goto)
        dif = len(route) - pre_goto
        rest_step = (k - pre_goto) % dif
        print(a[pre_goto + rest_step])
        break
    route.append(goto)
    cur = goto
else:
    print(a[k])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [False]*N
path = [0]
at = 0
loop = 0
visited[0]=True
for i in range(N):
    at = A[at]-1
    if visited[at]==True:
        loop = i+1-path.index(at)
        break
    else:
        visited[at]=True
        path.append(at)

print(path[(K-path.index(at))%loop+path.index(at)]+1)

Submission status: Wrong Answer

Solution code:
import sys
from functools import lru_cache
 
#read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
def mp(arg):
  return map(int,arg.split())
n,k=reads()
ls=[]
a=list(reads())
dic={}
ls=[1]
loopcount=loopstart=0
for i in range(k):
  hoge=ls[-1]
  hoge-=1
  if a[hoge] in dic:
    loopcount=i-dic[a[hoge]]
    loopstart=dic[a[hoge]]
    break
  ls.append(a[hoge])
  dic[a[hoge]]=i
else:
  print(ls[-1])
  exit()
k=(k-loopstart)%loopcount
print(ls[k+loopstart])

Submission status: Wrong Answer

Solution code:

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

town_loop = [1]
used = [-1] * n

for i in range(k):
    next_town = alist[town_loop[i]-1]

    if used[next_town-1] == -1:
        town_loop.append(next_town)
        used[next_town-1] = 1
        break
    else:
        break

loop_start = town_loop.index(next_town)
if len(town_loop) == k+1:
    print(town_loop[k])
else:
    o = len(town_loop) - loop_start
    print(town_loop[((k-loop_start) % o) + loop_start])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A = list(map(int,input().split()))
B = [0]*N
loop=0
now=0
C=[]
n=0
while(True):
    if B[now]==0:
        B[now]=1
        C.append(now+1)
        loop+=1
        now=A[now]-1
    else:
        n=A[now]
        break
if loop<=K:
    res=(K-C.index(n)+1)%(loop-C.index(n)+1)
    D=C[C.index(n)-1:]
    print(D[res%len(D)])
else:
    print(C[K])

Submission status: Wrong Answer

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n, k = map(int, input().split())
    f = list(map(lambda x : int(x) - 1, input().split()))
    step = [-1] * n
    step[0] = 0
    v = 0
    while step[f[v]] == -1:
        step[f[v]] = step[v] + 1
        if step[f[v]] == k:
            print(f[v])
            return
        v = f[v]


    k -= step[v]
    loop_length = step[v] - step[f[v]] + 1
    k %= loop_length
    for _ in range(k):
        v = f[v]
    print(v + 1)
resolve()

Submission status: Wrong Answer

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

num = list(map(int, input().split()))
t = [1]
t_test = []

for i in range(n + 1):
  x = num[t[-1] - 1]
  if(i == 0):
    t.remove(t[0])
  if(x not in t):
    t.append(x)
  else:
    t_test = t[t.index(x):len(t)]
    break

list_num = list(set(t) - set(t_test))
if(len(t_test) > 1):
  print(t_test[(k - len(list_num)) % len(t_test) - 1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
V=[]
p=0
x=0
for i in range(n):
  if A[p] in V:
    x=V.index(A[p])
    V=V[x:]
    break
  V.append(A[p])
  p=A[p]-1
#print(V)
print(V[(k-x)%len(V)-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
B=[0]
S=set()
S.add(0)
cur=A[0]-1
while True:
  if cur in S:
    break
  else:
    S.add(cur)
    B.append(cur)
    cur=A[cur]-1
d=len(B)
ind=B.index(cur)
print(B[(k-ind)%(d-ind)+ind]+1)

Submission status: Wrong Answer

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

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

b = [1 for i in range(n)]


now = 0
next = a[now] - 1
c = []
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = c[c.index(next):]
        print(d,e)
        break
    print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    print(" ",next)
    now = next

if k >= len(d):
    k = k - len(d)
    k = k % len(e)
    print(e[k]+1)

else:
    print(d[k]+1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k = map(int,input().split())

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

b = [1 for i in range(n)]


now = 0
next = a[now] - 1
c = []
check = False
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = c[c.index(next):]
        #print(d,e)
        check = True
        break
    #print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    #print(" ",next)
    now = next

if check:
    k = k - len(d)
    k = k % len(e)
    print(e[k]+1)

else:
    k = k % len(c)
    print(c[k]+1)

Submission status: Wrong Answer

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

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

b = [1 for i in range(n)]


now = 0
next = a[now] - 1
c = []
check = False
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = c[c.index(next):]
        print(d,e)
        check = True
        break
    print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    print(" ",next)
    now = next

if check:
    k = k - len(d)
    k = k % len(e)
    print(e[k]+1)

else:
    k = k % len(c)
    print(c[k]+1)

Submission status: Wrong Answer

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

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

b = [1 for i in range(n)]


now = 0
next = a[now] - 1
c = []
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = c[c.index(next):]
        #print(d,e)
        break
    #print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    #print(" ",next)
    now = next

k = k - len(d)
k = k % len(e)
print(e[k]+1)

Submission status: Wrong Answer

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

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

b = [1 for i in range(n)]


now = 0
next = -1
c = []
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = c[c.index(next):]
        #print(d,e)
        break
    #print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    #print(" ",next)
    now = next

k = k - len(d)
k = k % len(e)
print(e[k]+1)

Submission status: Wrong Answer

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

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

b = [1 for i in range(n)]


now = 0
next = -1
c = []
for i in range(n):
    if b[next] == 0:
        d = c[:c.index(next)]
        e = [c[-1]] + c[c.index(next):-1]
        #print(d,e)
        break
    #print(now)
    b[now] = 0
    c.append(now)
    next = a[now] - 1
    #print(" ",next)
    now = next

k = k - len(d)
k = k % len(e)
print(e[k])

Submission status: Wrong Answer

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

n_buf = [0 for i in range(n)]
path_buf = []

now = 1
while True:
    n_buf[now - 1] = 1
    _next = a[now - 1]
    path_buf.append(now)
    if n_buf[_next - 1] == 1:
        path_buf.append(_next)
        break

    now = _next

index = path_buf.index(path_buf[-1])
sa = len(path_buf) - index - 1


if k >= len(path_buf):
    ans = (k - len(path_buf) + 1) % sa
    ans = path_buf[-sa-1:-1][ans]

    print(ans)
else:
    ans = path_buf[k - 1]
    print(ans)

Submission status: Wrong Answer

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

n_buf = [0 for i in range(n)]
path_buf = []

now = 1
while True:
    n_buf[now - 1] = 1
    _next = a[now - 1]
    path_buf.append(now)
    if n_buf[_next - 1] == 1:
        path_buf.append(_next)
        break

    now = _next

index = path_buf.index(path_buf[-1])
sa = len(path_buf) - index - 1


if k >= len(path_buf) - 1:
    ans = (k - len(path_buf) + 1) % sa
    ans = path_buf[-sa-1:-1][ans]

    print(ans)
else:
    ans = path_buf[k - 1]
    print(ans)

Submission status: Wrong Answer

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

n_buf = [0 for i in range(n)]
path_buf = []

now = 1
while True:
    n_buf[now - 1] = 1
    _next = a[now - 1]
    path_buf.append(now)
    if n_buf[_next - 1] == 1:
        path_buf.append(_next)
        break

    now = _next

index = path_buf.index(path_buf[-1])
sa = len(path_buf) - index - 1

ans = (k - len(path_buf) + 1) % sa
ans = path_buf[-sa-1:-1][ans]

print(ans)

Submission status: Wrong Answer

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

visited=[1]

for i in range(n):
  next=a[visited[-1]]
  visited.append(next)
  
loop_end=n
loop_start=n-1
while visited[loop_start]!=visited[loop_end]:
  loop_start-=1
period=loop_end-loop_start
#print(visited) 
#print(period)
if k<n+1:
  print(visited[k])
  exit()
else:
  print(visited[k%period])

Submission status: Wrong Answer

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


flag = 0
position = 0
memo = [position]

while flag == 0:
    position = a[position] - 1
    if position in memo:
        flag = 1
    else:
        memo.append(position)

for i in range(len(memo)):
    if memo[i] == position:
        time_first = i
        break
time_second = len(memo)
time_loop = time_second - time_first

if time_loop == 1:
    print(memo[i])
else:
    K = K - time_first
    K = K % time_loop
    position = memo[i]
    for k in range(K):
        position = a[position] - 1
    print(position + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
  N,M=map(int,input().split())
  L=list(map(int,input().split()))
  i=1
  R=list()
  for j in range(N+1):
    if L[i-1] in R:
      c=R.index(L[i-1])
      t=len(R)-c
      m=R[c:]
      break
    R.append(L[i-1])
    i=L[i-1]
  l=(M-c-1)%t
  print(m[l])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
L=list(map(int,input().split()))
i=1
R=list()
for j in range(N+1):
  if L[i-1] in R:
    c=R.index(L[i-1])
    t=len(R)-c
    m=R[c:]
    break
  R.append(L[i-1])
  i=L[i-1]
l=(M-c-1)%t
print(m[l])

Submission status: Wrong Answer

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

route = [1]
i = 0
while True:
  next = A[i]
  if next not in route:
    route.append(next)
  else:
    cycle = route[route.index(next):]
    route = route[:route.index(next)]
    break

  i = next-1

K -= len(route)
print(cycle[K%len(cycle)])

Submission status: Wrong Answer

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

p = 0
route = [p]
route_set = set([p])
for i in range(1, K+1):
    p = A[p]-1
    if p in route_set:
        starti = route.index(p)
        div = i - starti
        rest = (K-starti) % div
        print(route[starti + rest]+1)
        break
    else:
        route.append(p)
        route_set.add(p)
else:
    print(p)

Submission status: Wrong Answer

Solution code:
import sys

def Destination(N, K, DestinationList, TravelHistory = [1] ):
    """
    for Num in range(N):
        Destination = DestinationList[ TravelHistory[-1]-1]
        TravelHistory.append(Destination)
        if(Destination in TravelHistory[:-1]):
            Pos = TravelHistory[:-1].index(Destination)
            Period = len(TravelHistory)-Pos-1
            break;
    """
    Pos_First = 0
    Pos_Second = 0
    for Num in range(N):
        Destination = DestinationList[ TravelHistory[-1]-1]
        """
        if(Destination in TravelHistory):
            Pos_First = TravelHistory.index(Destination)
            Pos_Second = len(TravelHistory)
            break
        """
        TravelHistory.append(Destination)
    if(K <= Pos_First ):
        return TravelHistory[K]
    for Num in range(N+1):
        if(TravelHistory[Num] in TravelHistory[Num+1:]):
            Pos_First = Num
            Pos_Second = TravelHistory[Num+1:].index( TravelHistory[Num] ) + Num+1

            break
    Period = Pos_Second-Pos_First
    #print(Pos_First, Pos_Second, Period)



    Times_K = (K-Pos_First)%Period
    Pos_K = TravelHistory[Pos_First+Times_K]
    return Pos_K


if(__name__ == '__main__'):
    In = input().split()

    N= int(In[0].strip())
    K= int(In[1].strip())

    DestinationList = []
    In = input().split()
    for Str in In:
        DestinationList.append( int(Str.strip()) ) # [M] -> value
    print(Destination(N, K, DestinationList) )

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()
 
#【ループ回数】200000
for i in range(1,len(A)):
    now_city=A[now_city]

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()
 
#【ループ回数】200000
for i in range(1,len(A)):
    now_city=A[now_city]
    #【ループ回数】B 内で now_city があるかを探索
    if now_city in B:
        route=B[C[now_city]-1:i-1]
        break
    B.append(now_city)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()
 
#【ループ回数】200000
for i in range(1,len(A)):
    now_city=A[now_city]
    #【ループ回数】B 内で now_city があるかを探索
    if now_city in B:
        route=B[C[now_city]-1:i-1]
        break
    B.append(now_city)
    C[now_city]=i

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()

#【ループ回数】200000
for i in range(1,len(A)):
    print("Baah")
    if now_city in B:
        route=B[C[now_city]-1:i-1]
        break
    B.append(now_city)
    C[now_city]=i

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()

#【ループ回数】200000
for i in range(1,len(A)):
    print("Baah")
    if now_city in B:
        route=B[C[now_city]-1:i-1]
        break

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()

#【ループ回数】200000
for i in range(1,len(A)):
    print("Baah")
    if now_city in B:
        print("Waah")

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()

#【ループ回数】200000
for i in range(1,len(A)):
    print("Baah")

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
roop = 0
port = [A[0]]
for _ in range(len(A)):
    port += [A[port[-1]-1]]
    if port.count(A[port[-2]-1]) == 2:#A in port
        port.pop()
        roop = len(port)-port.index(A[port[-1]-1])
        break
K = K-len(port)
print(port[len(port)-roop+(K%roop)-1])

Submission status: Wrong Answer

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

# mp = dict()
# for ix in range(N):
#     mp[ix+1] = A[ix]

hist = []
exists = [0] * (N+1)
cur = 1
cnt = 0

# ここがおかしい?
# while True:
#     cur = mp[cur]
#     cnt += 1
#     # K -= 1

#     if exists[cur] != 0:
#         break
#     else:
#         exists[cur] = 1
#         hist.append(cur)

A = [0] + A
# print(A)
while exists[cur] == 0:
    hist.append(cur)
    exists[cur] = 1
    cur = A[cur]

cycle_sp = cur
# cycle_l = 0
cycle_sp_ix = hist.index(cycle_sp)

import sys
if K < cycle_sp_ix:
    print(hist[K])
    sys.exit()

cycle_l = len(hist) - cycle_sp_ix
# while True:
#     cur = mp[cur]
#     cycle_l += 1

#     if cur == cycle_sp:
#         break

rest = (K-cnt) % cycle_l
# print("cnt:", cnt)

# print("K:", K)
# print("cycle_l:", cycle_l)
# print("rest:", rest)

print(hist[rest + cycle_sp_ix])
# while rest != 0:
#     cur = mp[cur]
#     rest -= 1

# print(cur)




"""
# hist = [1]
hist = {1: 0}
while True:

    hk = hist.keys()[-1]

    if not mp[hk] in hist.keys():
        # hist.append(mp[hist[-1]])
        hist[mp[hk]] = 0

    else:
        # found
        cstart = hist.index(mp[hk])
        cend = len(hist)
        cycle_l = cend - cstart
        # print("hist:", hist)
        # print("cstart:", cstart)
        # print("cend:", cend)
        # print("cycle_l:", cycle_l)

        ans_i = (K - cstart) % cycle_l
        # print("ans_i:", ans_i)
        ans = hist[cstart + ans_i]

        print(ans)
        break
"""

Submission status: Wrong Answer

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = []
exists = [0] * (N+1)
cur = 1
cnt = 0

# ここがおかしい?
# while True:
#     cur = mp[cur]
#     cnt += 1
#     # K -= 1

#     if exists[cur] != 0:
#         break
#     else:
#         exists[cur] = 1
#         hist.append(cur)

A = [0] + A
while exists[cur] == 0:
    hist.append(cur)
    exists[cur] = 1
    cur = A[cur]

cycle_sp = cur
cycle_l = 0
cycle_sp_ix = hist.index(cycle_sp)

import sys
if K < cycle_sp_ix:
    print(hist[K])
    sys.exit()

cycle_l = len(hist) - cycle_sp_ix
# while True:
#     cur = mp[cur]
#     cycle_l += 1

#     if cur == cycle_sp:
#         break

rest = (K-cnt) % cycle_l
# print("cnt:", cnt)

# print("K:", K)
# print("cycle_l:", cycle_l)
# print("rest:", rest)

print(hist[rest + cycle_sp_ix])
# while rest != 0:
#     cur = mp[cur]
#     rest -= 1

# print(cur)




"""
# hist = [1]
hist = {1: 0}
while True:

    hk = hist.keys()[-1]

    if not mp[hk] in hist.keys():
        # hist.append(mp[hist[-1]])
        hist[mp[hk]] = 0

    else:
        # found
        cstart = hist.index(mp[hk])
        cend = len(hist)
        cycle_l = cend - cstart
        # print("hist:", hist)
        # print("cstart:", cstart)
        # print("cend:", cend)
        # print("cycle_l:", cycle_l)

        ans_i = (K - cstart) % cycle_l
        # print("ans_i:", ans_i)
        ans = hist[cstart + ans_i]

        print(ans)
        break
"""

Submission status: Wrong Answer

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = []
exists = [0] * (N+1)
cur = 1
cnt = 0

while True:
    cur = mp[cur]
    cnt += 1
    # K -= 1

    if exists[cur] != 0:
        break
    else:
        exists[cur] = 1
        hist.append(cur)

cycle_sp = cur
cycle_l = 0
cycle_sp_ix = hist.index(cycle_sp)

import sys
if K < cycle_sp_ix:
    print(hist[K])
    sys.exit()

cycle_l = len(hist) - cycle_sp_ix
# while True:
#     cur = mp[cur]
#     cycle_l += 1

#     if cur == cycle_sp:
#         break

rest = (K-cnt) % cycle_l
# print("cnt:", cnt)

# print("K:", K)
# print("cycle_l:", cycle_l)
# print("rest:", rest)

print(hist[rest + cycle_sp_ix])
# while rest != 0:
#     cur = mp[cur]
#     rest -= 1

# print(cur)




"""
# hist = [1]
hist = {1: 0}
while True:

    hk = hist.keys()[-1]

    if not mp[hk] in hist.keys():
        # hist.append(mp[hist[-1]])
        hist[mp[hk]] = 0

    else:
        # found
        cstart = hist.index(mp[hk])
        cend = len(hist)
        cycle_l = cend - cstart
        # print("hist:", hist)
        # print("cstart:", cstart)
        # print("cend:", cend)
        # print("cycle_l:", cycle_l)

        ans_i = (K - cstart) % cycle_l
        # print("ans_i:", ans_i)
        ans = hist[cstart + ans_i]

        print(ans)
        break
"""

Submission status: Wrong Answer

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = []
exists = [0] * (N+1)
cur = 1
cnt = 0

while True:
    cur = mp[cur]
    cnt += 1
    # K -= 1

    if exists[cur] != 0:
        break
    else:
        exists[cur] = 1
        hist.append(cur)

cycle_sp = cur
cycle_l = 0
cycle_sp_ix = hist.index(cycle_sp)

import sys
if K < cycle_sp_ix:
    print(hist[K])
    sys.exit()


while True:
    cur = mp[cur]
    cycle_l += 1

    if cur == cycle_sp:
        break

rest = (K-cnt) % cycle_l
# print("cnt:", cnt)

# print("K:", K)
# print("cycle_l:", cycle_l)
# print("rest:", rest)

print(hist[rest + cycle_sp_ix])
# while rest != 0:
#     cur = mp[cur]
#     rest -= 1

# print(cur)




"""
# hist = [1]
hist = {1: 0}
while True:

    hk = hist.keys()[-1]

    if not mp[hk] in hist.keys():
        # hist.append(mp[hist[-1]])
        hist[mp[hk]] = 0

    else:
        # found
        cstart = hist.index(mp[hk])
        cend = len(hist)
        cycle_l = cend - cstart
        # print("hist:", hist)
        # print("cstart:", cstart)
        # print("cend:", cend)
        # print("cycle_l:", cycle_l)

        ans_i = (K - cstart) % cycle_l
        # print("ans_i:", ans_i)
        ans = hist[cstart + ans_i]

        print(ans)
        break
"""

Submission status: Wrong Answer

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = []
exists = [0] * (N+1)
cur = 1
cnt = 0

while True:
    cur = mp[cur]
    cnt += 1
    # K -= 1

    if exists[cur] != 0:
        break
    else:
        exists[cur] = 1
        hist.append(cur)

cycle_sp = cur
cycle_l = 0

import sys
if K < hist.index(cycle_sp):
    print(hist[K])
    sys.exit()


while True:
    cur = mp[cur]
    cycle_l += 1

    if cur == cycle_sp:
        break

rest = (K-cnt) % cycle_l
# print("cnt:", cnt)

# print("K:", K)
# print("cycle_l:", cycle_l)
# print("rest:", rest)

while rest != 0:
    cur = mp[cur]
    rest -= 1

print(cur)

Submission status: Wrong Answer

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = []
cur = 1
cnt = 0

while True:
    cur = mp[cur]
    cnt += 1
    # K -= 1

    if cur in hist:
        break
    else:
        hist.append(cur)

cycle_sp = cur
cycle_l = 0

import sys
if K < hist.index(cycle_sp):
    print(hist[K])
    sys.exit()


while True:
    cur = mp[cur]
    cycle_l += 1

    if cur == cycle_sp:
        break

rest = (K-cnt) % cycle_l
# print("cnt:", cnt)

# print("K:", K)
# print("cycle_l:", cycle_l)
# print("rest:", rest)

while rest != 0:
    cur = mp[cur]
    rest -= 1

print(cur)

Submission status: Wrong Answer

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = set()
cur = 1
cnt = 0

while True:
    cur = mp[cur]
    # cnt += 1
    K -= 1

    if cur in hist:
        break
    else:
        hist.add(cur)

cycle_sp = cur
cycle_l = 0

while True:
    cur = mp[cur]
    cycle_l += 1

    if cur == cycle_sp:
        break

rest = (K) % cycle_l
# print("cnt:", cnt)
# print("cycle_l:", cycle_l)
# print("rest:", rest)

while rest != 0:
    cur = mp[cur]
    rest -= 1

print(cur)

Submission status: Wrong Answer

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = set()
cur = 1
cnt = 0

while True:
    cur = mp[cur]
    # cnt += 1
    K -= 1

    if cur in hist:
        break
    else:
        hist.add(cur)

cycle_sp = cur
cycle_l = 0

while True:
    cur = mp[cur]
    cycle_l += 1

    if cur == cycle_sp:
        break

rest = (K) % cycle_l
# print("cnt:", cnt)
# print("cycle_l:", cycle_l)
# print("rest:", rest)

while rest > 0:
    cur = mp[cur]
    rest -= 1

print(cur)

Submission status: Wrong Answer

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = set()
cur = 1
cnt = 0

while True:
    cur = mp[cur]
    cnt += 1

    if cur in hist:
        break
    else:
        hist.add(cur)

cycle_sp = cur
cycle_l = 0

while True:
    cur = mp[cur]
    cycle_l += 1

    if cur == cycle_sp:
        break

rest = (K-cnt) % cycle_l

while rest > 0:
    cur = mp[cur]
    rest -= 1

print(cur)

Submission status: Wrong Answer

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = [1]

while True:

    if not mp[hist[-1]] in hist:
        hist.append(mp[hist[-1]])

    else:
        # found
        cstart = hist.index(mp[hist[-1]])
        cend = len(hist)
        cycle_l = cend - cstart
        # print("hist:", hist)
        # print("cstart:", cstart)
        # print("cend:", cend)
        # print("cycle_l:", cycle_l)

        ans_i = (K - cstart)%cycle_l
        # print("ans_i:", ans_i)
        ans = hist[cstart + ans_i]

        print(ans)
        break

Submission status: Wrong Answer

Solution code:
from collections import deque

N,K = map(int,input().split())
telpo = [int(x) for x in input().split()]
now = 1
loop = deque([now])
count = 0

for i in range(K):
    now = telpo[now-1]
    if now in loop:
        while now != loop[0]:
            loop.popleft()
            count += 1
        break
    else:
        loop.append(now)

remain = (K-count)%len(loop)

for i in range(remain):
    now = telpo[now-1]

print(now)

Submission status: Wrong Answer

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

L = [[] for _ in range(N)]
L[0] = 1
prev = 1
count = 1
flag_inf = False
for i in range(1, N):
    now = A[prev]
    if now == prev:
        flag_inf = True
        ans_inf = now
    elif now == 1:
        break
    else:
        L[i] = now
        prev = now
        count += 1
#        print(L)
L = L[:count]
#print(L)

if flag_inf and K > len(L):
#    print('inf')
    print(ans_inf)
else:
    print(L[K%count])

Submission status: Wrong Answer

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

L = [[] for _ in range(N)]
L[0] = 1
prev = 1
count = 1
flag_inf = False
for i in range(1, N):
    now = A[prev]
    if now == 1:
        break
    elif now == prev:
        flag_inf = True
        ans_inf = now
    else:
        L[i] = now
        prev = now
        count += 1
#        print(L)
L = L[:count]
#print(L)

if flag_inf and K > len(L):
#    print('inf')
    print(ans_inf)
else:
    print(L[K%count])

Submission status: Wrong Answer

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

L = [[] for _ in range(N)]
L[0] = 1
prev = 1
count = 1
flag_inf = False
for i in range(1, N):
    now = A[prev]
    if now == 1:
        break
    elif now == prev:
        flag_inf = True
        ans_inf = now
    else:
        L[i] = now
        prev = now
        count += 1
#        print(L)
L = L[:count]
#print(L)

if flag_inf:
    print(ans_inf)
else:
    print(L[K%count])

Submission status: Wrong Answer

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

L = [[] for _ in range(N)]
L[0] = 1
prev = 1
count = 1
for i in range(1, N):
    now = A[prev]
    if now == 1:
        break
    else:
        L[i] = now
        prev = now
        count += 1
#        print(L)
L = L[:count]
#print(L)
print(L[K%count])

Submission status: Wrong Answer

Solution code:

N,K=map(int,input().split())
A=list(map(int,input().split()))
now=1
count=0
a=0
l=[1]
b=0
c=0
while a==0and count<K:
    now=A[now-1]
    count+=1
    if now in l:
        a=1
    else:
        l.append(now)
        
b=l.index(now)
c=count-b
if count==K:
    print(now)
else:
    n= ((K-b)%c)
    print(b+l[n])

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = (int(i) for i in input().split())  
A = [int(i) - 1 for i in input().split()] 
N = len(A)

def get_keys_from_value(d, val):
    return [k for k, v in d.items() if v == val][0]

now = 0
lis = []
p = {}
for i in range(N):
  now = A[now]
  if now in p:
    break
  
  p[now] = i
  lis.append(now)

if K < i:
  print(A[i] + 1)
else:
  print(lis[(K-i-1) % (i - p[now]) + p[now]] + 1)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
now=1
count=0
a=0
l=[1]
b=0
c=0
while a==0and count<K:
    now=A[now-1]
    count+=1
    if now in l:
        a=1
    else:
        l.append(now)
        
print(l)
b=l.index(now)
c=count-b
if count==K:
    print(now)
else:
    n= ((K-b)%c)
    ncount=0
    while ncount <n:
        now=A[now-1]
        ncount+=1
    print(now)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
now=1
count=0
a=0
l=[1]
b=0
c=0
while a==0and count<K:
    now=A[now-1]
    count+=1
    if now in l:
        a=1
    else:
        l.append(now)
b=l.index(now)
c=count-b+1
if count==K:
    print(now)
else:
    n=int((K-b)/c)
    ncount=0
    while ncount <n:
        now=A[now-1]
        ncount+=1
    print(now)

Submission status: Wrong Answer

Solution code:
def main():
    nums = list(map(int, input().split()))
    n, k = nums[0], nums[1]
    graph = {}
    out = list(map(int, input().split()))
    for i in range(n):
        graph[i + 1] = out[i]
    
    cycle_length = None
    visited = {}
    ans = None

    def go(x, rem):
        while rem:
            x = graph[x]
            rem -= 1
        
        nonlocal ans
        ans = x
        return x

    def dfs(i, rem):
        j = i
        while rem:
            if j in visited:
                nonlocal cycle_length
                cycle_length = visited[j] - rem
                steps = rem % cycle_length
                go(j, steps)
                return
            visited[j] = rem
            rem -= 1
            j = graph[j]

        return j

    dfs(1, k)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split())
v=[0]+[-1]*~-n
p=0
for i in range(n):
  t=a[p]-1
  if v[t]>=0: l,r=v[t],i+1; break
  v[t]=i+1; p=t
print([k,v.index(l+(k-l)%(r-l))][k>l]+1)