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()))

A.insert(0,-1)
root = [1]
next = A[1]
rf = 0

for _ in range(N):
    if next not in root:
        root.append(next)
        next = A[next]
    else:
        rf = root.index(next)
        root = root[rf:]
        break

print(root[(K - rf) % len(root)])

Submission status: Wrong Answer

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

d = {}
d_n = {}
first_d_n = {}
for i in range(N):
  d[i+1] = a[i]
  if i+1 == 1:
    d_n[i+1] = 1
  else:
    d_n[i+1] = 0

m = 1
k = 0 # 累計移動回数
first_d_n[1] = 0


while m:
  m = d[m]
  d_n[m] += 1
  k += 1
  if d_n[m] == 1:
    first_d_n[m] = k
  
  if d_n[m] == 2:
    break
    
first_n = first_d_n[m]

loop_mod = (K - first_n) % (k - first_n)
#print(loop_mod)

if loop_mod == 0:
  print(m)
else:  
  for l in range(loop_mod):
    m = d[m]
  print(m)

# print(first_d_n[loop_mod -

Submission status: Wrong Answer

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

d = {}
d_n = {}
first_d_n = {}
for i in range(N):
  d[i+1] = a[i]
  if i+1 == 1:
    d_n[i+1] = 1
  else:
    d_n[i+1] = 0

m = 1
k = 0 # 累計移動回数
first_d_n[1] = 0


while m:
  m = d[m]
  d_n[m] += 1
  k += 1
  if d_n[m] == 1:
    first_d_n[m] = k
  
  if d_n[m] == 2:
    break
    
first_n = first_d_n[m]

loop_mod = (K - first_n) % (k - first_n)
#print(loop_mod)

if loop_mod == 0:
  print(m)
for l in range(loop_mod):
  m = d[m]
  print(m)

# print(first_d_n[loop_mod -

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
arr=list(map(int,input().split()))
l=[-1]*(n+1)
tmp=[]
pos=1
cnt=0
while 1:
  if l[pos]!=-1:
    if k<=l[pos]:
      print(tmp[k])
    else:
      print(tmp[l[pos]+(k+1)%(cnt-l[pos])])
    break
  l[pos]=cnt
  cnt+=1
  tmp.append(pos)
  pos=arr[pos-1]

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
visited = []
now = 1#最初の街にいる
next_town = A[now-1]
while(1):
    if next_town in visited:#もし次の街が訪れたことがある場合
        break#ループするので抜け出す
    visited.append(now)#今いる街は訪れたこととなるので記録する
    next_town = A[now-1]#次に行く街を探す
    now = next_town#次の街に着いたこととして変数を代入

first_roop_city = A[visited[-1]-1]#最初にループした街
first_roop_city_index = visited.index(first_roop_city)#何回目のテレポートにループが始まったか

if K <= len(visited):
    print(visited[K])
else:
    print(visited[K%(len(visited)-first_roop_city_index)])

Submission status: Wrong Answer

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

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

moved = {}
now_city = 1
next_city = A[0]
i=0

while i < N:
    moved[now_city] = i
    if next_city in moved:
        break
    now_city = next_city
    next_city = A[now_city-1]
    i += 1

# i 回目の試行で、now_city から next_city に移ろうとしている

mod = i - moved[next_city]

moved_list = [x for x in moved.keys()]

loop_list = moved_list[moved[next_city]:i+1]
loop = len(loop_list)
print(loop_list[(K-moved[next_city])%loop])


# print(f"{next_city=} {i=}")
# print(f"{loop_list=}")
# print(f"{moved_list=}")
# print(f"{K-moved[next_city]=}")
# print(f"{loop=}")

# ans = 5
# 6 10
# 2 3 4 5 4 6

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
A = list(map(int, sys.stdin.readline().split()))
visited = [0] * N
visited[0] = 1
ikisaki = 0
c = 1
while c < N:
    ikisaki = A[ikisaki] - 1
    c += 1
    if visited[ikisaki]:
        loop = c - visited[ikisaki]
        c = N
    else:
        visited[ikisaki] = c

amari = (K - (visited[ikisaki] - 1)) % loop
new = [i - visited[ikisaki] for i in visited]
print(new.index(amari) + 1)

Submission status: Wrong Answer

Solution code:
import numpy as np

