TnT/Multi_CodeNet4Repair huggingface dataset:
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):
from collections import deque

N, M = map(int, input().split())
AB = [map(int, input().split()) for _ in range(M)]

links = [[] for _ in range(N + 1)]
for a, b in AB:
    links[a].append(b)
    links[b].append(a)

result = [-1] * (N + 1)
q = deque([1])
while q:
    i = q.popleft()
    for j in links[i]:
        if result[j] == -1:
            result[j] = i
            q.append(j)
print('Yes')
print('\n'.join(str(i) for i in result[2:]))

num_town, num_move = map(int, input().split())
teleports = list(map(lambda x: int(x) - 1, input().split()))

current = 0
memo = {}
for i in range(num_move):
    if memo.get(current) == 2:
        break
    if not memo.get(current):
        memo[current] = 1
    else:
        memo[current] += 1
    destination = teleports[current]
    current = destination

loop = {k:v for k,v in memo.items() if v==2}
minus = {k:v for k,v in memo.items() if v==1}

new_move_count = 0
if len(loop) == 0:
    current = 0
    new_move_count = num_move
else:
    new_move_count = (num_move - len(minus)) % len(loop)

for i in range(new_move_count):
    destination = teleports[current]
    current = destination

print(current + 1)


Verdicts: Runtime Error

Accepted
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()))

c = [0]*N
cc = [0]
c[0] = 1
i = 0
while True:
  c[A[i]-1] += 1  
  cc.append(A[i]-1)
  if c[A[i]-1] > 1:
    memo = A[i]-1
    break
  i = A[i] - 1  
    
used = [i for i in range(N) if c[i] > 0]

pos = [i for i in range(len(cc)) if cc[i] == memo]

res = pos[1] - pos[0]

if K < pos[0]+1:
  for i in range(K):
    c[A[i]-1] += 1  
    i = A[i] - 1
  ans = i+1
else:
  i = (K - (pos[0]+1)) % res
  ans = cc[i]
  
print(ans)

N, K = map(int, input().split())
A = list(map(int, input().split()))

c = [0]*N
cc = [0]
c[0] = 1
i = 0
while True:
  c[A[i]-1] += 1  
  cc.append(A[i]-1)
  if c[A[i]-1] > 1:
    memo = A[i]-1
    break
  i = A[i] - 1  
    
used = [i for i in range(N) if c[i] > 0]

pos = [i for i in range(len(cc)) if cc[i] == memo]

res = pos[1] - pos[0]
loop = [i for i in cc[pos[0]:pos[1]]]

if K < N:
  ans = cc[K]+1
else:
  i = (K - (pos[0])) % res
  ans = loop[i]+1
  
print(ans)

Verdicts: Wrong Answer

Accepted
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 = list(map(int, input().split()))
A = list(map(int, input().split()))

l = [1, A[0]]
a = A[0] - 1
for _ in range(0, N):
    if A[a] not in l:
        l.append(A[a])
        a = A[a] - 1
    else:
        a = A[a]
        break
