Problem p02684 (python)

Fixed submission:
import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(K):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(now_town)
    sys.exit()

roop = len(queue) - (queue[start] - 1)
before_len = queue[start]

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])

Buggy submission (verdict: Runtime Error):
import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(N):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(queue[now_town])
    sys.exit()

before_len = len(queue) - queue[start] - 1
roop = len(queue) - before_len

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])

Buggy submission (verdict: Runtime Error):
import sys

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

roop = 0
queue = [1]

start = -1

for now_town in queue:
    if not town_list[now_town - 1] in queue:
        queue.append(town_list[now_town - 1])

    # 循環した
    else:
        start = town_list[now_town - 1]
        break

if start == -1:
    print(queue[K])
    sys.exit()

before_len = queue.index(start)
roop = len(queue) - before_len

print(queue[before_len + (K - before_len) % roop])

Buggy submission (verdict: Runtime Error):
import sys

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

graph = {}
for index, value in enumerate(input().split()):
    graph[index + 1] = int(value)

queue = [1]
now_pos = 1
start = 0

for i in range(K):
    if not graph[now_pos] in queue:
        queue.append(graph[now_pos])
        now_pos = graph[now_pos]

    # 循環した
    else:
        start = graph[now_pos]
        break

before = queue[:queue.index(start)]
roop = queue[queue.index(start):]

for i in range(len(roop)):
    if (K - len(before)) % len(roop) == i:
        print(roop[i])
        sys.exit()

Buggy submission (verdict: Runtime Error):
import sys

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

graph = list(map(int, input().split()))
graph.insert(0, 0)

queue = [1]
now_pos = 1
start = 0

for i in range(K):
    if not graph[now_pos] in queue:
        queue.append(graph[now_pos])
        now_pos = graph[now_pos]

    # 循環した
    else:
        start = graph[now_pos]
        break

before = queue[:queue.index(start)]
roop = queue[queue.index(start):]

for i in range(len(roop)):
    if (K - len(before)) % len(roop) == i:
        print(roop[i])
        sys.exit()

Buggy submission (verdict: Runtime Error):
import sys

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

graph_seed = list(map(int, input().split()))
graph = {}

for index, route in enumerate(graph_seed):
    index += 1
    graph[index] = route

queue = [1]
now_pos = 1

start = 0

for i in range(K):
    if not graph[now_pos] in queue:
        queue.append(graph[now_pos])
        now_pos = graph[now_pos]

    # 循環した
    else:
        start = graph[now_pos]
        break

before = queue[:queue.index(start)]
roop = queue[queue.index(start):]

for i in range(len(roop)):
    if (K - len(before)) % len(roop) == i:
        print(roop[i])
        sys.exit()