Problem: 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.

Submission attempts (in order):
from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
n , k = map(int , input().split())
arr = list(map(int , input().split()))
dic = defaultdict(int)
for i in range(1,n+1):
	dic[i] = arr[i-1]
cycle = []
visited = [False]*(10**6)
curr = 1
if k <= n:
	curr = 1
	count = 0
	while count != k:
		curr = dic[count]
		count+=1
	exit(print(curr))
while True:
	cycle.append(curr)
	visited[curr] = True
	curr = dic[curr]
	#k -= 1
	if visited[curr]:
		break

csize = 0
ind = 0
for i in range(len(cycle)-1,-1,-1):
	if cycle[i] == curr:
		ind = i + 1
		break
	csize += 1
'''
offset = len(cycle) - csize - 1
#print(cycle,csize+1, k , curr,offset)
k -= offset
k %= csize + 1
print(cycle[offset + k])
'''
print(cycle[ind + ((k-ind)%csize)])


from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
n , k = map(int , input().split())
arr = list(map(int , input().split()))
dic = defaultdict(int)
for i in range(1,n+1):
	dic[i] = arr[i-1]
cycle = []
def dfs(node):
	if node in cycle:
		return node
	else:
		cycle.append(node)
		k = dfs(dic[node])
	return k
cyele = dfs(1)
ind = 0
offset = 0
for i in range(len(cycle)):
	if cycle[i] == cyele:
		ind = i
		break
	offset+=1
#rem = k - offset
csize = len(cycle[ind:])
rem=rem%csize

count = 0
curr = 1
while count != rem:
	curr = dic[curr]
	count+=1
print(curr)


from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
n , k = map(int , input().split())
arr = list(map(int , input().split()))
dic = defaultdict(int)
for i in range(1,n+1):
	dic[i] = arr[i-1]
cycle = []
visited = [False]*(10**6)
curr = 1
if k <= n:
	curr = 1
	count = 0
	while count != k:
		curr = dic[count]
		count+=1
	exit(print(curr))
while True:
	cycle.append(curr)
	visited[curr] = True
	curr = dic[curr]
	#k -= 1
	if visited[curr]:
		break

csize = 0
for i in range(len(cycle)-1,-1,-1):
	if cycle[i] == curr:
		break
	csize += 1
'''
offset = len(cycle) - csize - 1
#print(cycle,csize+1, k , curr,offset)
k -= offset
k %= csize + 1
print(cycle[offset + k])
'''
k%=csize+1
count = 0
curr = 1
count = 0
while count != k:
	curr = dic[count]
	count+=1
exit(print(curr))


from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
n , k = map(int , input().split())
arr = list(map(int , input().split()))
dic = defaultdict(int)
for i in range(1,n+1):
	dic[i] = arr[i-1]
cycle = []
visited = [False]*(10**6)
curr = 1
while True:
	cycle.append(curr)
	visited[curr] = True
	curr = dic[curr]
	#k -= 1
	if visited[curr]:
		break

csize = 0
ind = 0
for i in range(len(cycle)-1,-1,-1):
	if cycle[i] == curr:
		ind = i + 1
		break
	csize += 1
offset = len(cycle) - csize - 1
#print(cycle,csize+1, k , curr,offset)
if k - offset < 0:
	exit(print(cycle[k]))
k -= offset
k %= csize + 1
print(cycle[offset + k])


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Accepted