def check_num_of_remaining_teleport(num_of_teleport, i):
    if num_of_teleport == 0:
        print(i)
        exit()

num_of_town, num_of_teleport = map(int, input().split())
town = np.array(list(map(int, input().split())))

# 香り高い町の探索
i = 1
# テレポートの移動ルールに合わせて1始まりにする
fragranttown_index = 1
fragrants = [0] * num_of_town
fragrants[0] = 1
for _ in range(num_of_town):
    i = town[i - 1]
    num_of_teleport = num_of_teleport - 1

    # 移動回数が終了した場合
    check_num_of_remaining_teleport(num_of_teleport, i)

    # すでに通った町か
    if fragrants[i - 1] != 1:
        fragrants[i - 1] = 1
    else:
        fragranttown_index = i
        break

# 香り高い町への再訪までの回数探索
# 再訪までの回数
revisit = 0
j = fragranttown_index
for _ in range(num_of_town):
    j = town[j - 1]
    revisit = revisit + 1
    num_of_teleport = num_of_teleport - 1

    # 移動回数が終了した場合
    check_num_of_remaining_teleport(num_of_teleport, j)

    # テレポート先が香り高い町なら
    if fragranttown_index == j:
        break

# 香り高い町からの最終移動
num_of_last_teleport = num_of_teleport % revisit
k = fragranttown_index
lasttown_index = fragranttown_index
# rangeの中は一時変数にした方が高速
for _ in range(num_of_last_teleport):
    lasttown_index = k
    k = town[k - 1]
    
print(lasttown_index)

Submission status: Wrong Answer

Solution code:
import numpy as np

def check_num_of_remaining_teleport(num_of_teleport, i):
    if num_of_teleport == 0:
        print(i)
        exit()

num_of_town, num_of_teleport = map(int, input().split())
town = np.array(list(map(int, input().split())))

# 香り高い町の探索
i = 1
# テレポートの移動ルールに合わせて1始まりにする
fragranttown_index = 1
fragrants = [0] * num_of_town
fragrants[0] = 1
for _ in range(num_of_town):
    print(num_of_teleport)
    i = town[i - 1]
    num_of_teleport = num_of_teleport - 1

    # 移動回数が終了した場合
    check_num_of_remaining_teleport(num_of_teleport, i)

    # すでに通った町か
    if fragrants[i - 1] != 1:
        fragrants[i - 1] = 1
    else:
        fragranttown_index = i
        break

# 香り高い町への再訪までの回数探索
# 再訪までの回数
revisit = 0
j = fragranttown_index
for _ in range(num_of_town):
    print(num_of_teleport)
    j = town[j - 1]
    revisit = revisit + 1
    num_of_teleport = num_of_teleport - 1

    # 移動回数が終了した場合
    check_num_of_remaining_teleport(num_of_teleport, j)

    # テレポート先が香り高い町なら
    if fragranttown_index == j:
        break

# 香り高い町からの最終移動
num_of_last_teleport = num_of_teleport % revisit
k = fragranttown_index
lasttown_index = fragranttown_index
# rangeの中は一時変数にした方が高速
for _ in range(num_of_last_teleport):
    lasttown_index = k
    k = town[k - 1]
    
print(lasttown_index)

Submission status: Wrong Answer

Solution code:
import numpy as np

def check_num_of_remaining_teleport(num_of_teleport, i):
    if num_of_teleport == 0:
        print(i)
        exit()

num_of_town, num_of_teleport = map(int, input().split())
town = np.array(list(map(int, input().split())))

# 香り高い町の探索
i = 1
# テレポートの移動ルールに合わせて1始まりにする
fragranttown_index = 1
fragrants = [0] * num_of_town
fragrants[0] = 1
for _ in range(num_of_town):
    i = town[i - 1]
    num_of_teleport = num_of_teleport - 1

    # 移動回数が終了した場合
    check_num_of_remaining_teleport(num_of_teleport, i)

    # すでに通った町か
    if fragrants[i - 1] != 1:
        fragrants[i - 1] = 1
    else:
        fragranttown_index = i
        break

# 香り高い町への再訪までの回数探索
# 再訪までの回数
revisit = 0
j = fragranttown_index
for _ in range(num_of_town):
    j = town[j - 1]
    revisit = revisit + 1
    num_of_teleport = num_of_teleport - 1

    # 移動回数が終了した場合
    check_num_of_remaining_teleport(num_of_teleport, j)

    # テレポート先が香り高い町なら
    if fragranttown_index == j:
        break

