Problem p02684 (python)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = []
ord = [-1 for i in range(n+1)]
v = 1
c = 1
l = 0
while ord[v] == -1:
    ord[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - ord[v]
l = ord[v]
if k < l:
    print(s[k])
else:
    k -= l
    k %= c
    print(s[l+k])

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

gone = []

isLooped = False


def dfs(index):
    if index in gone:
        isLooped = True
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

last = gone[-1]


loopStartIndex = gone.index(Alist[last-1])
if K > loopStartIndex:
    divK = (K - loopStartIndex) % len(gone[loopStartIndex:])
    print(gone[divK])
else:
    print(gone[K])

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

gone = []

isLooped = False


def dfs(index):
    global isLooped
    if index in gone:
        isLooped = True
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

last = gone[-1]

if isLooped:
    loopStartIndex = gone.index(Alist[last-1])
    divK = K % len(gone[loopStartIndex:])
    if divK < loopStartIndex:
        print(gone[divK])
    else:
        print(gone[divK])
else:
    print(gone[K])

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

gone = []

isLooped = False


def dfs(index):
    global isLooped
    if index in gone:
        isLooped = True
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

last = gone[-1]

if isLooped:
    loopStartIndex = gone.index(Alist[last-1])
    divK = K % len(gone[loopStartIndex:])
    print(divK)
    if divK < loopStartIndex:
        print(gone[divK])
    else:
        (gone[divK+1])

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

gone = []

isLooped = False


def dfs(index):
    global isLooped
    if index in gone:
        isLooped = True
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

last = gone[-1]

if isLooped:
    loopStartIndex = gone.index(Alist[last-1])
    divK = K % len(gone[loopStartIndex:])
    print(Alist[divK])

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

gone = []

isLooped = False


def dfs(index):
    global isLooped
    if index in gone:
        isLooped = True
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

if isLooped:
    last = gone[-1]
    loopStartIndex = gone.index(Alist[last-1])
    divK = K % len(gone[loopStartIndex:])
    print(Alist[divK])

else:
    print(Alist[K-1])

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

gone = []


def dfs(index):
    if index in gone:
        return

    gone.append(index)
    dfs(Alist[index-1])


dfs(1)

for g in range(0, len(gone)-1):
    if Alist[gone[g]-1] == gone[-1]:
        gone = gone[g-1:]
        index = g-1


ansi = K % len(gone)

print(gone[ansi-index])