n = len(l)
print(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i
        break
T = len(l)
print(l)
S = (K - b) % T
ans = l[S]
print(S)
print(b)

print(ans)

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

l = [A[0]]
a = A[0] - 1
for _ in range(0, N):
    if A[a] not in l:
        l.append(A[a])
        a = A[a] - 1
    else:
        break
        a = A[a]
n = len(l)
print(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i - 1
        break
N = len(A)
S = (K - b) % N
ans = A[S - 1]

print(ans)

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

l = [A[0]]
s = {A[0]}
a = A[0] - 1
for _ in range(K):
    if A[a] not in s:
        l.append(A[a])
        s.add(A[a])
        a = A[a] - 1
    else:
        a = A[a]
        break
n = len(l)
b = 0
for i in range(n):
    if l[i] == a:
        del l[0:i]
        b = i
        break
T = len(l)
S = (K - b) % T
ans = l[S - 1]

print(ans)


Verdicts: Wrong Answer

Wrong Answer

Accepted
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 = input().split()
A_MAP = {}
for i in range(N):
    A_MAP[str(i+1)] = A[i]

print(A_MAP)
route_hash = {'1': 0}
current_pos = '1'
next_pos = ''
counter = 0
while counter <= K:
    next_pos = A_MAP[current_pos]
    counter+=1
    if next_pos in route_hash:
        break
    route_hash[next_pos] = counter 
    current_pos = next_pos

if counter >= K:
    print(current_pos)
    exit()

print(route_hash, current_pos, counter)
remain = K - counter
counter -= route_hash[next_pos]
remain = remain % counter
print(K, counter, remain, next_pos)

current_pos = next_pos
for i in range(remain):
    next_pos = A_MAP[current_pos]
    route_hash[next_pos] = counter 
    current_pos = next_pos
print(current_pos)


N, K = map(int, input().split())
A = input().split()
A_MAP = {}
for i in range(N):
    A_MAP[str(i+1)] = A[i]

route_hash = {'1': 0}
current_pos = '1'
next_pos = ''
counter = 0
breaked = False
while counter < K:
    next_pos = A_MAP[current_pos]
    counter+=1
    if next_pos in route_hash:
        breaked = True
        break
    route_hash[next_pos] = counter 
    current_pos = next_pos

if not breaked:
    print(current_pos)
    exit()

remain = K - counter
counter -= route_hash[next_pos]
remain = remain % counter

current_pos = next_pos
for i in range(remain):
    next_pos = A_MAP[current_pos]
    route_hash[next_pos] = counter 
    current_pos = next_pos
print(current_pos)


Verdicts: Wrong Answer

Accepted
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())
num=[0 for i in range(n)]
first=[0 for i in range(n)]
first[0]=1
a=list(map(int,input().split()))
for i in range(n):
  a[i]-=1
num[0]=1
loop=0
s=0
for i in range(n):
  if num[a[s]]==0:
    num[a[s]]+=1
    first[a[s]]=i
  if num[a[s]]==2:
    loop1=loop
    break
  s=a[s]

kai=first[a[s]]+(k-first[a[s]])%(loop1-first[a[s]])
s=0
for i in range(kai):
  s=a[s]
print(s+1)

n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
  a[i]-=1
kai=[-1 for i in range(n)]
nx=0
now=0
kai[0]=0

if k<=n:
  for i in range(k):
    nx=a[now]
    now=nx
    #print(nx)
  ans=nx+1
else:
  for i in range(n):
    nx=a[now]
    if kai[nx]==-1:
      kai[nx]=i+1
      now=nx
    else:
      T=i+1-kai[nx]
      break
  times=kai[nx]+(k-kai[nx])%T
  now=0
  nx=0
  for i in range(times):
    nx=a[now]
    now=nx
    #print(nx)
  ans=nx+1
print(ans)

Verdicts: Runtime Error

Accepted
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_in=map(int, input().split())
a_l=list(map(int, input().split()))

def f(s):
    a = s
    b = s
    i=1
    while True:
        a = a_l[a-1]
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        i+=1

    j=1
    a = a_l[0]
    while True:
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        j+=1

    k=1
    while True:
        a = a_l[a-1]
        a = a_l[a-1]
        b = a_l[b-1]
        if a == b:
            break
        k+=1

    return j, k

lam, myu = f(a_l[0])

pos=[]
pos.append(a_l[0])
for i in range(n):
    pos.append(a_l[pos[i]])

if k_in<=lam:
    print(pos[k_in-1])
else:
    k_in=(k_in-lam)%myu
    print(pos[lam+k_in-1])


n, k=map(int, input().split())
a=list(map(int, input().split()))

for i in range(n):
    a[i]-=1

rec=[-1]*n
turn=0
current=0

while True:
    if k<=0:
        print(current+1)
        break
    if rec[current]==-1:
        rec[current]=turn
        turn+=1
        current=a[current]
        k-=1
    else:
        loop=turn-rec[current]
        k%=loop
        rec=[-1]*n


Verdicts: Runtime Error

Accepted
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()))
a.insert(0,0)
print(a)

town =1
visited = [0] * (n+1)

for i in range(1,k+1):
    town = a[town]
    if visited[town] !=0:
        rest =(k - visited[town]) % (i -visited[town])
        goal =visited.index(visited[town]+rest)
        break
    visited[town] = i 
    

print(goal)



#input = sys.stdin.readline

def main():
    N, K = map(int,input().split())
    A = list(map(int,input().split()))
    A = [0] +A  
    visited = [0] * (N+1)
    town = 1
    cycle = 0
    for i in range(1,K+1):
        town = A[town]
        if visited[town] ==0:
            visited[town] = i
        else:
            cycle = i - visited[town]
            place = (K - visited[town]) % cycle + visited[town]
            print(visited.index(place))
            exit()
    print(town)




if __name__ == "__main__":
    main()

Verdicts: Runtime Error

Accepted
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):
def check(pair,m,x):
  l = len(pair)
  sum = 0
  
  for i in range(1,m):
    p = 0
    for j in range(l):
      p += pair[j][i]
    
    if(p < x):
      return -1
    
  for e in pair:
    sum += e[0]
  return sum
      
n,m,x = (int(x) for x in input().split())
a = []
for i in range(n):
  a.append(tuple([int(x) for x in input().split()]))
  
min = -1
  
for i in range(2 ** n):
  pair = []
  for j in range(n):
    if((i >> j) & 1):
      pair.append(a[j])
      
  sum = check(pair,m,x)
  if sum != -1:
    if min == -1:
      min = sum
    elif min > sum:
      min = sum
    