# 香り高い町からの最終移動
# 最終移動時の移動先の値ではなく移動先の位置を取得するため、ループ回数を-1する
num_of_last_teleport = num_of_teleport % revisit - 1
k = fragranttown_index
# rangeの中は一時変数にした方が高速
for _ in range(num_of_last_teleport):
    k = town[k - 1]

print(k)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
lst=[0]
now=0
memo=0
town=[0]*n
town[0]=1
b=0
for i in range(k):
	now=a[now]
	if town[now]==1:
		memo=now
		b=1
		break
	lst.append(now)
	town[now]=1
if b==1:
	i=lst.index(memo)
	l=len(lst)
	if memo!=0:
		lst=lst[i:]
		l-=len(lst)
		print(lst[(k-l)%len(lst)]+1)
	else:
		print(lst[k%l]+1)
else:
	print(now)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
path = [1]
pos = 1
TF = [False] + [True]*n
for i in range(k-1):
    pos = a[pos - 1]
    if TF[pos-1]:
        path.append(pos)
        TF[pos-1] = False
    else:
        break
first = path.index(pos)
ans = (k - first) % (len(path) - first) + first
print(path[ans])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
path = [1]
pos = 1
TF = [False] + [True]*n
for i in range(k-1):
    pos = a[pos - 1]
    if TF[pos-1]:
        path.append(pos)
    else:
        break
first = path.index(pos)
ans = (k - first) % (len(path) - first) + first
print(path[ans])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
path = [1]
pos = 1
for i in range(k-1):
    pos = a[pos - 1]
    if pos not in path:
        path.append(pos)
    else:
        break
first = path.index(pos)
ans = (k - first) % (len(path) - first) + first
print(path[ans])

Submission status: Wrong Answer

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

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

Visited = [-1]*N
now = 1
Order = [0]*N

loopcount = 1
Order[0] = 1
Visited[0] = 1
for i in range(1,N):
    nt = Map[now-1]
    Order[i] = nt
    loopcount += 1
    if Visited[nt-1] == -1:
        Visited[nt-1] = loopcount
    else:
        break
    now = nt
    

ns = Visited[nt-1]
period = loopcount - ns

if period == 0:
    period = loopcount
    ns = 0

Kmod = K - ns
Kmod %= period

print(Order[ns+Kmod])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
aList = list(map(int, input().split()))
usedList = [0 for n in range(N)]
count = 1
for n in range(K+1):
    if(n == 0):
        index = aList[0] - 1
        usedList[0] = 1
    elif(usedList[index] == 0):
        index = aList[index] - 1
        usedList[index] = 1
    else:
        tmp = aList[index]
        while(1):
            index = aList[index] - 1
            count += 1
            if(tmp == aList[index]):
                break
        break
K %= count
for n in range(K+1):
    if(n == 0):
        index = aList[0] - 1
    elif(usedList[index] == 0):
        index = aList[index] - 1
print (index + 1)

Submission status: Wrong Answer

