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,list(input().split())))

exist = [a[0]]
ex = {}
ex[a[0]] = 0

cnt = 1
while(True):
    if a[exist[-1]-1] in ex:        
        idx = exist.index(a[exist[-1]-1])
        exist.append(a[exist[-1]-1])
        break
    ex[a[exist[-1]-1]] = 0
    exist.append(a[exist[-1]-1])
    
    cnt += 1
    if cnt >= k-1:
        print(exist[-1])
        exit()


#k = k-idx
#print(idx,exist,cnt)
d = (k-idx) % (cnt-idx)

print(exist[d+idx-1])

Submission status: Wrong Answer

Solution code:
l = list(map(int, input().split(' ')))
n = l[0]
k = l[1]
l = list(map(int, input().split(' ')))
index = l[0]-1
e = l[index]-1
lindex = [0]
while True: 
    lindex += [index]
    if lindex.index(index)!=len(lindex)-1-lindex[::-1].index(index):
        break
    index = e
    e = l[index]-1
tloopsize = len(lindex)-lindex[::-1].index(index)-1-lindex.index(index)
k = k%tloopsize

index = 0
e = l[index]-1
while k>0:
    k-=1
    index = e
    e = l[index]-1
print(index+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
from sys import stdin


def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    As = [int(x) for x in stdin.readline().rstrip().split()]
    pos = 1
    tmp = deque([pos])
    while True:
        pos = As[pos - 1]
        if pos in tmp:
            idx_s = tmp.index(pos)
            break
        tmp.append(pos)
    K = K - idx_s
    tmp = list(tmp)
    print(tmp[idx_s:][K % (len(tmp) - idx_s)])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
List=list(map(int,input().split()))
s=[1]
point=1

while True:
    point=List[point-1]
    if point not in s:
        s.append(point)
    else:
        p=0
        for i in range(n):
            if s[i]!=point:
                p+=1
            else:
                break
        break

x=len(s[p:])
count=len(s)

b=(k-count+1)%x

if b==0:
    q=len(s)
    c=s[q-1]
else:
    c=s[p+b-1]

print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a=[i-1 for i in a]
memo = [1]+[0]*(n-1)
telep=0
now=0
point=-1
while telep<k:
  telep+=1
  now=a[now]
  if memo[now]==0:
    memo[now]+=1
  else:
    point=now
    break
if point==-1:
  print(now+1)
  exit()

now=0
t=-1
while True:
  if now==point and t==-1:
    t=0
  now=a[now]
  if t>=0:
    t+=1
  if now==point and t!=-1:
    cycle=t
    break
k %= cycle
telep=0
now=0
while telep<k:
  telep+=1
  now=a[now]
print(now+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
l = [1]
next = s[0]
flag = 0
for i in range(k):
    l.append(next)
    if s[next-1] in l:
        flag = 1
        break
    next = s[next-1]
if flag==1:
    key = len(l)-l.index(s[next-1])
    key2 = (k-l.index(s[next-1]))%key

else:
    print(l[-2])

Submission status: Wrong Answer

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

cnt = 0
x = 1
for i in range(N):
    y = A[x-1]
    cnt += 1
    if y == 1:
        break
    x = y
    
x = 1
for i in range(K% cnt):
    y = A[x-1]
    x = y
print(y)

Submission status: Wrong Answer

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

keys = range(1,n+1)
values = [0]*n
dic = dict(zip(keys,values))
dic[1] = 1
t = 1
i = 2

while(True):
    t = l[t-1]
    if dic[t] != 0:
        m  = dic[t]
        d = i-m
        r = (k-i)%d
        ans = l[m+r]
        break
    dic[t] = i
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for i,l in enumerate(li):
  li[i]-=1

done = [0]*n
root = []
que  = deque()

done[0] = 1
root.append(0)
que.append(li[0])


while(len(que)!=0 and len(root)<k+1):
  v = que.pop()
  
  if done[v]!=0:
    root.append(v)
    break

  done[v] = 1
  root.append(v)
  que.append(li[v])

print(root)

if len(root)==k+1:
  print(root[-1]+1)
else:
  fidx = root.index(root[-1])
  lidx = len(root)-1
  lp   = lidx-fidx

  cnt = (k-fidx)%lp

  if cnt==0:
    print(root[-1]+1)
  else:
    que.append(root[-1])
    for i in range(cnt):
      v = que.pop()
      que.append(li[v])
    print(que.pop()+1)

Submission status: Wrong Answer

Solution code:
import sys 
n,m=list(map(int,input().split(" ")))
sys.setrecursionlimit(10**6) 
arr=list(map(int,input().split(" ")))
d={}
for i in range(n):
	d[i]=[]
for i in range(n):
	d[i].append(arr[i]-1)

from collections import deque
def bfs(start,n,visited,m):
	# count=0

	# l2=[i+1]
	q=[start]
	q=deque(q)
	# count+=1
	moves=[]
	moves.append(start)
	visited[start]=1
	left=-1
	fl=0
	while (len(q)!=0 and fl==0):
		y=d[q[0]]
		for j in y:
			if(visited[j]==0):
				moves.append(j)
				visited[j]=1
				# l2.append(j)
				q.append(j)

				# count+=1
			else:
				left=j
				fl=1
				break



		q.popleft()
	# print(left,moves)
	if(left!=-1):
		pre=[]
		post=[]
		for i in range(len(moves)):
			if(moves[i]==left):
				pre=moves[:i]
				post=moves[i:]
				break
		if(m<=len(pre)):
			print(pre[m-1]+1)
		else:
			m-=len(pre)

			print(post[m%len(post)]+1)


	else:
		# curr=0

		print(pre[m%len(pre)]+1)
visited=[0]*n
bfs(0,n,visited,m)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]

    done = [int()]*(2*10**5)
    done[0] = 1
    i = 1
    q = A[0]
    while q not in done:
        done[i] = q
        i+=1
        q = A[q-1]
        # print("q: {} i:{}".format(q,i))

    f_q = done.index(q)
    # print("i :{} r:{}".format(i,f_q))
    r = i - f_q
    # print(r)
    if K <= i:
        t = K
    else:
        k = K - f_q
        k = k % r
        t = k + f_q
        

    q = A[0]
    for _ in range(t-1):
        q=A[q-1]
    print(q)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def search(now, teleporter, limit):
    visit = [-1] * len(teleporter)
    cnt = 0
    
    while visit[now] == -1:
        if limit <= cnt:
            break
        
        visit[now] = cnt
        cnt += 1
        now = teleporter[now]
        
    
    return now, cnt, cnt - visit[now]
    

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

start, initial_cnt, repeat_num = search(0, a, 10**5 + 1)

#print(start, initial_cnt, repeat_num)

r = (k - initial_cnt) % repeat_num

#print(r)

ans, _, _ = search(start, a, r)

print(ans + 1)

Submission status: Wrong Answer

Solution code:
import math
from collections import defaultdict


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

    visited = [-1 for _ in range(N)]
    visited[0] = 0

    current = 0
    th = -1
    per_loop = -1
    for i in range(1, N):
        current = A[current] - 1

        if visited[current] >= 0:
            # print(i, current, visited[current])
            per_loop = i - visited[current]
            th = visited[current]
            break
        else:
            visited[current] = i

    current = 0
    # print(th, per_loop)
    # print(th + (K - th) % per_loop)

    if th < 0:
        num_rep = K
    else:
        num_rep = th + (K - th) % per_loop

    for i in range(num_rep):
        current = A[current] - 1

    print(current + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
li=[]
ans=1

for i in range(k):
    ans=a[ans-1]
    
    if ans not in li:
        li.append(ans)
    else:
        break
else:
    print(ans)
    exit()


li_index=li.index(ans)
t=len(li)-li_index
k-=li_index
k%=t

#print(li_index,t,k)
print(li[li_index+k-1])

#print(ans)
#print(li)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
arr=list(map(int,input().split()))
rem=k%n
place=arr[rem-1]
print(arr[place-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
flag=[False]*n
flag[0]=0
x,count=1,0
for i in range(1,n+1):
  x=a[x-1]
  if flag[x-1]==False:
    flag[x-1]=i
  else:
    l=i-flag[x-1]
    count=flag[x-1]-1
    break
k=(k-count)%l
for _ in range(i-1,i+k+1):
  x=a[x-1]
print(x)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
idx_appearence = [False] * n
idx_appearence[0] = True
arrival_idx = a[0] - 1
idx_appearence[arrival_idx] = True
idx_lst = [0, arrival_idx]
for i in range(n):
    arrival_idx = a[arrival_idx] - 1
    if idx_appearence[arrival_idx] == False:
        idx_lst.append(arrival_idx)
        idx_appearence[arrival_idx] = True
    else:
        idx_lst.append(arrival_idx)
        break
former_part_len = idx_lst.index(idx_lst[-1])
recursive_len = len(idx_lst) - 1 - former_part_len
print(idx_lst[former_part_len + ((k - former_part_len) % recursive_len)] + 1)

Submission status: Wrong Answer

Solution code:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
#dt = {k:v for k,v in sorted(x.items(), key=lambda i: i[1])}
ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda :int(input())
ip = lambda :[int(w) for w in input().split()]
mp = lambda :map(int,input().split())

n,k = mp()
x = ip()
if x[0] == 1:
    print(1)
    exit()
cyc = [1]
vis = {}
i = 1
while not vis.get(i,0):
    vis[i] = 1
    i = x[i-1]
    cyc.append(i)
#print(cyc)
last = cyc[-1]
ind = cyc.index(last)
kk = ind
k -= kk
ind = (k-1) % (len(cyc)-kk-1) + 1
print(cyc[kk+ind])

Submission status: Wrong Answer

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

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

cnt=0
now=1
a =[-1]*n
for i in range(n):
        if now in a:
                for j in range(n):
                        if a[n-1-j]==now:
                                point=n-1-j
                                break
                break
        else:
                a[i]=now
                now=li[now-1]
                cnt+=1

rest=(k-cnt)%(cnt-point)
print(a[point+rest])

Submission status: Wrong Answer

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

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

ls = [1]

for i in range(K):
    if A[ls[i]-1] in ls:
        loop = len(ls) - len(ls[:ls.index(A[ls[i]-1])])
        K -= len(ls[:ls.index(A[ls[i]-1])])
        q = ls.index(A[ls[i]-1])
        ls = ls[q:q+loop]
        ans = K % loop
        print(ls[ans])
        break
    ls.append(A[ls[i]-1])

Submission status: Wrong Answer

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

root = [1]
for n in range(N):
  new = A_list[ list(root)[n]-1]
  if new not in root:
    root.append(new)
  else:
    ind = root.index(new)
    break
    
pre = len(root[:ind])
roop = len(root)-pre

print(root[ind:][(K-pre)%roop])

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    D = [0] * (n+1)
    now = 1
    for i in range(1, k+1):
        tmp = A[now-1]
        if not D[tmp]:
            now = tmp
            D[now] += i
        else:
            mod = D[tmp] - 1
            loop = i - D[tmp]
            print(D.index((k - mod) % loop + mod))
            return
    print(now)
main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
li=list(map(int,input().split()))
H=[0]*(n)
walk=[]
x=1
l=0
while l==0:
    walk.append(x)
    if H[x-1]==0:
        H[x-1]=1
    else:
        dd=[i for i, j in enumerate(walk) if j == x ]
        l=1
    x=li[x-1]
ran=dd[1]-dd[0]
k-=dd[0]
kk=k%ran
print(walk[dd[0]+kk])

Submission status: Wrong Answer

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n, k =MAP()

a = LIST()

# vis = [False]*n
# idx = 0
# temp = [a[0]-1]
# vis[a[0]-1] =True
i=0
j = 1
dd = defaultdict(int)
dd[1] = 0
while(True):
    if(a[i] in dd):
        pos = dd[a[i]]
        break
    dd[a[i]] = j
    i = a[i]-1
    j+=1

suited = list(dd.keys())[pos:]
temp = j-pos
k-=pos
k = k%temp

# print(dd, pos, j)

print(suited[k])

Submission status: Wrong Answer

Solution code:
#coding: utf-8
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [False for _ in range(N)]
state = 0
heiro = [0]
visited[0] = True
while True:
    state = A[state]-1
    heiro.append(state)
    # print("state", state, A[state])
    if visited[state]:
        break
    visited[state] = True
cnter = dict(Counter(heiro))
for k in cnter.keys():
    if cnter[k] == 2:
        v = k
        break
idx = heiro.index(v)
K -= idx
# ans = heiro[idx:-1][K % (len(heiro) - idx - 1)] + 1
ans = heiro[idx:-1][K % len(heiro[idx:-1])] + 1
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
now = 1
flag = [False] * N
c = 0
temp = [0]*N
hoge = False
for i in range(K):
    if flag[now-1]:
        hoge = True
        break
    flag[now-1] = True
    now = A[now-1]
    c += 1
    temp[now-1] = i
if hoge:
    c -= temp[now]
    K -= temp[now]
    for _ in range(K%c):
        now = A[now-1]
print(now)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = input().split()
A_MAP = {}
for i in range(N):
    A_MAP[str(i+1)] = A[i]

print(A_MAP)
route_hash = {'1': 0}
current_pos = '1'
next_pos = ''
counter = 0
while counter <= K:
    next_pos = A_MAP[current_pos]
    counter+=1
    if next_pos in route_hash:
        break
    route_hash[next_pos] = counter # たどり着くまでにかかった回数
    current_pos = next_pos

if counter >= K:
    print(current_pos)
    exit()

print(route_hash, current_pos, counter)
remain = K - counter
counter -= route_hash[next_pos]
remain = remain % counter
print(K, counter, remain, next_pos)

current_pos = next_pos
for i in range(remain):
    next_pos = A_MAP[current_pos]
    route_hash[next_pos] = counter # たどり着くまでにかかった回数
    current_pos = next_pos
print(current_pos)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [val - 1 for val in A]
# print(A)
route = [0]
seen = {0}
here = 0
while True:
    here = A[here]

    if here in seen:
        start = route.index(here)
        period = len(route) - start
        # print(period)
        r = (K - start + 1) % period
        if r == 0:
            idx = period + start - 1
        else:
            idx = r + start - 1
        print(route[idx] + 1)
        break
    seen.add(here)
    route.append(here)
    # print(route)
# print(period)
# else:
#     print(route[K] + 1)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
A = list(map(int, input().split()))
A = {i+1:A[i] for i in range(len(A))}

if k <= n:
    current = 1
    for i in range(k):
        current = A[current]
    print(current)
else:
    current = 1
    pos = []
    visited = {i+1:0 for i in range(len(A))}
    for i in range(n):
        pos.append(current)
        visited[current] += 1
        if visited[current] == 2:
            circle_len = len(pos)
            print(pos[k%circle_len])
            break
        current = A[current]

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
i=1
S=[1]
while(1):
	S.append(L[i-1])
	i=S[-1]
	if len(set(S))!=len(S):
		break
for i in range(len(S)):
	if S[-1]==S[i]:
		K=K-i
		K=K%(len(S)-1-i)
		print(S[K+i])
		exit()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
data = list(map(int, input().split()))
visited = [False for _ in range(n)]
nex = 0
visited[0] = True
while True:
    nex = data[nex]-1
    if visited[nex] is True:
        break
    else:
        visited[nex] = True
start = nex
cycle = 1
c = [start]
while True:
    nex = data[nex]-1
    if nex == start:
        break
    else:
        cycle += 1
        c.append(nex)
nex = 0
before = 1
while True:
    nex = data[nex]-1
    if nex == start:
        break
    else:
        before += 1

if start != 0:
    print(c[(k-before)%cycle]+1)
else:
    print(c[k%cycle]+1)

Submission status: Wrong Answer

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

l = []

t = 1
for i in range(n):
  t = a[t - 1]
  if t in l:
    #print(t)
    t = l.index(t)
    break
  l.append(t)
  #print(t)

shuki = i - t
ls = l[t:]

b = (k - t) % shuki
print(ls[b - 1])

Submission status: Wrong Answer

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

if len(a)/2>k:
    for i in range(k):
        c=a[c]+1
    print(c)

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
dict={}
for i in range(n):
    dict[i]=a[i]-1
tmp=0
num2city={}
rec={}
rec[0]=1
num2city[0]=0
num2city[0]=0
for j in range(k):
    tmp=dict[tmp]#現在地更新
  #  print(tmp+1)
    if tmp not in num2city.keys():
        num2city[tmp]=j #町に対いて到着した番号#
    else:#もしすでに行ったことあるなら
        res=k-j-1
        mae=num2city[tmp]
        term=j-mae+1
        r=res%term
      #  print(j,res,mae,term,r)
       # print(num2city)
        #print(dict)
        tmp=dict[r]#残りの回数がk-j-1 を過去に付いてた時から進めればよい

        break



print(tmp+1)

Submission status: Wrong Answer

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,k = readInts()
    A = readInts()
    root = []
    ok = True
    l = 0
    dic = defaultdict(int)
    while True:
        if ok:
            a = 1
            ok = False
        else:
            a = root[-1]
        if not dic[A[a-1]]:
            dic[A[a-1]] = 1
            root.append(A[a-1])
        else:
            j = 0
            for i in range(len(root)):
                if A[a-1] == root[i]:
                    j = i
            root = root[j:]
            l = j
            #print(root)
            break

    #print(root)
    k = k - (l+1) if k - (l+1) >= 0 else k
    print(root[k%len(root)])
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a = [a[i] - 1 for i in range(n)]
    next_i = 0
    seen = [-1] * n
    status = True
    count = 0
    check_a = 0
    check_b = 0
    while status:
        count += 1
        if count == k:
            print(a[next_i] + 1)
            exit()
        elif seen[next_i] != -1:
            status = False
            check_a = next_i
            check_b = count - seen[next_i]
        else:
            seen[next_i] = count
            next_i = a[next_i]
    index = (k - count + 1) % (check_b - check_a) + seen[check_a]
    for i in range(n):
        if seen[i] == index:
            print(i + 1)
            exit()
    print(next_i + 1)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
# # for i in range(m):
# s = input()
# t = input()
# n = len(s)
# m = len(t)
# # print(s,t,n,m)
# if n+1==m:
# 	for i in range(n):
# 		# print(s[i], t[i])
# 		if s[i]!=t[i]:
# 			print("No")
# 			break
# 	else:
# 		print("Yes")
# else:
# 	print("No")
	# l, r = [int(j)-1 for j in input().split()]
	################
# def fun(a,b,c,k):
# 	ans =0 
# 	if k<=a:
# 		return k
# 	else:
# 		ans = a
# 		k-=a
# 	if k<=b:
# 		return ans
# 	else:
# 		k-=b
# 	# if k<=c:
# 	# print(ans, c)
# 	return ans-min(c,k)
# a,b,c,k = [int(j) for j in input().split()]
# # l = [a,b,c]
# # j = 0
# print(fun(a,b,c,k))
################################

# while(k>0):
# 	a = min(k, l[j])
# 	k-=a

#######################

n,k = [int(j) for j in input().split()]
a = [int(j) for j in input().split()]
s = dict()
l = []
temp = 1
ct = 0
rep = None
# k_copy = 
while(k>0):
	ct+=1
	k-=1
	# print(temp)
	temp = a[temp-1]
	if temp in s:
		rep = s[temp]
		break
	else:
		s[temp]=ct
		l.append(temp)
if rep==None:
	print(l[k])
else:
	n = len(l)
	# print(l, rep, k, n)


	print(l[rep-1+(k)%(max(n-rep, 1))])
# print(l[s[a[ct]]-1+(k-s[a[ct]]+1)%(-1*s[a[ct]]+ct+1)])

Submission status: Wrong Answer

Solution code:
import sys

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

array = [-1] * (n+1)
array[1] = 0

li = []
li.append(1)

tmp = -1
index = 0
for i in range(2 * (10**5)):
  if array[a[index]] != -1:
    tmp = array[a[index]]
    break
    
  if i == k:
    print(a[index])
    sys.exit(0)
    
  li.append(a[index])
  array[a[index]] = i+1
  index = a[index] -1
  
k -= tmp
d = len(li) - tmp

k %= d

print(li[k+tmp])

Submission status: Wrong Answer

Solution code:

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



step = [0] * (N+1)
step[0] = 1
curr = 1
cnt = 0
visited = [False] * N
first = [0] * N

while True:
      
      if visited[curr-1]:
            break
      nxt = A[curr-1]
      first[curr-1] = cnt
      visited[curr-1] = True
      step[cnt] = curr
      cnt += 1
      curr = nxt
      
#print(step, cnt, first)

if K < cnt:
  print(step[K])
else:
  cnt_until_loop = cnt
  num = cnt - first[curr-1]
  K -= num
  K %= cnt - first[curr-1]
  print(step[K])

Submission status: Wrong Answer

Solution code:
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)


def main():
    N, K = map(int, input().split())
    A_i = list(map(int, input().split()))
    B_i = []
    cycle_list = []
    
    cycle = 0
    diff = 0
    counter = 0
    machi = 1
    for num in A_i:
        if machi in B_i:
            diff = B_i.index(machi)
            cycle = counter - diff
            cycle_list = B_i[diff:]
        else:
            counter += 1
            B_i.append(machi)
            machi = A_i[machi - 1]

    mod_K = (K - diff) % cycle

    print(cycle_list[mod_K])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

a=[int(x) for x in input().split()]
for i in range(n):
  a[i]-=1


l=[-1 for i in range(n)]
c=0
t=-1
ans=k
for i in range(n):
  if l[c]>=0:
    t=i-l[c]
    ans= (k-l[c])%t+l[c]
    break
  l[c]=i
  c=a[c]
for i in range(n):
  if l[i]==ans:
    break
print(i+1)
#print(l,ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
chk=[False]*n
x=0
visit=[0]
chk[x]=True
while chk:
    x=a[x]-1
    if chk[x]:
        break
    else:
        visit.append(x)
        chk[x]=True
l1=0
while visit[l1]!=x:
    l1+=1
l2=len(visit)-l1
print(visit[l1+((k-l1)%l2)]+1)

Submission status: Wrong Answer

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


i = 0
ans = [1]


def has_duplicates(seq):
    return len(seq) != len(set(seq))


while True:
    if (has_duplicates(ans)):
        break
    else:
        ans.append(a[(ans[i])])
        i += 1

delcount = 0
for i in range(len(ans)):
    if ans[-1] == ans[0]:
        ans.pop(-1)
        break
    else:
        ans.pop(0)
        delcount += 1

print(ans[(k-delcount) % len(ans)])

Submission status: Wrong Answer

Solution code:
import sys 
n,m=list(map(int,input().split(" ")))
sys.setrecursionlimit(10**6) 
arr=list(map(int,input().split(" ")))
d={}
for i in range(n):
	d[i]=[]
for i in range(n):
	d[i].append(arr[i]-1)

from collections import deque
def bfs(start,n,visited,m):
	# count=0

	# l2=[i+1]
	q=[start]
	q=deque(q)
	# count+=1
	moves=[]
	moves.append(start)
	visited[start]=1
	left=-1
	fl=0
	while (len(q)!=0 and fl==0):
		y=d[q[0]]
		for j in y:
			if(visited[j]==0):
				moves.append(j)
				visited[j]=1
				# l2.append(j)
				q.append(j)

				# count+=1
			else:
				left=j
				fl=1
				break



		q.popleft()
	# print(left,moves)
	if(left!=-1):
		pre=[]
		post=[]
		for i in range(len(moves)):
			if(moves[i]==left):
				pre=moves[:i]
				post=moves[i:]
				break
		if(m<=len(pre)):
			print(pre[m-1]+1)
		else:
			m-=len(pre)

			print(post[m%len(post)]+1)


	else:
		# curr=0

		print(pre[moves%len(pre)]+1)
visited=[0]*n
bfs(0,n,visited,m)

Submission status: Wrong Answer

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

for i in range(N):
  A[i] -= 1

loop_lis = [0]
visited = [0 for _ in range(N)]
visited[0] = 1
loop_time = 0
start_loop = 0
for i in range(K):
  if visited[A[loop_lis[-1]]] != 0:
    loop_time = (i+2)-(visited[A[loop_lis[-1]]])
    start_loop = i+1 - loop_time
    break
  visited[A[loop_lis[-1]]] = i+1
  loop_lis.append(A[loop_lis[-1]])
if len(loop_lis) == K+1:
  print(loop_lis[-1]+1)
else:
  print(loop_lis[start_loop+(K-start_loop)%loop_time]+1)

Submission status: Wrong Answer

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

flag = [0 for _ in range(N)]

tmp = 1
i = 1
flag[0] = 1
while(True):
    tmp = A[tmp-1]
    i += 1
    if (flag[tmp-1] != 0):
        break
    flag[tmp-1] = i

pre = flag[tmp-1] - 1
cyc = i - flag[tmp-1]
index = (K - pre) % cyc
print(A[flag[pre + index ]])