Problem: The Kingdom of Takahashi has N towns, numbered 1 through N.  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.  Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.

Submission attempts (in order):
n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]
print(next)
# print(link)

link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0
# if len(link2) == 1 :
#     print('a')
#     rep = len(link)+1-len(link2)
# else:
print('b')
rep = len(link)- len(link2)

if(k > len(link2)):
    # if len(link2) == 1:
    #     print('c')
    #     p = (k-len(link2)+2)%rep
    #     print(p)

    print('d')
    p = (k-len(link2)+1)%rep
    # print(p)
    #
    # print(next)
    for i in range(p+rep-1):
        next = a[next-1]
    print('e')
else:
    print('f')
    next = 0
    for i in range(k):
        next = a[next-1]

ans = next
print(link)
print(link2)
print(ans)


n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]

print(next)
link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0

rep = len(link) - len(link2)
print(rep)
if (len(link2) == 0):
    p = k%rep
    for i in range(p+rep):
        next = a[next-1]
elif(k > len(link2)):
    p = (k-len(link2))%rep
    for i in range(p+rep):
        next = a[next-1]
else:
    next = 0
    for i in range(k-1):
        next = a[next-1]

ans = next
print(ans)


n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]

link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0

rep = len(link)- len(link2)

if(k > len(link2)):
    p = (k-len(link2))%rep
    for i in range(p+rep-1):
        next = a[next-1]
else:
    next = 0
    for i in range(k-1):
        next = a[next-1]

ans = next
print(ans)


n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]

# print(next)
link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0

rep = len(link) - len(link2)
# print(rep)
if (len(link2) == 0):
    p = k%rep
    # print('e')
    for i in range(p+rep):
        next = a[next-1]
elif(k > len(link2)):
    # print('f')
    p = (k-len(link2))%rep
    # print(p)
    # print(next)
    for i in range(p+rep):
        next = a[next-1]
else:
    # print('d')
    next = a[0]
    for i in range(k-1):
        next = a[next-1]
# print(next)
ans = next
# print(link,link2)
print(ans)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted