Problem p02684 (python)

Fixed submission:
N, K = map(int, input().split(' '))
A = list(map(int, input().split(' ')))

visited = [False for _ in range(N)]
now = 0

cycle = []

while True:
    cycle.append(now)
    visited[now] = True
    now = A[now] - 1

    if visited[now]:
        break

total_len = len(cycle)

cycle_start = 0

for i in range(total_len):
    if cycle[i] == now:
        cycle_start = i
        break

cycle_len = total_len - cycle_start

if K < cycle_start:
    print(cycle[K] + 1)
else:
    print(cycle[cycle_start + (K - cycle_start) % cycle_len] + 1)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split(' '))
A = list(map(int, input().split(' ')))

A_itta = [None] * N
# A_cycle = '1'

A_stayed = []
# A_stayed.append(1)

is_looped = None
jump_cnt = 0

i = 0
while True:
    # for i in range(K):
    # while True:

    # 行ったことある町についた
    if A_itta[i]:
        is_looped = 1
        break

    # 訪問済みフラグ
    A_itta[i] = 1

    # 訪問順リスト
    A_stayed.append(i + 1)

    # 次の町のidx
    i = A[i] - 1
    jump_cnt += 1

    # ジャンプ回数が上限を超えた
    if jump_cnt > K:
        is_looped = 0
        break

# print('ループするまでにいった町', A_stayed)

# 最初の町からループする場合
if is_looped and i == 0:
    cycle = A_stayed
    ans = cycle[K % len(cycle)]
elif is_looped and i > 0:
    cycle = A_stayed[i + 1:]
    K -= i + 1
    ans = cycle[K % len(cycle)]
else:
    cycle = 'no cycle'
    ans = A_stayed[-1]

# print('i', i)
# print('loop=', cycle)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split(' '))
A = list(map(int, input().split(' ')))

A_itta = [None] * N
# A_cycle = '1'

A_stayed = []
# A_stayed.append(1)

i = 0
while True:
    # 行ったことある
    if A_itta[i]:
        break

    # 訪問済みフラグ
    A_itta[i] = 1

    # 訪問順リスト
    A_stayed.append(i + 1)

    # 次の町のidx
    i = A[i] - 1

# print('ループするまでにいった町', A_stayed)

# 最初の町からループする場合
if i == 0:
    cycle = A_stayed
else:
    cycle = A_stayed[i+1:]
    K -= i+1

ans = cycle[K % len(cycle)]

# print('i', i)
# print('loop=', cycle)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split(' '))
A = list(map(int, input().split(' ')))

A_itta = [None] * N
# A_cycle = '1'

A_stayed = []
A_stayed.append('1')

i = 0
while True:
    # print('stay', i)

    # 訪問フラグ
    A_itta[i] = 1

    # 訪問リスト
    # A_cycle = A_cycle + str(A[i])
    A_stayed.append(str(A[i]))

    # 次の番号
    i = A[i] - 1

    # print('next', i)

    # 行ったことある
    if A_itta[i]:
        # print('loop at=', i)
        break

# 最初の町からループする場合
if i == 0:
    # cycle = A_cycle[0:-1]
    cycle = A_stayed[0:-1]
    ans = cycle[K % len(cycle)]

else:
    # cycle = A_cycle[i + 1:-1]
    cycle = A_stayed[i+1:-1]
    ans = cycle[(K - (i + 1)) % len(cycle)]

# print(cycle)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split(' '))
A = list(map(int, input().split(' ')))

A_itta = [None] * N
A_cycle = '1'

i = 0
while True:
    # print('stay', i)

    # 訪問フラグ
    A_itta[i] = 1

    # 訪問リスト
    A_cycle = A_cycle + str(A[i])

    # 次の番号
    i = A[i] - 1

    # print('next', i)

    # 行ったことある
    if A_itta[i]:
        # print('loop at=', i)
        break

# 最初の町からループする場合
if i == 0:
    cycle = A_cycle[0:-1]
    ans = cycle[K % len(cycle)]

else:
    cycle = A_cycle[i + 1:-1]
    ans = cycle[(K - (i + 1)) % len(cycle)]

print(ans)