print(min)

n,k = (int(x) for x in input().split())
a = tuple([int(x) for x in input().split()])
 
c = [0] + [-1]*(n)
l = [1]
now = 1
dupl = -1

for i in range(k):
  now = a[now - 1]
  c[now - 1] += 1
   
  if c[now - 1] == 1:
    dupl = now
    break
  else:
    l.append(now)
    
if dupl == -1:
  print(now)
  exit()
       
notloop = l.index(dupl)
length = len(l)-notloop
print(l[(k-notloop)%length+notloop])

Verdicts: Runtime Error

Accepted
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=[int(i)-1 for i in input().split()]
p=0
visited=[0]*n
first=[]
roop=[]
while visited[p]!=2:
    if visited[p]==0:
        first.append(p)
    else:
        roop.append(p)
    visited[p]+=1
    p=a[p]
print(first,roop)
if len(first)>k:
    print(first[k]+1)
else:
    print(roop[(k-(len(first)-len(roop)))%len(roop)]+1)

n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
p=0
visited=[0]*n
first=[]
roop=[]
while visited[0]!=2:
    if visited[p]==0:
        first.append(p)
    else:
        roop.append(p)
    visited[p]+=1
    p=a[p]
if len(first)>k:
    print(first[k])
else:
    print(roop[(k-len(first)-len(roop))%len(roop)])

n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
p=0
visited=[0]*n
first=[]
roop=[]
while visited[0]!=2:
    if visited[p]==0:
        first.append(p)
    else:
        roop.append(p)
    visited[p]+=1
    p=a[p]
if len(first)>k:
    print(first[k])
else:
    print(roop[(k-(len(first)-len(roop)))%len(roop)])

n,k=map(int,input().split())
a=[int(i)-1 for i in input().split()]
p=0
visited=[0]*n
first=[]
roop=[]
while visited[p]!=2:
    if visited[p]==0:
        first.append(p)
    else:
        roop.append(p)
    visited[p]+=1
    p=a[p]
if len(first)>k:
    print(first[k]+1)
else:
    print(roop[(k-(len(first)-len(roop)))%len(roop)]+1)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted
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):
# -*- coding: utf-8 -*-

import math


def get_input() -> tuple:
    
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    return N, K, A


def main(N: int, K: int, A: list) -> None:
    
    
    to = [A]
    for i in range(math.ceil(math.log2(K))):
        to_i = to[i]
        to_next = []
        
        if ((K >> i) & 1):
            for j in range(N):
                to_next.append(to_i[to_i[j]-1])

        to.append(to_next)

    result = 1
    for i in range(math.ceil(math.log2(K))):
        if ((K >> i) & 1):
            result = to[i][result-1]

    print(result)


if __name__ == "__main__":
    N, K, A = get_input()
    main(N, K, A)


# -*- coding: utf-8 -*-

import math


def get_input() -> tuple:
    
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))

    return N, K, A


def main(N: int, K: int, A: list) -> None:
    
    
    to = [A]
    for i in range(math.ceil(math.log2(K))):
        to_i = to[i]
        to_next = []
        for j in range(N):
            to_next.append(to_i[to_i[j]-1])

        to.append(to_next)

    result = 1
    for i in range(math.ceil(math.log2(K))):
        if ((K >> i) & 1):
            result = to[i][result-1]

    print(result)


if __name__ == "__main__":
    N, K, A = get_input()
    main(N, K, A)


Verdicts: Runtime Error

Accepted
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(lambda x: int(x) - 1, input().split()))
P = [-1] * N 
cur = 0
P[cur] = 0
route = [1]

for step in range(K):
  cur = A[cur]
  route.append(cur + 1)
  if P[cur] >= 0:
    l_start = P[cur]
    l_end = step + 1
    l_len = l_end - l_start
    break
  else:
    P[cur] = step + 1
else:
  print(cur)
  quit()
  

print(route,l_start,l_end)
print(route[l_start + ((K - l_start) % l_len)])

N,K = map(int,input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
P = [-1] * N 
cur = 0
P[cur] = 0
route = [1]

for step in range(K):
  cur = A[cur]
  route.append(cur + 1)
  if P[cur] >= 0:
    l_start = P[cur]
    l_end = step + 1
    l_len = l_end - l_start
    break
  else:
    P[cur] = step + 1
else:
  print(cur + 1)
  quit()

print(route[l_start + ((K - l_start) % l_len)])

Verdicts: Wrong Answer

Accepted
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):
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(100**5)

def search(u, n, g, disc, k):
    if disc[u] != None:
        circles = n - disc[u]
        v = (k - disc[u]) % circles
        ans = u
        for i in range(v):
            ans = g[ans]
        return ans + 1
    disc[u] = n
    return search(g[u], n+1, g, disc, k)

