Problem p02684 (python)

Fixed submission:
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(1, k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            cyc = (k - visited[town])%(i - visited[town]) + visited[town]
            print(visited.index(cyc))
            exit()
    print(town)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            print(visited.index( (k - visited[town])%(i + visited[town]) + visited[town] ))
            exit()
    print(town)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            print(visited.index( (k - visited[town])%(i + visited[town]) + visited[town]))
            exit()
    print(town)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(1, k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            cyc = (k - visited[town])%(i + visited[town]) + visited[town]
            print(visited.index(cyc))
            exit()
    print(town)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    towns = [1]
    i = 0
    while a[i] not in towns and len(towns) < k:
        towns.append(a[i])
        i = a[i] - 1
    ti = towns.index(a[i])
    if ti+1 > k:
        print(towns[ti])
    else:
        print(towns[(k - ti)%(len(towns) - ti) + ti])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    towns = [1]
    i = 0
    while a[i] not in towns and len(towns) < k:
        towns.append(a[i])
        i = a[i] - 1
    ti = towns.index(a[i])
    if ti+1 > k:
        print(towns[ti])
    else:
        print(towns[(k - ti)%(len(towns) - ti) + ti])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    towns = [1]
    i = 0
    while a[i] not in towns:
        towns.append(a[i])
        i = a[i] - 1
    ti = towns.index(a[i]) + 1
    if ti > k:
        print(towns[ti - 1])
    else:
        print(towns[(k - ti)%(len(towns) - ti) + ti])

if __name__ == "__main__":
    main()