Problem p02684 (python)

Fixed submission:
import sys
import os

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    # int(input())
    N,K=map(int, input().split())
    A=list(map(int, input().split()))

    d=1
    keys=list(range(N))
    li={s:-1 for s in keys}
    li[0]=0

    roop=0
    pre_d=0
    for i in range(1,K+1):
        pre_d=d
        d=A[d-1]
        # print("i:"+str(i)+" "+str(pre_d)+"->"+str(d))
        if li[d-1]!=-1:
            roop=i-li[d-1]
            # print("detect_roop:"+str(i)+"~"+str(li[d-1]))
            break
        else:
            li[d-1]=i

    if roop!=0:
        k=1
        for j in range((K-li[d-1]-1)%roop+li[d-1]+1):
            k=A[k-1]
        print(k)
    else:
        print(d)


if __name__ == '__main__':
    main()

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

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    # int(input())
    N,K=map(int, input().split())
    A=list(map(int, input().split()))

    d=1
    keys=list(range(N))
    li={s:-1 for s in keys}
    li[0]=0

    pre_d=0
    i=0
    while True:
        i+=1
        pre_d=d
        d=A[d-1]
        if li[d-1]!=-1:
            break
        else:
            li[d-1]=i

    # print(pre_d)
    # print(d)
    # roop=pre_d-d
    roop=i-li[d-1]
    print(A[(K-d)%roop+d])


if __name__ == '__main__':
    main()

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

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    # int(input())
    N,K=map(int, input().split())
    A=list(map(int, input().split()))

    d=1
    keys=list(range(N))
    li={s:-1 for s in keys}
    li[0]=0

    pre_d=0
    i=0
    while True:
        i+=1
        pre_d=d
        d=A[d-1]
        if li[d-1]!=-1:
            break
        li[d-1]=i

    # print(pre_d)
    # print(d)
    # roop=pre_d-d
    roop=i-li[d-1]
    print(A[(K-d)%roop+d])


if __name__ == '__main__':
    main()

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

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    # int(input())
    N,K=map(int, input().split())
    A=list(map(int, input().split()))

    d=1
    keys=list(range(N))
    li={s:-1 for s in keys}
    li[0]=0

    pre_d=0
    while True:
        pre_d=d-1
        d=A[d-1]
        if li[d-1]==0:
            break
        li[d-1]=0

    # print(pre_d)
    # print(d)
    roop=pre_d-d+1
    print(A[(K-d)%roop+d])


if __name__ == '__main__':
    main()

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

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    # int(input())
    N,K=map(int, input().split())
    A=list(map(int, input().split()))

    d=1
    li=[False]*N
    li[0]=True

    pre_d=0
    while True:
        pre_d=d-1
        d=A[d-1]
        if li[d-1]:
            break
        li[d-1]=True

    # print(pre_d)
    # print(d)
    roop=pre_d-d+1
    print(A[(K-d)%roop+d])


if __name__ == '__main__':
    main()

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

def file_input():
    f = open('ABC167/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    # int(input())
    N,K=map(int, input().split())
    A=list(map(int, input().split()))

    d=1
    # for i in range(K):
    #     # print(d)
    #     d=A[d-1]
    # li=[1]
    li=[False]*N
    li[0]=True

    while True:
        d=A[d-1]
        if li[d-1]:
            break
        # li.append(d)
        li[d-1]=True

    d_p=A.index(d)
    # print(d_p)
    roop=d-1-d_p
    # print(K-d_p)
    # print(len(li))
    # if len(li)>K:
    #     # print(K-1)
    #     print(li[K-1])
    # else:
    # print((K-d_p)%roop)
    print(A[(K-d_p)%roop+d_p])

    # print(d)

if __name__ == '__main__':
    main()