def resolve():
    n, k = map(int, input().split())
    g = list(map(lambda x: int(x) - 1, input().split()))
    disc = [None]*n
    v = search(0, 0, g, disc, k)
    print(v)
resolve()

import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**5)

def search(u, n, g, disc, k):
    if disc[u] != None:
        circles = n - disc[u]
        v = (k - disc[u]) % circles
        ans = u
        for i in range(v):
            ans = g[ans]
        return ans + 1
    disc[u] = n
    return search(g[u], n+1, g, disc, k)
resolve()

def resolve():
	n, k = map(int, input().split())
	a = list(map(int, input().split()))
	rec = [True]*(n+2)
	now = 0
	stp = [1]

	while rec[now]:
		rec[now] = False
		now = a[now] - 1
		stp.append(now+1)
	before_loop = stp.index(now+1)
	loop = stp[before_loop:-1]
	if k <= (before_loop):
		print(stp[k])
	else:
		r = (k - before_loop) % len(loop)
		print(loop[r])
resolve()

Verdicts: Runtime Error

Runtime Error

Accepted
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 = [0] + list(map(int,input().split()))

journey = [1]

for i in range(N):
    nxt = a[journey[-1]]
    journey.append(nxt)

if K < N:
    print(journey[K])
    exit()

loop_start = N-1
loop_end   = N

finished = False
for i in range(N):
    for j in range(i,N+1):
        if journey[i] == journey[j]:
            loop_start = i
            loop_end   = j
            finished = True
            break
    if finished:
        break

period = loop_end - loop_start

ans = (K - loop_start) % period
print(ans)

N, K = map(int,input().split())
a = [0] + list(map(int,input().split()))

journey = [1]

for i in range(N):
    nxt = a[journey[-1]]
    journey.append(nxt)

if K < N:
    print(journey[K])
    exit()

loop_start = N-1
loop_end   = N

finished = False
for i in range(1,N):
    for j in range(i+1,N+1):
        if journey[i] == journey[j]:
            loop_start = i
            loop_end   = j
            finished = True
            break
    if finished:
        break

period = loop_end - loop_start

K %= period
while K < period:
  K += period

print(journey[K])


N, K = map(int,input().split())
a = [0] + list(map(int,input().split()))

journey = [1]

for i in range(N):
    nxt = a[journey[-1]]
    journey.append(nxt)

if K < N:
    print(journey[K])
    exit()

loop_start = N-1
loop_end   = N

finished = False
for i in range(1,N):
    for j in range(i+1,N+1):
        if journey[i] == journey[j]:
            loop_start = i
            loop_end   = j
            finished = True
            break
    if finished:
        break

period = loop_end - loop_start

ans = K % period
while ans < period:
  ans += period

print(journey[ans])


N,K = map(int,input().split())
a = list(map(int,input().split()))

ans = a[K % N]
print(ans)

N, K = map(int,input().split())
a = [0] + list(map(int,input().split()))

journey = [1]

for i in range(2*N+1000):
    nxt = a[journey[-1]]
    journey.append(nxt)

if K <= N:
    print(journey[K])
    exit()

loop_start = N-1
loop_end   = N
 
while(journey[loop_start] != journey[loop_end]):
    loop_start -= 1
    
period = loop_end - loop_start

K %= period
while K < N:
  K += period

print(journey[K])


Verdicts: Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Accepted
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()))

town = [0 for i in range(N)]

def next(A,now):
    return(A[now-1])

now = 1
i = 0
while True:
    if town[now-1] != 0:
        roop_start = town[now-1]
        roop_end = i
        break
    town[now-1] = i+1
    now = next(A,now)
    i += 1

roop = roop_end - roop_start + 1
teleport = roop_start + (K - roop_start)%roop
print(teleport)
now = 1
for i in range(teleport):
    now = next(A,now)

print(now)


N,K = map(int,input().split())
A = list(map(int,input().split()))

town = [0 for i in range(N+1)]

def next(A,now):
    return(A[now-1])

now = 1
i = 0
while True:
    if town[now] != 0:
        roop_start = town[now]
        roop_end = i
        break
    town[now] = i+1
    now = next(A,now)
    i += 1

roop = roop_end - roop_start + 1
teleport = min(roop_start + (K - roop_start)%roop,K)

now = 1
for i in range(teleport):
    now = next(A,now)

print(now)


Verdicts: Wrong Answer

Accepted
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):
root = list()


def dfs(A, num, N):
    if A[num] in root:
        return root.index(A[num])
    else:
        root.append(A[num])
        return dfs(A, A[num]-1, N)


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

    start = dfs(A, 0, N)

    if K <= len(root):
        print(root[K-1])
    else:
        index = (K - start) % (len(root)-start)
        print(root[index])


