Problem p02684 (python)

Fixed submission:
N,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
dp = [False]*(N+1)
tele = 0
for i in range(10**6):
    if i==0:
        tele = 1
        dp[tele]=True
    else:
        tele = A[tele]
        if dp[tele]==True:
            finish = i
            tele1 = tele
            break
        else:
            dp[tele]=True
#print(finish)
#print(tele)
tele = 0
for i in range(10**6):
    if i == 0:
        tele = 1
        if tele == tele1:
            start = i
            break
    else:
        tele = A[tele]
        if tele == tele1:
            start = i
            break
#print(start)
loop = finish-start
if K<=start:
    ans = 1
    for i in range(K):
        ans = A[ans]
    print(ans)
else:
    ans = 1
    for i in range(start+(K-start)%loop):
        ans = A[ans]
    print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
dp = [False]*(N+1)
tele = 0
for i in range(10**5):
    if i==0:
        tele = 1
        dp[tele]=True
    else:
        tele = A[tele]
        if dp[tele]==True:
            finish = i
            tele1 = tele
            break
        else:
            dp[tele]=True
#print(finish)
#print(tele)
tele = 0
for i in range(10**5):
    if i == 0:
        tele = 1
        if tele == tele1:
            start = i
            break
    else:
        tele = A[tele]
        if tele == tele1:
            start = i
            break
#print(start)
loop = finish-start
if K<=start:
    ans = 1
    for i in range(K):
        ans = A[ans]
    print(ans)
else:
    ans = 1
    for i in range(start+(K-start)%loop):
        ans = A[ans]
    print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
dp = [False]*(N+1)
tele = 0
# 同じものが出てきた時
for i in range(10**5):
    if i == 0:
        tele = 1
        dp[1] = True
    else:
        tele = A[tele]
        if dp[tele] == False:
            dp[tele] = True
        else:
            finish = i
            tele1 = tele
            break
tele = 0
# 始めてループの数が現れる時
for i in range(10**5):
    if i == 0:
        tele = 1
        if tele == tele1:
            start = i
            break
    else:
        tele = A[tele]
        if tele == tele1:
            start = i
            break
loop = finish-start
if K<start:
    ans = 0
    for i in range(K+1):
        if i==0:
            ans = 1
        else:
            ans = A[ans]
    print(ans)
else:
    ans = 0
    for i in range((K-start)%(finish-start)+1):
        if i == 0:
            ans = tele1
        else:
            ans = A[ans]
    print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
dp = [False]*(N+1)
tele = 0
# 同じものが出てきた時
for i in range(10**5):
    if i == 0:
        tele = 1
        dp[1] = True
    else:
        tele = A[tele]
        if dp[tele] == False:
            dp[tele] = True
        else:
            finish = i
            tele1 = tele
            break
tele = 0
# 始めてループの数が現れる時
for i in range(10**5):
    if i == 0:
        tele = 1
        if tele == tele1:
            start = i
            break
    else:
        tele = A[tele]
        if tele == tele1:
            start = i
            break
loop = finish-start
if K<start:
    ans = 0
    for i in range(K+1):
        if i==0:
            ans = 1
        else:
            ans = A[ans]
else:
    ans = 0
    for i in range((K-start)%(finish-start)+1):
        if i == 0:
            ans = tele1
        else:
            ans = A[ans]
print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
dp = [False]*(N+1)
tele = 0
for i in range(10**5):
    if i == 0:
        tele = A[1]
        dp[1] = True
    else:
        tele = A[tele]
        if dp[tele] == False:
            dp[tele] = True
        else:
            finish = i+1
            tele1 = tele
            break
tele = 0
for i in range(10**5):
    if i == 0:
        tele = 1
        if tele == tele1:
            start = i
            break
    else:
        tele = A[tele]
        if tele == tele1:
            start = i+1
            break
ans = A[1]
if K<start:
    for _ in range(K-1):
        ans = A[ans]
else:
    ans = tele1
    for _ in range((K-start)%(finish-start)):
        ans = A[ans]
print(ans)