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:
#! /usr/bin/env python3

from fractions import gcd
# from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]

flag = 0
done = [0] * (N + 1)

root = []
ind = 0

ind = A[0]
root = [1]
done[1] = 1

while True:
    
    if done[ind]:
        flag = 1
        break
    done[ind] = 1
    
    root += [ind]
    ind = A[ind-1]

if flag:
    left = root.index(ind) - 1
    loopcnt = len(root) - (root.index(ind))

    root = root[left+1:]
    print(root[(K-(left+1)) % loopcnt])
else:
    print(root[:-1])

Submission status: Wrong Answer

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

done = [0] * (N + 1)

root = []
ind = 0

ind = A[0]
root = [1]
done[1] = 1

while True:
    
    if done[ind]:
        break
    done[ind] = 1
    
    root += [ind]
    ind = A[ind-1]

left = root.index(ind) - 1
loopcnt = len(root) - (root.index(ind))

root = root[left+1:]
print(root[(K-(left+1)) % loopcnt])

Submission status: Wrong Answer

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

done = [0] * (N + 1)

root = []
ind = 0

ind = A[0]
root = [1]
done[1] = 1

while True:
    print(ind)
    
    if done[ind]:
        break
    done[ind] = 1
    
    root += [ind]
    ind = A[ind-1]

left = root.index(ind) - 1
loopcnt = len(root) - (root.index(ind))

root = root[left+1:]
print(root[(K-(left+1)) % loopcnt])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [-1 for i in range(N+1)]
B[1] = 0
n = 1
m = 0
while 1:
  n = A[n]
  m += 1
  if m == K:
    break
  if B[n] == -1:
    B[n] = m
  else:
    a = (K - m) % (m - B[n])
    for i in range(a):
      n = A[n]
    print(n)
    break

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A= list(map(int, input().split()))
machi=[0]*N
machi[0]=1
machi[A[0]-1]= 1
tugi=A[0] #next town number 3
count=0
amari=1
while(1):
    tugi=A[tugi-1]#A[2]-1=3
    #print(machi,amari)
    if machi[tugi-1]==1:
        count+=1
        machi[tugi-1]=2
        loopstart=tugi-1
    elif machi[tugi-1]==2:
        break
    else:
        machi[tugi-1]=1
        amari+=1
loop=K-(amari-count)
susumu=loop%count
if susumu==0:
    print(A[loopstart])
else:
    for a in range(susumu):
        kot=A[loopstart]
        loopstart=kot-1
    print(kot)
#print(amari,count,susumu)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A= list(map(int, input().split()))
machi=[0]*N
machi[0]=1
tugi=A[0] #next town number 3
count=0
amari=1
while(1):
    tugi=A[tugi-1]#A[2]-1=3
    #print(machi,amari)
    if machi[tugi-1]==1:
        count+=1
        machi[tugi-1]=2
        loopstart=tugi-1
    elif machi[tugi-1]==2:
        break
    else:
        machi[tugi-1]=1
        amari+=1
loop=K-(amari-count)
susumu=loop%count
if susumu==0:
    print(A[loopstart])
else:
    for a in range(susumu):
        kot=A[loopstart]
        loopstart=kot-1
    print(kot)
#print(amari,count,susumu)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A= list(map(int, input().split()))
machi=[0]*N
machi[A[0]-1]=1
tugi=A[0] #next town number 3
count=0
amari=1
while(1):
    tugi=A[tugi-1]#A[2]-1=3
    if machi[tugi-1]==1:
        count+=1
        machi[tugi-1]=2
        loopstart=tugi-1
    elif machi[tugi-1]==2:
        break
    else:
        machi[tugi-1]=1
        amari+=1
loop=K-(amari-count)
susumu=loop%count
if susumu==0:
    print(A[loopstart])
else:
    for a in range(susumu):
        kot=A[loopstart]
        loopstart=kot-1
    print(kot)

Submission status: Wrong Answer