if __name__ == "__main__":
    main()


import sys
sys.setrecursionlimit(10**6)

root = list()


def dfs(A, num, N, memo):
    if memo[A[num]-1] == 1:
        return root.index(A[num])
    else:
        root.append(A[num])
        memo[A[num]-1] = 1
        return dfs(A, A[num]-1, N, memo)


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

    memo = [0] * N
    start = dfs(A, 0, N, memo)

    if K <= len(root):
        print(root[K-1])
    else:
        index = (K - start) % (len(root)-start)
        loop = root[start:]
        print(loop[index-1])


if __name__ == "__main__":
    main()


Verdicts: Runtime Error

Accepted
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()))
s=1
B=[1]
for i in range(K):
  s=A[s-1]
  if s in B:
    u=B.index(s)
    u_=len(B)-u
    t=(K-u+1)%u_
    if t>=1 and u>=1:
      print(B[u+t-1])
      break
    elif u==0:
      t=(K+1)%u_
      print(B[t-1])
      break
    else:
      print(B[u])
      break
  else:
    B.append(s)
 
print(s)      


n,k=map(int,input().split())
a=list(map(int,input().split()))
visit=[0]*n
visit[0]=1
now=0
c=0
for i in range(k):
  now_=now
  now=a[now]-1
  if visit[now]==0:
    visit[now]=visit[now_]+1
  else:
    c=1
    break
if c==1:
  t=(k+2-visit[now])%(visit[now_]-visit[now]+1)
  if t==0:
    print(now_+1)
  else:
    i=now
    for _ in range(t-1):
      i=a[i]-1
    print(i+1) 
else:
  u=now+1
  print(u)
  

Verdicts: Wrong Answer

Accepted
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()))
alist=[]
i=1
while a[i-1] not in alist:
  i=a[i-1]
  alist.append(i)
loop1=alist.index(a[i-1])
if k<loop1+1:
  print(alist[k-1])
else:
  lenloop=len(alist)-loop1
  print(alist[((k-loop1-1)%lenroop)+loop1])

n,k=map(int, input().split())
a=list(map(int, input().split()))
alist=[]
cnt=0
i=1
blist=[0]*(n+1)
while blist[a[i-1]]==0:
  i=a[i-1]
  alist.append(i)
  blist[i]=1
  cnt+=1
loop1=alist.index(a[i-1])
if k<loop1+1:
  print(alist[k-1])
else:
  print(alist[((k-loop1-1)%(cnt-loop1))+loop1])

Verdicts: Runtime Error

Accepted
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):
import sys
input = lambda : sys.stdin.readline().strip()
n,k = map(int,input().split())
k = k%n
a = list(map(int,input().split()))
cur = 0
while k:
    k-=1
    cur = a[cur]-1
print(cur+1)

import sys
input = lambda : sys.stdin.readline().strip()
n,k = map(int,input().split())
a = list(map(int,input().split()))
sala = set([0,a[0]-1])
f = 0
s = a[0]-1
power = 1
length = 1
while f!=s:
    if length==power:
        power*=2
        length=0
        f = s 
    s = a[s]-1
    sala.add(s)
    length+=1
start = len(sala)-length
if k<=start:
    cur = 0 
    while k:
        k-=1
        cur = a[cur]-1
    print(cur+1)
else:
    k-=start 
    k%=length
    f = 0
    s = 0 
    while length:
        s = a[s]-1
        length-=1
    while s!=f:
        s = a[s]-1
        f = a[f]-1
    cur = f 
    while k:
        cur = a[cur]-1
        k-=1
    print(cur+1)

Verdicts: Wrong Answer

Accepted
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):
def main():
    
    N,K = map(int, input().split())
    A = list(map(int, input().split()))

    
    
    
    now = 0
    history = [0]
    i = 0
    while i<K:
        
        print(i)
        now = A[now]-1
        
        if now not in history:
            history.append(now)
            i+=1
        
        else:
            
            skip = len(history)-history.index(now)-1
            now = history[history.index(now)+skip]
            i += skip

    print(now+1)

main()


import sys
def main():
    
	input = sys.stdin.readline
    N,K = map(int, input.split())
    input = sys.stdin.readline
    A = list(map(int, input.split()))

    
    
    
    now = 0
    history = [0]
    i = 0
    while i<K:
        
        now = A[now]-1
        
        if now not in history:
            history.append(now)
            i+=1
        
        else:
            start = history.index(now)
            r = start + (K-1-i)%(len(history)-start)
            now = history[r]
            break

    print(now+1)

