Problem p02684 (python)

Fixed submission:
import sys
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

flg = [False for _ in range(N + 1)]
town = []
i = 1
cnt = 0
for _ in range(N):
    town.append(i)
    flg[i] = True
    i = A[i]

if len(town) >= K:
    print(town[K])
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s
    print(town[(K - to_s) % loop + to_s])

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

flg = [False for _ in range(N + 1)]
town = []
i = 1
cnt = 0
for i in range(N):
    town.append(i)
    flg[i] = True
    i = A[i]

if len(town) >= K:
    print(town[K])
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s
    print(town[(K - to_s) % loop + to_s])

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

flg = [False for _ in range(N + 1)]
town = []
i = 1
cnt = 0
while flg[i] == False or cnt > N:
    town.append(i)
    flg[i] = True
    i = A[i]
    cnt += 1

if len(town) >= K:
    print(town[K])
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s
    print(town[(K - to_s) % loop + to_s])

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

if K == 0:
    print(1)
    sys.exit()

flg = [False for _ in range(N + 1)]
town = []
i = 1
while flg[i] == False:
    town.append(i)
    flg[i] = True
    i = A[i]
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s

if len(town) >= K:
    print(town[K])
else:
    print(town[(K - to_s) % loop + to_s])

Buggy submission (verdict: Runtime Error):
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

flg = [False for _ in range(N + 1)]
town = []
i = 1
while flg[i] == False:
    town.append(i)
    flg[i] = True
    i = A[i]
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s

if len(town) >= K:
    print(town[K])
else:
    print(town[(K - to_s) % loop + to_s])

Buggy submission (verdict: Runtime Error):
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

flg = [False for _ in range(N + 1)]
town = []
i = 1
while flg[i] == False:
    town.append(i)
    flg[i] = True
    i = A[i]
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s

if len(town) >= K:
    print(town[K])
else:
    print(town[(K - to_s) % loop + to_s])

Buggy submission (verdict: Runtime Error):
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

flg = [False for _ in range(N + 1)]
town = []
i = 1
while flg[i] == False:
    town.append(i)
    flg[i] = True
    i = A[i]
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s

if len(town) >= K:
    print(town[K])
else:
    print(town[(K - to_s) % loop + to_s])

Buggy submission (verdict: Runtime Error):
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A -= 1

flg = [False for _ in range(N)]
loop = 0
town = []
i = 0
while flg[i] == False:
    town.append(i)
    flg[i] = True
    i = A[i]
    loop += 1
else:
    loop_start = i

print(town[K % loop + loop_start] + 1)