Solution code:
import copy
N, K = map(int, input().split())
A = list(map(int, input().split()))
routes = [0]
visited = [False] * N
visited[0] = True
now = 0
while True:
    next_town = A[now] - 1
    if visited[next_town]:
        routes.append(next_town)
        break
    visited[next_town] = True
    routes.append(next_town)
    now = next_town
for i, r in enumerate(routes):
    if r == routes[-1]:
        loop = copy.deepcopy(routes[i:])
        break
routes.pop()
loop.pop()
if len(routes) > K:
    print(routes[K])
else:
    K -= len(routes)
    K %= len(loop)
    print(loop[K] + 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()
  p=set()
  for j in range(N+1):
    if L[i-1] in p:
      c=R.index(L[i-1])
      t=len(R)-c
      m=R[c:]#ループ
      break
    R.append(L[i-1])
    p.add(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,K=map(int,input().split())
L=list(map(int,input().split()))
A=list()
start=1
for i in range(N+2):
  start=L[start-1]
  if start in A:
    s=A.index(start)
    syuuki=i-s
    break
  A.append(start)
if K<=s:
  print(A[K-1])
else:
  K=K-i
  K=K%syuuki
  K+=i
  K=K%syuuki
  print(A[K-1])

Submission status: Wrong Answer

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

now=0
visited=set()
visited.add((0))
visited2=[0]


while True:
  k-=1
  now=a[now]-1

  if k==0:
    print(now-1)
    exit()
    
  
  if now in visited:
    visited.add((now))
    visited2.append(now)
    break

  visited.add((now))
  visited2.append(now)


xxx=visited2[-1]
cnt=[]
for i in range(len(visited2)):
  if visited2[i]==xxx:
    cnt.append(i)


k%=cnt[1]-cnt[0]


if k!=0:
  for i in range(k):
    now=a[now]-1
  

print(now+1)

Submission status: Wrong Answer

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

now=0
visited=set()
visited.add((0))
visited2=[0]


while True:
  k-=1
  now=a[now]-1
  
  if now in visited:
    visited.add((now))
    visited2.append(now)
    break

  visited.add((now))
  visited2.append(now)


xxx=visited2[-1]
cnt=[]
for i in range(len(visited2)):
  if visited2[i]==xxx:
    cnt.append(i)


k%=cnt[1]-cnt[0]


if k!=0:
  for i in range(k):
    now=a[now]-1
  

print(now+1)

Submission status: Wrong Answer

Solution code:
# import itertools
# import math
import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
N, K = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

A.insert(0, 0)
cnt = [0] * (N + 1)

j = 1
times_s = 0
times_e = 0
cycle = 0
for i in range(N + 1):
    if i == K:
        print(j)
        sys.exit()
    cnt[j] += 1
    if cnt[j] == 2:
        times_e = i
        cycle = j
        break
    j = A[j]

j = 1
for i in range(N + 1):
    if j == cycle:
        times_s = i
        break
    j = A[j]

print(A, cnt)

print(times_s, times_e, cycle)

K_eff = (K - times_s) % (times_e - times_s)

for i in range(N + 1):
    if i == K_eff:
        print(j)
        sys.exit()
    j = A[j]

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
s = [1]
x = 1
while a[x-1] not in s:
    s.append(a[x-1])
    x = a[x-1]
    
if a[s[-1]-1] != 1:
    t = a[s[-1]-1]
    cnt = 0
    for i in s:
        cnt += 1
        del s[0]
        if i == t:
            break
    k -= cnt

l = len(s)
print(s[k%l])

Submission status: Wrong Answer

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

num = 0
li = [0]
flag = [True] * n
flag[0] = False

while True:
    num = a[num]
    if flag[num]:
        li.append(num)
        flag[num] = False
    else:
        break

d = li.index(num)
ans = (k-d)%(len(li)-d)+d
print(li[ans]+1)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,K=map(int,readline().split())
A=list(map(lambda x: int(x)-1,readline().split()))

visited=[True]*N
visitednum=[]
now=0
cnt=0
while True:
    if not visited[now]:
        break
    else:
        visited[now]=False
        visitednum.append(now)
        now=A[now]
        cnt+=1
d=visitednum.index(now)
ans=(K-d)%(len(visitednum)-d)+d
print(visitednum[ans]+1)

Submission status: Wrong Answer

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

visited = [False for _ in range(N)]
visited[0] = True
pattern = [0]
x = 0
start = 0
patternBegin = 0
while True:
    x = A[x]
    if visited[x]:
        patternBegin = x
        break
    pattern.append(x)
    visited[x] = True

print(pattern)
patternBeginIndex = pattern.index(patternBegin)
beforeLoop = pattern[0:patternBeginIndex]
repeat = pattern[patternBeginIndex:]
print(beforeLoop, repeat)

if len(pattern) > K:
    print(pattern[K]+1)
else:
    ans = repeat[(K - len(beforeLoop))%len(repeat)]
    # print(pattern)
    print(ans+1)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
L = list(map(int,input().split()))
i = 1
j = 0
li = [1]
while j <=2*n:
    li.append(L[i-1])
    i = L[i-1]
    j+=1
#print(li)

for i in range(2*n):
    j = i+1
    p = ()
    while j <=2*n-1:
        if li[i] == li[j]:
            p = (i,j)
            #print(p)
            break
        j +=1
    else:
        continue
    break
    #if p:
        #break
ini = p[0]
interval = p[1]
a = (m-ini)%(p[1]-p[0])
b = ini + a
print(li[b])

Submission status: Wrong Answer

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

pattern = [0]
x = 0
start = 0
patternBegin = 0
while True:
    x = A[x]
    if x in pattern:
        patternBegin = x
        break
    pattern.append(x)

patternBeginIndex = pattern.index(patternBegin)
extra = pattern[0:patternBeginIndex]
repeat = pattern[patternBeginIndex:]

# print(extra, repeat)

if len(pattern) > K:
    print(pattern[K]+1)
else:
    ans = pattern[(K - len(extra)-1)%len(repeat)]
    # print(pattern)
    print(ans+1)

Submission status: Wrong Answer

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

current = 0
d = dict()
d[current] = d.get(current, 0) + 1
pattern = [0]
cnt = K

while True:
    current = A[current]
    if d.get(current) == 2:
        break
    else:
        if d.get(current) == None:
            d[current] = d.get(current, 0) + 1
        else:
            d[current] = d.get(current) + 1
        pattern.append(current)
        cnt -= 1
        if cnt == 0:
            break

if cnt == 0:
    print(pattern[-1]+1)
else:
    loop = [key for key, value in d.items() if value == 2]
    res = len(pattern) % len(loop)
    pattern = pattern[res:]
    ans = len(pattern) % len(loop)

    print(pattern[ans]+1)

Submission status: Wrong Answer

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

current = 0
d = dict()
d[current] = d.get(current, 0) + 1
pattern = [0]
cnt = K

while True:
    current = A[current]
    if d.get(current) == 2:
        break
    else:
        if d.get(current) == None:
            d[current] = d.get(current, 0) + 1
        else:
            d[current] = d.get(current) + 1
        pattern.append(current)
        cnt -= 1
        if cnt == 0:
            break

if cnt == 0:
    print(pattern[-1]+1)
else:
    loop = [key for key, value in d.items() if value == 2]
    res = len(pattern) % len(loop)
    ans = (K - res) % len(loop)

    print(pattern[res+ans]+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_ls = list(map(int, input().split()))
for i in range(n):
    a_ls[i] -= 1
times_town = [0] * (n+1)
town_done = [0] * n
town_times = [0] * n
times = 0

now_town = 0
while True:
    # きたtownに関する処理
    town_done[now_town] = 1
    town_times[now_town] = times
    times_town[times] = now_town
    
    # 次の移動
    now_town = a_ls[now_town]
    times += 1
    if town_done[now_town]:
        break

times_before_loop = town_times[now_town]
times_per_loop = times - times_before_loop
k -= times_before_loop
r = k % times_per_loop
print(times_town[times_before_loop+r]+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_ls = list(map(int, input().split()))
done_ls = [0] * n
#0-idxで処理
nex = a_ls[0]
times = 0
mod_town = [0] * n
while True:
    nex -= 1
    mod_town[times] = nex+1
    times += 1
    if done_ls[nex]:
        break
    done_ls[nex] = 1
    nex = a_ls[nex]
mod = k % times
print(mod_town[mod])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_ls = list(map(int, input().split()))
done_ls = [0] * n
#0-idxで処理
nex = a_ls[0]
times = 0 
mod_town = [0] * n
while True:
    nex -= 1
    mod_town[times] = nex+1
    times += 1
    if done_ls[nex]:
        break
    done_ls[nex] = 1
    nex = a_ls[nex]
mod = k % times
print(mod_town[mod])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
i=l=1
l=0
F=[0]*n
L=[]
while 1:
  F[i-1]=l
  i=A[i-1]
  if F[i-1]>0:
    break
  else:
    L.append(i)
    F[i-1]=l
    l+=1
print((L[F[i-1]-1:])[(k-F[i-1])%(l-F[i-1]+1)])

Submission status: Wrong Answer

Solution code:
"TLE"

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

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

A_dir_list = []
A_visit_list = [-1 for i in range(N)]
dir = 0

while True:
    A_dir_list.append(dir)
    A_visit_list[dir] = 1
    if A_visit_list[A_list[dir]-1] == -1: 
        dir = A_list[dir]-1
    else:
        cycle_pos = A_dir_list.index(A_list[dir]-1)
        break

cnt = (K-cycle_pos)%(len(A_dir_list)-cycle_pos)
print(A_dir_list[cnt+cycle_pos]+1)

Submission status: Wrong Answer

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

A.insert(0,0)

start = 1
next_ = A[1]

visited = [0] * (N+1)
visited[start] = 1

route = [1]

while True:
    if visited[next_] == 1:
        break
    visited[next_] = 1
    route.append(next_)
    next_ = A[next_]

roop_s = route.index(next_)
roop = route[roop_s:]

stop = (K - roop_s) % len(roop) + 1
ans = roop[stop-1]

print(ans)

Submission status: Wrong Answer

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

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

A_dir_list = []
dir = 0

while True:
    A_dir_list.append(dir)
    if A_list[dir]-1 not in A_dir_list:
        dir = A_list[dir]-1
    else:
        cycle_pos = A_dir_list.index(A_list[dir]-1)
        break

cnt = (K-cycle_pos)%(len(A_dir_list)-cycle_pos)
print(A_dir_list[cnt+cycle_pos]+1)

Submission status: Wrong Answer

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

roop_num = 1
city = 0
first_city = 0

while True:
    next_city = A[city] - 1
    if next_city == first_city:
        break
    if roop_num == K:
        print(city + 1)
        exit()
    city = next_city
    roop_num += 1

rem_num = K % roop_num
city = 0
num = 0

while True:
    next_city = A[city] - 1
    if num == rem_num:
        print(city + 1)
        exit()
    city = next_city
    num += 1

Submission status: Wrong Answer

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

roop_num = 1
city = 0
first_city = 0

while True:
    next_city = A[city] - 1
    if next_city == first_city:
        break
    if roop_num == K:
        print(city + 1)
        exit()
    city = next_city
    roop_num += 1

rem_num = K % roop_num
city = 0
num = 0

while True:
    next_city = A[city] - 1
    if num == rem_num:
        print('answer', city + 1)
        exit()
    city = next_city
    num += 1

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*N
point=1
m,l=0,0#mは循環する町の数、lはそれまでの町の数
for i in range(N):
    if B[point-1]==0:
        B[point-1]=i+1
        point=A[point-1]
    else:
        m=i+1-B[point-1]
        l=B[point-1]
        break
if m!=0:
    m=(K-l)%m
else:
    if K>N:
        m=N
    else:
        m=K
ans=1
for i in range(l+m):
    ans=A[ans-1]

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*N
point=1
m,l=0,0#mは循環する町の数、nはそれまでの町の数
for i in range(N):
    if B[point-1]==0:
        B[point-1]=i+1
        point=A[point-1]
    else:
        m=i+1-B[point-1]
        l=B[point-1]
        break
if m!=0:
    m=(K-l)%m
else:
    m=N
ans=1
for i in range(l+m):
    ans=A[ans-1]

print(ans)

Submission status: Wrong Answer

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

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

check = [0]*N

jump = 0

count = 0

while check[A[jump]-1] == 0 and count <= K:
    n_jump = A[jump]-1
    check[n_jump] = check[jump]+1
    count += 1
    jump = n_jump

if count == K:
    key = max(check)
    ans = check.index(key)+1
else:
    count2 = 0
    key = max(check)
    jump = check.index(key)
    check2 = [0]*N
    while check2[A[jump]-1] == 0:
        n_jump = A[jump]-1
        check2[n_jump] = check2[jump]+1
        count2 += 1
        jump = n_jump
    amari = (K-count)%count2
    if amari == 0:
        key = max(check2)
        ans = check2.index(key)+1
    else:
        ans = check2.index(amari)+1
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.insert(0,0)
B = {1:1}
x = A[1]
st = 0
while True:
    if x not in B:
        B[x] = 1
        x = A[x]
    else:
        st = x
        break
x = 1
t = 0
while x!=st:
    x = A[x]
    t += 1
x = st
x = A[x]
T = 1
while x!=st:
    x = A[x]
    T += 1
if K<=t:
    x = 1
    cnt = 0
    while cnt<t:
        x = A[x]
        cnt += 1
    print(x)
else:
    K -= t
    K = K%T
    x = st
    cnt = 0
    while cnt<K:
        x = A[x]
        cnt += 1
    print(x)

Submission status: Wrong Answer

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

cnt = 0
cl = [-1]*(n+1)
cll = [1]
curr = 1
for i in range(n):
    # print(curr)
    next_c = al[curr-1]
    if cl[next_c] != -1:
        # loop_start = (i+1)
        loop_start = cl[next_c]
        rem = k - (i+1)
        break
    else:
        cl[next_c] = i + 1
        cll.append(next_c)
    curr = next_c

else:
    pass

# print(rem)
loop_c = cll[loop_start:]
# print(loop_c)
last_ind = rem%len(loop_c)
ans = loop_c[last_ind]
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*N
point=1
m,n=0,0
for i in range(N):
    if B[point-1]==0:
        B[point-1]=i+1
        point=A[point-1]
    else:
        m=i+1-B[point-1]
        n=B[point-1]
        break
if m==0:
    m=N
m=(K-n)%m
ans=1
for i in range(n+m):
    ans=A[ans-1]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
m = [1, a[0]]
seen = [False] * n
seen[0] = True
que = deque()
que.append(a[0])
n = 0
while que:
    n = que.pop()
    if seen[n - 1] is False:
        seen[n - 1] = True
        if a[n - 1] not in m:
            que.append(a[n - 1])
            m.append(a[n - 1])
    else:
        break
d = m.index(a[n - 1])
ans = (k - d) % (len(m) - d) + d
print(m[ans])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]*N
point=1
m,n=0,0
for i in range(N):
    if B[point-1]==0:
        B[point-1]=i+1
        point=A[point-1]
    else:
        m=i+1-B[point-1]
        n=B[point-1]
        break
m=(K-n)%m
ans=1
for i in range(n+m):
    ans=A[ans-1]
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
from collections import defaultdict
import sys
sys.setrecursionlimit(10**7)
def dfs1(src):
    global steps
    if steps==rem:return src
    steps+=1
    return dfs1(g[src][0])
def dfs(src):
    global steps,done,boc,nic
    vis[src]=1
    steps+=1
    dfn[src]=steps
    for neigh in g[src]:
        if not vis[neigh]:dfs(neigh)
        else:
            boc,done,nic=[neigh,steps,steps-dfn[neigh]+1]
            # print(done,boc,nic)
            return

n,k=list(map(int,stdin.readline().split()))
done,boc,nic=0,0,0
a=list(map(int,stdin.readline().split()))
g=defaultdict(list)
for i in range(1,n+1):
    g[i]+=[a[i-1]]
vis=[0]*(n+1)
dfn=[0]*(1+n)
steps=0
dfs(1)
# print(done,boc,nic)
left=k-done
rem=(left%nic)
if rem==0:rem=nic
steps=1
print(dfs1(g[boc][0]))

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
from collections import defaultdict
import sys
sys.setrecursionlimit(10**7)
def dfs1(src):
    global steps
    if steps>rem:return src
    steps+=1
    return dfs1(g[src][0])
def dfs(src):
    global steps,done,boc,nic
    vis[src]=1
    steps+=1
    dfn[src]=steps
    for neigh in g[src]:
        if not vis[neigh]:dfs(neigh)
        else:
            boc,done,nic=[neigh,steps,steps-dfn[neigh]+1]
            # print(done,boc,nic)
            return

n,k=list(map(int,stdin.readline().split()))
done,boc,nic=0,0,0
a=list(map(int,stdin.readline().split()))
g=defaultdict(list)
for i in range(1,n+1):
    g[i]+=[a[i-1]]
vis=[0]*(n+1)
dfn=[0]*(1+n)
steps=0
dfs(1)
# print(done,boc,nic)
left=k-done
rem=(left%nic)
if rem==0:rem=nic
steps=1
print(dfs1(boc))

Submission status: Wrong Answer

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

l=K%N
ans=1
for i in range(l):
  ans=A[ans-1]
  
print(ans)

Submission status: Wrong Answer

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

cnt = {}
for i in range(1, N+1):
    cnt[i] = 0

root = [0]
loop = []
now = 1
tmp = 0
amari = 0
while cnt[now] != 2:
    cnt[now] += 1
    root.append(now)
    if cnt[now] == 2:
        tmp = now
        break
    amari += 1
    now = A[now]

while cnt[tmp] == 2:
    loop.append(tmp)
    tmp = A[tmp]
    cnt[tmp] += 1
    
amari -= len(loop)

if K <= amari:
    ans = root[K]
else:
    ind = amari + (K-amari)%len(loop) + 1
    ans = root[ind]
    
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def input():
  return sys.stdin.readline()


N,K = map(int,input().split())
A=list(map(int,input().split()))
P=[True]*N
P[0]=False
i,a=1,0
while 1:
  a+=1
  if P[A[i-1]-1]:
    P[A[i-1]-1]=False
    i=A[i-1]
  else:
    break
if a>=K:
  i=1
  for _ in range(K):
    i=A[i-1]
  print(A[i-1])
else:
  K-=a
  a,j=0,i
  while 1:
    a+=1
    if A[j-1]==i:
      break
    j=A[j-1]
  K%=a
  for _ in range(K):
    i=A[i-1]
  print(A[i-1])

Submission status: Wrong Answer

Solution code:
import sys

def input():
  return sys.stdin.readline()


N,K = map(int,input().split())
A=list(map(int,input().split()))
P=[True]*N
P[0]=False
i,a=1,0
while 1:
  a+=1
  if P[A[i-1]-1]:
    P[A[i-1]-1]=False
    i=A[i-1]
  else:
    break
if a>=K:
  i=1
  for _ in range(K):
    i=A[i-1]
else:
  K-=a
  a,j=0,i
  while 1:
    a+=1
    if A[j-1]==i:
      break
    j=A[j-1]
  K%=a
  for _ in range(K):
    i=A[i-1]
  print(A[i-1])

Submission status: Wrong Answer

Solution code:
import sys

def input():
  return sys.stdin.readline()


N,K = map(int,input().split())
A=list(map(int,input().split()))
P=[True]*N
P[0]=False
i,a=1,0
while 1:
  a+=1
  if P[A[i-1]-1]:
    P[A[i-1]-1]=False
    i=A[i-1]
  else:
    break
if a>=K:
  print(A[K])
else:
  K-=a
  a,j=0,i
  while 1:
    a+=1
    if A[j-1]==i:
      break
    j=A[j-1]
  K%=a
  for _ in range(K):
    i=A[i-1]
  print(A[i-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def input():
  return sys.stdin.readline()


N,K = map(int,input().split())
A=list(map(int,input().split()))
L=deque([1])
i=1
for _ in range(N):
  i=A[i-1]
  if i in L:
    p=i
    break
  else:
    L.append(i)
L=list(L)
n,p=len(L),L.index(p)
if p>=K:
  print(L[K-1])
else:
  K-=p
  K%=len(L[p:])
  print(L[K+p])

Submission status: Wrong Answer

Solution code:
import sys

def input():
  return sys.stdin.readline()

N,K = map(int,input().split())
A=list(map(int,input().split()))
L=[1]
i=1
for _ in range(N):
  i=A[i-1]
  if i in L:
    p=i
    break
  else:
    L.append(i)
n,p=len(L),L.index(p)
if p>=K:
  print(L[p])
else:
  K-=p
  K%=len(L[p:])
  print(L[K+p])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A=list(map(int,input().split()))
L=[1]
i=1
for _ in range(N):
  i=A[i-1]
  if i in L:
    p=i
    break
  else:
    L.append(i)
n,p=len(L),L.index(p)
if p>=K:
  print(L[p])
else:
  K-=p
  K%=len(L[p:])
  print(L[K+p])

Submission status: Wrong Answer

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

color=["white" for _ in range(n+1)]
DP,index=[],1

while 1:
    if color[index]=="white":color[index]="gray"
    elif color[index]=="gray":color[index]="black"
    DP.append(A[index])
    index=A[index]
    if color[index]=="black":break

repeat=[]
for i in DP:
    if color[i]=="black":
        repeat.append(i)
        if repeat[0]==i and len(repeat)!=1:
            repeat.pop()
            break
length=len(repeat)
cnt=0
for i in DP:
    if repeat[0]==i:break
    cnt +=1

if k<=cnt+length:
    print(DP[k])
else:
    k -=cnt
    k %=length
    print(repeat[k-1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l = [-1]+list(map(int,input().split()))

visited = [-1]+[False]*n

circuits = []
i = 1
while True:
    if visited[i]:
        fragrant = i
        break
    else:
        visited[i] = True
        i = l[i]

i = fragrant
cnt = 1
while l[i] != fragrant:
    cnt += 1
    i = l[i]
# print(cnt)

k %= cnt
i = 1
while k > 0:
    i = l[i]
    k -= 1
print(i)

Submission status: Wrong Answer

Solution code:
import heapq

def binary_search(data, value):
    left = 0            # 探索する範囲の左端を設定
    right = len(data) - 1            # 探索する範囲の右端を設定
    while left <= right:
        mid = (left + right) // 2            # 探索する範囲の中央を計算
        if data[mid] == value:
            # 中央の値と一致した場合は位置を返す
            return mid
        elif data[mid] < value:
            # 中央の値より大きい場合は探索範囲の左を変える
            left = mid + 1
        else:
            # 中央の値より小さい場合は探索範囲の右を変える
            right = mid - 1
    return -1            # 見つからなかった場合

n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
way = [0]
hq = [0]
heapq.heapify(hq)
posi = 0
cycle = 0
while True:
    if binary_search(hq, a[posi]) != -1:
        rest = way.index(a[posi])
        cycle -= (rest - 1)
        break
    way.append(a[posi])
    heapq.heappush(hq, a[posi])
    posi = a[posi]
    cycle += 1
# print(way)
# print(cycle)
if k < len(way):
    print(way[k])
else:
    print(way[(k-rest) % cycle + rest] + 1)

Submission status: Wrong Answer

Solution code:
import heapq

def binary_search(data, value):
    left = 0            # 探索する範囲の左端を設定
    right = len(data) - 1            # 探索する範囲の右端を設定
    while left <= right:
        mid = (left + right) // 2            # 探索する範囲の中央を計算
        if data[mid] == value:
            # 中央の値と一致した場合は位置を返す
            return mid
        elif data[mid] < value:
            # 中央の値より大きい場合は探索範囲の左を変える
            left = mid + 1
        else:
            # 中央の値より小さい場合は探索範囲の右を変える
            right = mid - 1
    return -1            # 見つからなかった場合

n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
way = [0]
hq = [0]
heapq.heapify(hq)
posi = 0
cycle = 0
while True:
    if binary_search(hq, a[posi]) != -1:
        rest = way.index(a[posi])
        cycle -= (rest - 1)
        break
    way.append(a[posi])
    heapq.heappush(hq, a[posi])
    posi = a[posi]
    cycle += 1
# print(way)
# print(cycle)
if k < rest:
    print(way[k])
else:
    print(way[(k-rest) % cycle + rest] + 1)

Submission status: Wrong Answer

Solution code:
import heapq

def binary_search(data, value):
    left = 0            # 探索する範囲の左端を設定
    right = len(data) - 1            # 探索する範囲の右端を設定
    while left <= right:
        mid = (left + right) // 2            # 探索する範囲の中央を計算
        if data[mid] == value:
            # 中央の値と一致した場合は位置を返す
            return mid
        elif data[mid] < value:
            # 中央の値より大きい場合は探索範囲の左を変える
            left = mid + 1
        else:
            # 中央の値より小さい場合は探索範囲の右を変える
            right = mid - 1
    return -1            # 見つからなかった場合

n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
way = [0]
hq = [0]
heapq.heapify(hq)
posi = 0
cycle = 0
while True:
    if binary_search(hq, a[posi]) != -1:
        rest = way.index(a[posi])
        cycle -= (rest - 1)
        break
    way.append(a[posi])
    heapq.heappush(hq, a[posi])
    posi = a[posi]
    cycle += 1
# print(way)
# print(cycle)
print(way[(k-rest) % cycle + rest] + 1)

Submission status: Wrong Answer

Solution code:
import heapq

def binary_search(data, value):
    left = 0            # 探索する範囲の左端を設定
    right = len(data) - 1            # 探索する範囲の右端を設定
    while left <= right:
        mid = (left + right) // 2            # 探索する範囲の中央を計算
        if data[mid] == value:
            # 中央の値と一致した場合は位置を返す
            return mid
        elif data[mid] < value:
            # 中央の値より大きい場合は探索範囲の左を変える
            left = mid + 1
        else:
            # 中央の値より小さい場合は探索範囲の右を変える
            right = mid - 1
    return -1            # 見つからなかった場合

n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
way = [0]
hq = [0]
heapq.heapify(hq)
posi = 0
cycle = 0
while True:
    if binary_search(hq, a[posi]) != -1:
        rest = way.index(a[posi])
        cycle -= (rest - 1)
        break
    way.append(a[posi])
    heapq.heappush(hq, a[posi])
    posi = a[posi]
    cycle += 1
# print(way)
# print(cycle)
print(way[k%cycle] +1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
b = [1]
now = A[0]
i = 1
while True:
	if K == 0:
		print(A[now-1])
		exit()
	if A[now-1] in set(b):
		x = A[now-1]
		break
	b.append(A[now-1])
	i += 1
	K -= 1
diff = i - b.index(x)

print(b[b.index(x)+(K%diff)])