main()


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

    
    
    
    now = 0
    history = [0]
    isVisited = [False for i in range(N)]
    i = 0
    while i<K:
        
        now = A[now]-1
        
        #if now not in history:
        if isVisited[now] == False:
            history.append(now)
            isVisited[now] = True
            i+=1
        
        else:
            start = history.index(now)
            r = start + (K-1-i)%(len(history)-start)
            now = history[r]
            break

    print(now+1)

main()


Verdicts: Wrong Answer

Runtime Error

Accepted
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 = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]

dest = []

current = 0
next_dest = A[current]
count = 0
seen = set()

while not next_dest in seen:
    dest.append(next_dest)
    current = next_dest-1
    next_dest = A[current]
    seen.add(current)

dest.append(next_dest)
modulus = len(dest)

print(dest[(K - 1) % modulus])


N, K = [int(n) for n in input().split()]

A = [1] + [int(n) for n in input().split()]

dest = []

current = 0
count = 0
next_dest = A[current]
seen = {}

while not next_dest in seen:
    current = next_dest
    next_dest = A[current]
    dest.append(current)
    seen[current] = count
    count += 1

cycle_start = seen[next_dest]
cycle_length = seen[current] - cycle_start + 1

if K <= cycle_start:
    print(dest[K])
else:
    print(dest[cycle_start + ((K - cycle_start) % cycle_length)])

Verdicts: Wrong Answer

Accepted
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):
import sys

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [0] + A
town = 1
visited = [0]*(N+1)

for i in range(1, K+1):
    town = A[town]
    if visited[town] == 0:
        visited[town] = i
    else:
        loop = i - visited[town]
        town = (K - visited[town]) % loop + visited[town]
print(town)

import sys

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [0] + A
town = 1
visited = [0]*(N+1)

for i in range(1, K+1):
    town = A[town]
    if visited[town] == 0:
        visited[town] = i
    else:
        loop = i - visited[town]
        town = (K - visited[town]) % loop + visited[town]
        break
print(town)

import sys

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [0] + A
town = 1
visited = [0]*(N+1)

for i in range(1, K+1):
    town = A[town]
    if visited[town] == 0:
        visited[town] = i
    else:
        loop = i - visited[town]
        place = (K - visited[town]) % loop + visited[town]
        print(visited.index(place))
        sys.exit()
print(town)

Verdicts: Runtime Error

Wrong Answer

Accepted
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()))
l=[]*n
for i in range(n):
    b=[0,0]
    b[0]+=i+1
    b[1]+=a[i]
    l.append(b)
c=0
ne=0
l1=[0]*n
while True:
    if c==k:
        print(l[ne][1])
        exit()
    ne+=-ne+l[ne][1]-1
    c+=1
    if l1[ne]==0:l1[ne]+=1
    else:
        roop_act+=ne
        break

roop_c=0
while True:
    if c+roop_c==k:
        print(l[ne][1])
        exit()
    if l1[ne]==1:l1[ne]+=1
    else:break
    ne+=-ne+l[ne][1]-1
    roop_c+=1

re=(k-c)%roop_c
if re==0:print(l[ne][0])
else:
    for i in range(re):
        if i==re-1:print(l[ne][1])
        ne+=-ne+l[ne][1]-1

n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]*n
for i in range(n):
    b=[0,0]
    b[0]+=i+1
    b[1]+=a[i]
    l.append(b)
c=0
ne=0
l1=[0]*n
while True:
    if c==k:
        print(l[ne][1])
        exit()
    ne+=-ne+l[ne][1]-1
    c+=1
    if l1[ne]==0:l1[ne]+=1
    else:
        break

roop_c=0
while True:
    if c+roop_c==k:
        print(l[ne][1])
        exit()
    if l1[ne]==1:l1[ne]+=1
    else:break
    ne+=-ne+l[ne][1]-1
    roop_c+=1

re=(k-c)%roop_c
if re==0:print(l[ne][0])
else:
    for i in range(re):
        if i==re-1:print(l[ne][1])
        ne+=-ne+l[ne][1]-1

n,k=map(int,input().split())
a=list(map(int,input().split()))
l1=[0]*n
l1[0]+=1
c=0 
now=0
ne=a[now]-1 
while True:
    if c==k:
        print(now+1)
        exit()
    if l1[ne]==0:
        l1[ne]+=1
        now=ne
        ne=a[now]-1
        c+=1
    else:
        now=ne
        ne=a[now]-1
        c+=1
        break

roop_c=0
while True:
    if c+roop_c==k:
        print(now+1)
        exit()
    if l1[ne]==1:
        l1[ne]+=1
        now=ne
        ne=a[now]-1
        roop_c+=1
    else:break

re=(k-c)%roop_c
if re==0:print(now+1)
else:
    for i in range(re):
        if i==re-1:print(ne+1)
        ne+=-ne+a[ne]-1

Verdicts: Runtime Error

Wrong Answer

Accepted
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()))

