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):
N, K = map(int, input().split(" "))
towns = list(map(lambda x: int(x)-1, input().split(" ")))
assert len(towns) == N

print(towns)

score = {0: 0}
current = 0
move = 0
while 1:
  next_town = towns[current]
  move += 1
  # print(f"move {current} to {next_town}")
  if score.get(next_town) is not None:
    # print(f"find loop!")
    loop_move = move - score.get(next_town)
    loop_town = next_town
    until_loop = score.get(next_town)
    # print(f"loop start town : ", loop_town)
    # print(f"cost to start to loop town : ", until_loop)
    # print(f"loop length : ", loop_move)
    break
  score[next_town] = move
  current = next_town

if K < until_loop:
  result = 0
  for k in range(K):
    result = towns[result]
else:
  result = loop_town
  for k in range((K - until_loop) % loop_move):
    result = towns[result]
print(result + 1)



visited = []
current = 0
while 1:
  if K == 0:
    break
  current = towns[current]
  K -= 1
  if current in visited:
    L = 0
    tcurrent = current
    while 1:
      tcurrent = towns[tcurrent]
      L += 1
      if tcurrent == current:
        break
    break
  visited.append(current)

K = K % L
for _ in range(K):
  current = towns[current]
print(current + 1)

N, K = map(int, input().split(" "))
towns = list(map(lambda x: int(x)-1, input().split(" ")))
assert len(towns) == N

# print(towns)

# score = {0: 0}
# current = 0
# move = 0
# while 1:
#   next_town = towns[current]
#   move += 1
#   # print(f"move {current} to {next_town}")
#   if score.get(next_town) is not None:
#     # print(f"find loop!")
#     loop_move = move - score.get(next_town)
#     loop_town = next_town
#     until_loop = score.get(next_town)
#     # print(f"loop start town : ", loop_town)
#     # print(f"cost to start to loop town : ", until_loop)
#     # print(f"loop length : ", loop_move)
#     break
#   score[next_town] = move
#   current = next_town


#   result = 0
#   for k in range(K):
#     result = towns[result]
# else:
#   result = loop_town
#   for k in range((K - until_loop) % loop_move):
#     result = towns[result]
# print(result + 1)

visited = set([])
current = 0
while 1:
  if K == 0:
    break
  current = towns[current]
  K -= 1
  if current in visited:
    L = 0
    tcurrent = current
    while 1:
      tcurrent = towns[tcurrent]
      L += 1
      if tcurrent == current:
        break
    break
  visited.add(current)

K = K % L
print(K)
for _ in range(K):
  current = towns[current]
print(current + 1)


N, K = map(int, input().split(" "))
towns = list(map(lambda x: int(x)-1, input().split(" ")))
assert len(towns) == N

# print(towns)

# score = {0: 0}
# current = 0
# move = 0
# while 1:
#   next_town = towns[current]
#   move += 1
#   # print(f"move {current} to {next_town}")
#   if score.get(next_town) is not None:
#     # print(f"find loop!")
#     loop_move = move - score.get(next_town)
#     loop_town = next_town
#     until_loop = score.get(next_town)
#     # print(f"loop start town : ", loop_town)
#     # print(f"cost to start to loop town : ", until_loop)
#     # print(f"loop length : ", loop_move)
#     break
#   score[next_town] = move
#   current = next_town


#   result = 0
#   for k in range(K):
#     result = towns[result]
# else:
#   result = loop_town
#   for k in range((K - until_loop) % loop_move):
#     result = towns[result]
# print(result + 1)

L = None
visited = set([])
current = 0
while 1:
  if K == 0:
    break
  current = towns[current]
  K -= 1
  if current in visited:
    L = 0
    tcurrent = current
    while 1:
      tcurrent = towns[tcurrent]
      L += 1
      if tcurrent == current:
        break
    break
  visited.add(current)

if L is not None:
  K = K % L
  
  for _ in range(K):
    current = towns[current]
print(current + 1)


Verdicts: Wrong Answer

Runtime Error

Runtime Error

Accepted