Solution code:
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()]

    done = [0] * N
    step = -1
    start = 0
    current = 1
    between = 0

    while True:
        if done[current - 1] > 0:
            start = done[current - 1] - 1
            between = step - done[current - 1] + 1
            break
        step += 1
        done[current - 1] = step
        current = As[current - 1]

    if K <= step:
        print(done.index(K) + 1)
    else:
        res = (K - start) % between
        print(done.index(res + start) + 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
lambda_1=lambda s:int(s)-1
a=list(map(lambda_1, input().split()))
#print(a)
r=[-1]*n
b=[]
c=0
while True:
	if r[a[c]]!=-1:
		a0=b.index(a[c])
		k0=(k-1-a0)%(len(b)-a0)+a0
		"""print(k,len(b),a0)
		print("b",b,a[c])
		print("r",r)
		print(a0,k0)"""
		print(b[k0]+1)
		exit()
	b.append(a[c])
	r[a[c]]=c
	c=a[c]

Submission status: Wrong Answer

Solution code:
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()]

    done = [0] * N
    step = -1
    start = 0
    current = 1
    between = 0

    while True:
        if done[current - 1] > 0:
            start = done[current - 1] - 1
            between = step - done[current - 1] + 1
            break
        step += 1
        done[current - 1] = step
        current = As[current - 1]

    if K <= current:
        print(done.index(K) + 1)
    else:
        res = (K - start) % between
        print(done.index(res + start) + 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
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()]

    done = [0] * N
    step = -1
    start = 0
    current = 1
    between = 0

    while True:
        if done[current - 1] > 0:
            start = done[current - 1] - 1
            between = step - done[current - 1] + 1
            break
        step += 1
        done[current - 1] = step
        current = As[current - 1]

    if K <= current:
        print(done.index(K) + 1)
    else:
        res = (K - start) % between
        print(done.index(res) + 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
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()]

    done = [0] * N
    step = -1
    start = 0
    current = 1
    between = 0

    while True:
        if done[current - 1] > 0:
            start = done[current - 1] - 1
            between = step - done[current - 1] + 1
            break
        step += 1
        done[current - 1] = step
        current = As[current - 1]
    print(done)

    if K <= current:
        print(done.index(K) + 1)
    else:
        res = (K - start) % between
        print(done.index(res) + 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#import bisect,collections,copy,heapq,itertools,math,numpy,string
#from operator import itemgetter
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
N,K = LI()
A = LI()

#方針
#ループ検知でmod演算に移行
hist = []
now = 0
chk = [-1] * N
while True:
	hist.append(now+1)
	if chk[now] != -1:
		# ループ検知
		break
	chk[now] = len(hist)-1
	now = A[now]-1
#print("histpos[town]:",chk)
#print("hist:",hist)
loopstart = chk[hist[len(hist)-1]-1]+1
K-=loopstart
K%=len(hist)-loopstart
print(hist[loopstart+K])

Submission status: Wrong Answer

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


visited = [0] * N

idx = 0

town = 0

for i in range(K):
    if visited[idx]:
        cycle = i - visited[idx]
        rem_step = (K - i) % cycle
        town = visited.index(visited[idx] + rem_step)
        break


    visited[idx] = i
    idx = t[idx] - 1


print(town+1)

Submission status: Wrong Answer

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

    curr_city = 1
    city_index_d = {1:0}
    for i in range(1,n+1):
        next_city = a_list[curr_city-1]
        if next_city in city_index_d:
            rep_top = city_index_d[next_city]
            rep_end = i-1
            rep_time = rep_end - rep_top + 1
            top_remained = rep_top - 1
            if next_city == 1:
                r = k%rep_time
                # print(rep_time,'a')
                # print(city_index_d)
                for k,v in city_index_d.items():
                    if v  == r:
                        print(k)
                        return
            break
        elif i == n:
            print(next_city)
            return
        else:
            city_index_d[next_city] = i
            curr_city = next_city

    # print(rep_top, rep_end, rep_time, top_remained)
    # print(city_index_d)

    r = k -top_remained - ( (k - top_remained) // rep_time ) * rep_time
    if r == 0:
        r = rep_time

    # print(r)
    for k,v in city_index_d.items():
        if v - top_remained == r:
            print(k)
            return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    curr_city = 1
    city_index_d = {1:0}
    for i in range(1,n+1):
        next_city = a_list[curr_city-1]
        if next_city in city_index_d:
            # if next_city == 1:
            # else:
            rep_top = city_index_d[next_city]
            rep_end = i-1
            rep_time = rep_end - rep_top + 1
            top_remained = rep_top - 1
            break
        elif i == n:
            print(next_city)
            return
        else:
            city_index_d[next_city] = i
            curr_city = next_city

    # print(rep_top, rep_end, rep_time, top_remained)
    # print(city_index_d)

    r = k -top_remained - ( (k - top_remained) // rep_time ) * rep_time
    if r == 0:
        r = rep_time

    # print(r)
    for k,v in city_index_d.items():
        if v - top_remained == r:
            print(k)
            return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import itertools
n,k=map(int,input().split())
a=list(map(int,input().split()))
pas=[1]
p=1
count=1
for i in range(n):
	p=a[p-1]
	pas.append(p)
	count+=1
	if count!=len(set(pas)):
		nloop=pas.index(p)
		loop=count-1-nloop
		break
last=(k-nloop)%loop
print(pas[nloop+last])

Submission status: Wrong Answer

Solution code:
import itertools
n,k=map(int,input().split())
a=list(map(int,input().split()))
pas=[1]
p=1
for i in range(n):
	p=a[p-1]
	pas.append(p)
	if len(pas)!=len(set(pas)):
		nloop=pas.index(p)
		loop=len(pas)-1-nloop
		break
last=(k-nloop)%loop
print(pas[nloop+last])

Submission status: Wrong Answer

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

t = 0
done = [t]
itvl = 1
st = 1
for i in range(k):
  t=A[t]
  if t in done:
    itvl = i + 1 - done.index(t)
    for j in range((k - i - 1)%itvl):
      t=A[t]
    print(t+1)
    exit()
  done.append(t)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
p=1
l=[1]
while True:
	p=a[p-1]
	if p in l:
		s=l.index(p)
		w=len(l)-s
		break
	l.append(p)
ans=l[s:]
print(ans[(k-s)%w])

Submission status: Wrong Answer

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

d = [-1]*(N+1)
i = 1
step = 1
while step <= K:
	next_i = As[i]
	if d[next_i] != -1:
		freq = step - d[next_i] +1
		step = step + ((K - step) // freq)*freq
	d[next_i] = step
	step += 1
	i = next_i
 
print(i)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[-1 for i in range(N+1)]
C=[0 for i in range(N)]

B[1]=1
i=0

C[0]=1
for i in range(1,N+1):

    if(B[A[C[i-1]-1]]==-1):

        B[A[C[i-1]-1]]=A[C[i-1]-1]
        C[i]=A[C[i-1]-1]




    else:
        bfirst=C.index(A[C[i-1]-1])
        blast=i

        roop=blast-bfirst

        ans=(K-bfirst)%roop
        while(0):
            print(f)
        print(C[bfirst+ans])
        while(0):
            print(f)
        exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[-1 for i in range(N+1)]
C=[0 for i in range(N)]

B[1]=1
i=0

C[0]=1
for i in range(1,N+1):

    if(B[A[C[i-1]-1]]==-1):

        B[A[C[i-1]-1]]=A[C[i-1]-1]
        C[i]=A[C[i-1]-1]




    else:
        bfirst=C.index(A[C[i-1]-1])
        blast=i

        roop=blast-bfirst

        ans=(K-bfirst)%roop

        print(C[bfirst+ans])
        while(0):
            print(f)
        exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[-1 for i in range(N+1)]
C=[0 for i in range(N)]

B[1]=1
i=0

C[0]=1
for i in range(1,N+1):

    if(B[A[C[i-1]-1]]==-1):

        B[A[C[i-1]-1]]=A[C[i-1]-1]
        C[i]=A[C[i-1]-1]




    else:
        bfirst=C.index(A[C[i-1]-1])
        blast=i

        roop=blast-bfirst

        ans=(K-bfirst)%roop

        print(C[bfirst+ans])
        exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[-1 for i in range(N+1)]
C=[0 for i in range(N)]

B[1]=1
i=0

C[0]=1
for i in range(1,N):

    if(B[A[C[i-1]-1]]==-1):

        B[A[C[i-1]-1]]=A[C[i-1]-1]
        C[i]=A[C[i-1]-1]




    else:
        bfirst=C.index(A[C[i-1]-1])
        blast=i

        roop=blast-bfirst

        ans=(K-bfirst)%roop

        print(C[bfirst+ans])
        exit()

while(1):
    print('f')

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[-1 for i in range(N+1)]
C=[0 for i in range(N)]

B[1]=1
i=0
mae=1
C[0]=1
for i in range(1,N):

    if(B[A[mae-1]]==-1):

        B[A[mae-1]]=A[mae-1]
        C[i]=A[mae-1]

        mae=A[mae-1]


    else:
        bfirst=C.index(A[mae-1])
        blast=i

        roop=blast-bfirst

        ans=(K-bfirst)%roop

        print(C[bfirst+ans])
        exit()

while(1):
    print('f')

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[-1 for i in range(N+1)]
C=[0 for i in range(N)]

B[1]=1
i=0
mae=1
C[0]=1
for i in range(1,N):

    if(B[A[mae-1]]==-1):

        B[A[mae-1]]=A[mae-1]
        C[i]=A[mae-1]

        mae=A[mae-1]


    else:
        bfirst=C.index(A[mae-1])
        blast=i

        roop=blast-bfirst

        ans=(K-bfirst)%roop

        print(C[bfirst+ans])
        exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[1]
B.append(A[B[0]-1])
i=0
for i in range(1,N):

    B.append(A[B[i]-1])

    if(len(set(B))!=i+2):

        blast=B[-1]
        bfirst=B.index(blast)
        roop=len(B)-bfirst
        ans=(K-bfirst+1)%roop
        print(B[ans])
        exit()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
B=[1]
B.append(A[B[0]-1])
i=0
for i in range(1,K):

    B.append(A[B[i]-1])

    if(len(set(B))!=i+2):

        blast=B[-1]
        bfirst=B.index(blast)
        roop=len(B)-bfirst
        ans=(K-bfirst+1)%roop
        print(B[ans])
        exit()

Submission status: Wrong Answer

Solution code:
def find_kth_city(a, n, k):
  visited = {i:-1 for i in range(n)}
  visited[0] = 0
  current = 1
  for i in range(k):
    current = a[current-1]
    if visited[current-1] != -1:
      const = visited[current-1]
      period = i+1 - visited[current-1]
      break
    visited[current-1] = i+1
  else:
    return current
  print(visited)
  inverse = {v: i for i, v in visited.items() if v >= 0}
  print(inverse)
  return inverse[(k-const)%period + const]+1
  
n, k = map(int, input().split())
a = list(map(int, input().split()))
print(find_kth_city(a, n, k))

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

l = [-1]*(n + 1)
l[1] = 1

order = 1
now = 0

while k > 0:
    k -= 1
    now = a[now]
    order += 1
    if l[now] == -1:
        l[now] = order
    else:
        loop = order - l[now] + 1
        k %= loop
        break

while k > 0:
    k -= 1
    now = a[now]

print(now + 1)

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

l = [-1]*(n + 1)
l[1] = 1

order = 1
now = 0

while k > 0:
    k -= 1
    now = a[now]
    order += 1
    if l[now] == -1:
        l[now] = order
    else:
        loop = order - now + 1
        k %= loop
        break

while k > 0:
    k -= 1
    now = a[now]

print(now + 1)

Submission status: Wrong Answer

Solution code:
import sys

def machi(k,W):
    visited = set()
    t = 0
    time = 0
    result = []
    while not t in visited:
        if time > k:
            return -1, result
        visited.add(t)
        result.append(t)
        t = W[t] - 1
        time += 1
    bp = result.index(t)
    return bp,result

        

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')


n,k = nm()
a = nl()
bp,roop = machi(k,a)
if bp == -1:
    print(roop[-1]+1)
    print(roop[-2])
    exit()

l_dist = len(roop[bp:])
f_dist = bp
fs = k - f_dist
arp = f_dist + (fs % l_dist)
print(roop[arp]+1)

Submission status: Wrong Answer

Solution code:
import sys

def machi(W):
    visited = set()
    t = 0
    result = []
    while not t in visited:
        visited.add(t)
        result.append(t)
        t = W[t] - 1
    bp = result.index(t)
    return bp,result
        

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')


n,k = nm()
a = nl()
bp,roop = machi(a)
l_dist = len(roop[bp:])
f_dist = bp
fs = k - f_dist
arp = f_dist + (fs % l_dist)
print(roop[arp]+1)

Submission status: Wrong Answer

Solution code:
import sys

def machi(W):
    visited = set()
    t = 0
    result = []
    while not t in visited:
        visited.add(t)
        result.append(t)
        t = W[t] - 1
    bp = result.index(t)
    return bp,result
        

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')


n,k = nm()
a = nl()
bp,roop = machi(a)
l_dist = len(roop[bp:])
f_dist = bp
fs = k - f_dist
lst = fs // l_dist
arp = f_dist + (fs - l_dist * lst)
print(roop[arp]+1)

Submission status: Wrong Answer

Solution code:
def resolve():
    citys_num, k = map(int, input().split())
    cities = list(map(int, input().split()))

    visited = set()
    route = []
    isAgain = False
    currectCity = 1
    while not isAgain:
        if currectCity in visited:
            route.append(currectCity)

            isAgain = True
        else:
            visited.add(currectCity)
            route.append(currectCity)

            currectCity = cities[currectCity - 1]
    loopstart = route.index(route[-1])
    loop = route[loopstart:-1]

    k = k - loopstart
    k = k % len(loop)

    print(loop[k])
resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    citys_num, k = map(int, input().split())
    cities = list(map(int, input().split()))

    visited = []
    isAgain = False
    currectCity = 1
    while not isAgain:
        if {currectCity} | set(visited) == set(visited):
            visited.append(currectCity)

            isAgain = True
        else:
            visited.append(currectCity)
            currectCity = cities[currectCity - 1]
    loopstart = visited.index(visited[-1])
    loop = visited[loopstart:-1]

    k = k - loopstart
    k = k % len(loop)

    print(loop[k])
    
resolve()

Submission status: Wrong Answer

Solution code:

def resolve():
    citys_num, k = map(int, input().split())
    cities = list(map(int, input().split()))

    visited = []
    isAgain = False
    currectCity = 1
    while not isAgain:
        if currectCity in visited:
            visited.append(currectCity)

            isAgain = True
        else:
            visited.append(currectCity)
            currectCity = cities[currectCity - 1]
    loopstart = visited.index(visited[-1])
    loop = visited[loopstart:-1]

    k = k - loopstart
    k = k % len(loop)

    print(loop[k])
resolve()

Submission status: Wrong Answer

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

current = machi[n - 1]
for _ in range(k):
    current = machi[current - 1]

print(current)

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    A.insert(0, 0)
    J = [-1] * (N + 1)
    T = []
    que = deque([1])
    count = 0
    l = 0
    while que:
        i = que.popleft()
        if J[i] != -1:
            l = J[i]
            break
        J[i] = count
        count += 1
        T.append(i)
        que.append(A[i])
    if K < l:
        return T[K]
    else:
        return T[(K-(l+1))%(len(T)-l)]
print(main())

Submission status: Wrong Answer

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

A={k+1:P[k] for k in range(N)}


U=list()
S=set()
x=1
k=0

while (x not in S) and (k<=K):
    U.append(x)
    S.add(x)
    
    x=A[x]
    k+=1

if k>K:
    print(U[-1])
else:
    d=U.index(x)
    V=U[d:]
    print(V[(K-d)%len(V)])
    print(U,V)

Submission status: Wrong Answer

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

A={k+1:P[k] for k in range(N)}


U=list()
S=set()
x=1
k=0

while (x not in S) and (k<K):
    U.append(x)
    S.add(x)
    
    x=A[x]
    k+=1

if k==K:
    print(U[-1])
else:
    d=U.index(x)
    V=U[d:]
    print(V[(K-d)%len(V)])

Submission status: Wrong Answer

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

A={k+1:P[k] for k in range(N)}


U=list()
x=1
k=0

while (x not in U) and (k<K):
    U.append(x)
    x=A[x]
    k+=1

if k==K:
    print(U[-1])
else:
    d=U.index(x)
    V=U[d:]
    print(V[(K-d)%len(V)])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a_list = list(map(int,input().split()))
visited = []
i,j = 0,1

while i<len(a_list)+10:
    tmp = a_list[j-1]
    if (j,tmp) not in visited:
        visited.append((j,tmp))
        j = tmp
        i += 1
    else:
        break
        
start = visited.index((j,tmp))
loop_len = len(visited) - start
not_loop_len = start
finish = (k -not_loop_len)%loop_len

print(a_list[start+finish])

Submission status: Wrong Answer

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

loop = []
nextcity = 1
for i in range(1,N+1): 
    if nextcity not in loop: 
        loop.append(nextcity)
        nextcity = A[nextcity]
    else:
        start = loop.index(nextcity) 
        loop = loop[start:] 
        break

print(loop[((K+1-start)%len(loop)) -1])

Submission status: Wrong Answer

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

warped = [ False ] * n
warped[0] = True

x = 0
cycle = 0

while not warped[warping[x]]:
    x = warping[x]
    warped[x] = True

    cycle += 1

    if cycle == k:
        print(x)
        exit(0)

x = warping[x]
cycle += 1

y = 0
cnt = 0

while warping[y] != x:
    y = warping[y]
    cnt += 1

y = warping[y]
cnt += 1

cycle -= cnt

mod = (k - cnt) % (cycle if cycle != 0 else cnt)

while mod:
    y = warping[y]

    mod -= 1

print(y + 1)