l = [1]
ind = 0
val = 0
for i in range(n):
    val = a[ind]
    l.append(val)
    ind = val - 1

print("l:", l)
cnt = 1
repeat =[]
repeat.append(l.pop())
z = 0

for j in range(n - 1, -1, -1):
    z = l.pop()
    if z == repeat[0]:
        print("yes")
        break
    else:
        cnt += 1
        repeat.append(z)

repeat = repeat[::-1]

jouyo = (k- n - 1) % cnt

print(repeat[jouyo])


n, k = map(int, input().split())
a = list(map(int, input().split()))

l = [1]
ind = 0
val = 0
for i in range(n):
    val = a[ind]
    l.append(val)
    ind = val - 1

if n >=k:
    answer1 = l[k]

cnt = 1
repeat =[]
repeat.append(l.pop())
z = 0

for j in range(n - 1, -1, -1):
    z = l.pop()
    if z == repeat[0]:
        break
    else:
        cnt += 1
        repeat.append(z)

repeat = repeat[::-1]

jouyo = (k - n - 1) % cnt

if n >= k:
    print(answer1)
else:
    print(repeat[jouyo])


Verdicts: Wrong Answer

Accepted
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(lambda x: int(x) - 1, input().split()))
count_arr = [0] * n
count = 0

now = 0
loop_length = -1
while True:
    if count == k:
        print(a[now] + 1)
        exit()
    if count_arr[now] != 0:
        loop_length = count - count_arr[now] + 1
        break
    count += 1
    count_arr[now] = count
    now = a[now]


rest = (k - count) % loop_length


if rest == 0:
    print(now + 1)
    exit()

while rest > 0:
    print(f"now = {now}(next = {a[now]})")
    rest -= 1
    if rest == 0:
        break
    now = a[now]
print(a[now] + 1)
# 727202214173249352






n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
count_arr = [0] * n
count = 0

now = 0
loop_length = -1
while True:
    if count == k:
        print(now + 1)
        exit()
    if count_arr[now] != 0:
        loop_length = count - count_arr[now] + 1
        break
    count += 1
    count_arr[now] = count
    now = a[now]


rest = (k - count) % loop_length


if rest == 0:
    print(now + 1)
    exit()

#
while rest > 0:
    now = a[now]
    rest -= 1
print(a[now] + 1)



n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
count_arr = [0] * n
count = 0

now = 0
loop_length = -1
while True:
    if count == k:
        print(now + 1)
        exit()
    if count_arr[now] != 0:
        loop_length = count - count_arr[now] + 1
        break
    count += 1
    count_arr[now] = count
    now = a[now]


rest = (k - count) % loop_length


if rest == 0:
    print(now + 1)
    exit()

while rest > 0:
    rest -= 1
    if rest == 0:
        break
    now = a[now]
print(a[now] + 1)
# 727202214173249352

Verdicts: Wrong Answer

Wrong Answer

Accepted
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()))

ans=0
now=1
i=1
result=[1]
while i<=k:
    now=a[now]
    if now in result:
        x=result.index(now)
        length=i-1-x
        ans=result[(k-x)%length+x]
    result.append(now)
    i+=1
    
if ans!=0:
    print(ans)
else:
    print(result[-1])

n,k=map(int,input().split())
a=list(map(int,input().split()))

ans=0
now=1
i=1
result=[1]
while i<=k:
    now=a[now-1]
    if now in result:
        x=result.index(now)
        length=i-1-x
        ans=result[(k-x)%length+x]
    result.append(now)
    i+=1
    
if ans!=0:
    print(ans)
else:
    print(result[-1])

n,k=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))

ans=-1
l=[-1]*n
now=0
i=1
result=[0]
while i<=k:
    now=a[now]
    if l[now]!=-1:
        
        length=i-l[now]
        d=(k-i)%length+l[now]
        ans=l.index(d)
        break
    l[now]=i
    i+=1
    
if ans!=-1:
    print(ans+1)
else:
    print(now+1)

Verdicts: Runtime Error

Runtime Error

Accepted
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):
def main():
    
    IN = [int(_) for _ in input().split()]
    A = [int(_) for _ in input().split()]
    N = IN[0]
    K = IN[1]

    loop = []
    target = 1
    mark = [False] * N
    print(mark)
    mark[0] = True
    while True:
        target = A[target -1]
        loop.append(target)
        if mark[target-1]:
            break
        mark[target-1] = True 

    looper = loop
    si = len(loop)-1
    if loop[si] != 1:
        K = K - loop[si] -1
        looper = loop[loop[si]-1:si]
        
    ind = (len(looper) % K) - 1
    print(A[ind])
        



if __name__ == "__main__":
    main()


def main():
    
    IN = [int(_) for _ in input().split()]
    A = [int(_) for _ in input().split()]
    N = IN[0]
    K = IN[1]

    
    l = []
    l.append(1)
    
    nextInd = 1
    
    mark = [False] * N
    mark[0] = True
    
    startLoopCount = 0
    
    while True:
        nextInd = A[nextInd-1]
        if mark[nextInd-1]:
            startLoopCount = l.index(nextInd)
            break
        l.append(nextInd)
        mark[nextInd-1] = True
    
    move = l[0:startLoopCount]
    loop = l[startLoopCount:len(l)]

    # print(startLoopCount)
    # print(move)
    # print(loop)
    
    if K < startLoopCount:
        print(move[K])
        return
    index = (K - startLoopCount) % len(loop)
    print(loop[index])
    return

if __name__ == "__main__":
    main()


Verdicts: Wrong Answer

Accepted
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=[int(i) for i in input().split()]

went={i:0 for i in range(1,n+1)}

now=1
cnt=1
for i in range(n+1):
    if went[now]>0:
        t=cnt
        s=went[now]
        break
    else:
        went[now]=cnt
        now=A[now-1]
        cnt+=1

val=((k-t)%(t-s+1)-1)%(t-s+1)+s
for ele in went:
    if went[ele]==val:
        print(ele)
        break

n,k=map(int,input().split())
A=[int(i) for i in input().split()]

went={i:0 for i in range(1,n+1)}

now=1
cnt=1
for i in range(n+1):
    if went[now]>0:
        t=cnt-1
        s=went[now]
        break
    else:
        went[now]=cnt
        now=A[now-1]
        cnt+=1


if k <= t-1:
    val=k+1
else:
    val=((k+1-t)%(t-s+1)-1)%(t-s+1)+s
for ele in went:
    if went[ele]==val:
        print(ele)
        break

Verdicts: Wrong Answer

Accepted
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=list(map(int,input().split()))
As=list(map(int,input().split()))
root=[1]
count=[0]*n
count[0]+=1

for i in range(k):
    if count[root[-1]-1]!=0:
        break
    else:
        root.append(As[root[-1]-1])
        count[As[root[-1]-1]-1]+=1
print(root[k%len(root)])

n,k=list(map(int,input().split()))
As=list(map(int,input().split()))
root=[1]
count=[0]*n
count[0]+=1

cnt=0
for _ in range(k):
    dis=As[root[-1]-1]-1
    if count[dis]!=0:
        cnt+=1
        break
    else:
        root.append(dis+1)
        count[dis]+=1
if cnt==1:
    first=root.index(As[root[-1]-1])
    print(root[(k-first)%(len(root)-first)+first])
else:
    print(root[-1])

Verdicts: Wrong Answer

Accepted
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=list(map(int,input().split()))
a=[0]+ list(map(int,input().split()))

visited=[1]

for i in range(n):
  next=a[visited[-1]]
  visited.append(next)
  
loop_end=n
loop_start=n-1
while visited[loop_start]!=visited[loop_end]:
  loop_start-=1
period=loop_end-loop_start
#print(visited) 
#print("p:",period)
#print("k%period:",k%period)

if k<n+1:
  print(visited[k])
  exit()
else:
  print(visited[n-(k-n)%period+1])

n,k=list(map(int,input().split()))
a=[0]+ list(map(int,input().split()))

visited=[1]

for i in range(n):
  next=a[visited[-1]]
  visited.append(next)
  
loop_end=n
loop_start=n-1
while visited[loop_start]!=visited[loop_end]:
  loop_start-=1
period=loop_end-loop_start
#print(visited) 
#print("p:",period)
#print("(k-n)%period:",(k-n)%period)

if k<n+1:
  print(visited[k])
  exit()
else:
  print(visited[n-period+(k-n)%period])

Verdicts: Runtime Error

Accepted
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()))
B = []
i = 0
B.append(1)
for n in range(N):
  B.append(A[i])
  i = A[i]-1
if K>N:
  start = 0
  num = 0
  for j in range(len(B)):
    if B[len(B)-1] == B[j]:
      start = j
      break
  for j in range(1,len(B)-start):
    if B[start+j] == B[start]:
      num = j
      break
  print(B[((K-start)%num)+start],((K-start)%num)+start)
else:
  print(B[K])

N, K = map(int, input().split())
A = list(map(int, input().split()))
B = []
i = 0
B.append(1)
for n in range(N):
  B.append(A[i])
  i = A[i]-1
if K>N:
  start = 0
  num = 0
  for j in range(len(B)):
    if B[len(B)-1] == B[j]:
      start = j
      break
  for j in range(1,len(B)-start):
    if B[start+j] == B[start]:
      num = j
      break
  print(B[((K-start)%num)+start])
else:
  print(B[K])

Verdicts: Wrong Answer

Accepted