Problem:


Score: 400 points

Problem Statement

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.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2



Sample input:
4 5
3 2 4 1


Sample output:
4

Submission status: Runtime Error

Solution code:
import collections 
n,k = map(int,input().split())
A = list(map(int,input().split()))
B=[1]
m=0
for i in range(n):
    B.append(A[m])
    m=A[m]-1

D=collections.Counter(A)
for i in range(n):
    if D[i+1]==1:
        del D[i+1]

if D!={}:
    for i in range(len(B)):
        if B[i] in D.keys():
            M=B[i]
            j=i+1
            break
else:
    M=1
    j=1
l=j
while B[l]!=M:
    l+=1

T=l-j+1
print(B[k-((k-j)//T)*T])

Submission status: Runtime Error

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

d = {}
for i in range(1, n + 1):
    d[i] = a[i - 1]

did = set(1)
route = [1]

now = 1
for i in range(k):
    now = d[now]
    route.append(now)
    if now in did:
        break
    did.add(now)

if k < 10 ** 6:#計算量的に間に合うので愚直に
    now = 1
    for i in range(k):
        now = d[now]

    print(now)

else:
    for i in range(len(route)):
        if route[i] == route[-1]:
            roop = route[i:-1]
            print(roop[(k - i) % len(roop)])
            exit()

Submission status: Runtime Error

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

d = {}
for i in range(1, n + 1):
    d[i] = a[i - 1]

did = set()
route = [1]

now = 1
for i in range(k):
    now = d[now]
    route.append(now)
    if now in did:
        break
    did.add(now)


for i in range(len(route)):
  if route[i] == route[-1]:#routeには第二ループの始めがギリ入っている
    roop = route[i:-1]
    print(roop[(k - i) % len(roop)])#k-iは残り移動階数
    exit()

Submission status: Runtime Error

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

d = {}
for i in range(1, n + 1):
    d[i] = a[i - 1]

did = set()
route = [1]

now = 1
for i in range(k):
    now = d[now]
    route.append(now)
    if now in did:
        break
    did.add(now)

for i in range(len(route)):
    if route[i] == route[-1]:
        roop = route[i:-1]
        break

print(roop[(k - i) % len(roop)])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
li = list(map(int,input().split()))
townlist = []
rooptown = 0
nowtown = 1
for i in range (0,200000):
    nowtown.append(nowtown)
    nowtown = li[nowtown]
    for j in list:
        if (nowtown == j and rooptown == 0):
            rooptown == i
K = K % rooptown + rooptown
for i in range (0,K):
    nowtown.append(nowtown)
    nowtown = li[nowtown]
print(nowtown)

Submission status: Runtime Error

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

d = [-1] * (n+1)
ls = 0
le = 0

def dfs(i, t):
  global d, ls, le
  if d[i] != -1:
    ls = i
    return False
  d[i] = t
  ret = dfs(a[i-1], t+1)
  if ret == False:
    le = i

dfs(1, 0)

#print(d)
#print(ls, le)

ans = 0
if k <= d[le] or ls == 0:
  ans = d.index(k)
else:
  ld = d[ls]
  t = (k-ld) % (d[le] - d[ls] + 1)
  t += d[ls]
  ans = d.index(t)

print(ans)

Submission status: Runtime Error

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

d = [-1] * (n+1)
ls = 0
le = 0

def dfs(i, t):
  global d, ls, le
  if d[i] != -1:
    ls = i
    return False
  d[i] = t
  ret = dfs(a[i-1], t+1)
  if ret == False:
    le = i

dfs(1, 0)

#print(d)
#print(ls, le)

ans = 0

ld = d[ls]
t = (k-ld) % (d[le] - d[ls] + 1)
t += d[ls]
ans = d.index(t)

print(ans)

Submission status: Runtime Error

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

d = [-1] * (n+1)
ls = 0
le = 0

def dfs(i, t):
  global d, ls, le
  if d[i] != -1:
    ls = i
    return False
  d[i] = t
  ret = dfs(a[i-1], t+1)
  if ret == False:
    le = i

dfs(1, 0)

#print(d)
#print(ls, le)
ld = d[ls]

ans = 0
if k <= d[le]:
  ans = d.index(k)
else:
  t = (k-ld) % (d[le] - d[ls] + 1)
  t += d[ls]
  ans = d.index(t)

print(ans)

Submission status: Runtime Error

Solution code:

def main():
  n,k = map(int,input().split())
  a = list(map(int,input().split()))
  l = [1]

  for i in range(1,k+1):
    ind = l[-1]
    if a[ind-1] in set(l):
      l.append(a[ind-1])
      roop = l[-1]
      nokori = k-i
      break
    
    else:
      l.append(a[ind-1])
    
  l.pop()
  rooplist = l[l.index(roop):]
  rooplen = len(rooplist)
  mod = nokori%rooplen

  print(rooplist[mod])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = [1]

for i in range(1,k+1):
  ind = l[-1]
  if a[ind-1] in set(l):
    l.append(a[ind-1])
    roop = l[-1]
    nokori = k-i
    break
    
  else:
    l.append(a[ind-1])
    
l.pop()
rooplist = l[l.index(roop):]
rooplen = len(rooplist)
mod = nokori%rooplen

print(rooplist[mod])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [True]*N
C = [1]
now = 0
while B[now]:
    B[now] = False
    now = A[now] - 1
    C.append(now+1)
start = C.index(C[-1])
D = C[start:-1]
L = len(D)
if K < start:
    print(C[k])
else:
    K -= start
    K %= L
    print(D[K])

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
if A[0] == 1:
    print(1)
    exit()
B = [A[0]]
C = []
num = A[0]
Flag = 1
for i in range(N):
    num = A[num-1]
    if num == A[0]:
        Flag = 0
        break
    B.append(num)
if Flag == 1:
    B.reverse()
    C.append(B[0])
    for b in B[1:]:
        if B[0] == b:
            break
        C.append(b)
        l = len(C)
    print(C[K%l-1])
else:
    l = len(B)
    print(B[K%l-1])

Submission status: Runtime Error

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
s="1"
if K> N
	K = K % N
d = 0
for i in range(K):
	c =a[c-1]

print(c)

Submission status: Runtime Error

Solution code:
N,K = list(map(int,input().split()))
a = list(map(int,input().split()))
c = 1
s="1"
mod = 0
d = 0
for i in range(K):
	c =a[c-1]
	if str(c) in s:
		b=s.index(str(c))
		mod = i + 1 -s.index(str(c))
		d = 1
		break
	s += str(c)
if d == 1:
	x= K % mod
	c = 1
	for i in range(b):
		c=a[c-1]
	for i in range(x-s.index(str(b))):
		c =a[c-1]
print(c)

Submission status: Runtime Error

Solution code:
n,k  = map(int,input().split())
a = list(map(int,input().split()))
a.insert(0,0)

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)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
temps=[1]
i=0
while i<k:
  temp=a[temp-1]
  result.append(temp)
  temps=list(set(result))
  if len(temps)!=len(result):
    break
  i=i+1
l=len(result)
temp=result.index(result[l-1])
if k<l:
  print(result[k+1])
else:
  l2=l-1-temp
  print(result[temp+(k-temp)%l2])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
L = list(map(int,input().split()))

li = [1]
for i in range(10**5*2+1):
    li.append(L[li[i]-1])

last = li[-1]
first = li[-2]

p = 10**5*2
cnt = 0
while last!= first:
    first = li[p]
    cnt +=1
    p -=1
#print(first,last,cnt)
idx = 0
for i in range(200001):
    if li[i] == li[i+cnt]:
        idx = i
        break

p = (k-idx)%cnt
print(li[idx+p])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
L = list(map(int,input().split()))

li = [1]
for i in range(10**5*2+1):
    li.append(L[li[i]-1])

last = li[-1]
first = li[-2]

p = 10**5*2
cnt = 0
while last!= first:
    first = li[p]
    cnt +=1
    p -=1
#print(first,last,cnt)
idx = 0
for i in range(1000):
    if li[i] == li[i+cnt]:
        idx = i
        break

p = (k-idx)%cnt
print(li[idx+p])

Submission status: Runtime Error

Solution code:
,k = map(int,input().split())
L = list(map(int,input().split()))

li = [1]
for i in range(10**5*2+1):
    li.append(L[li[i]-1])

last = li[-1]
first = li[-2]

p = 10**5*2
cnt = 0
while last!= first:
    first = li[p]
    cnt +=1
    p -=1
#print(first,last,cnt)
idx = 0
for i in range(1000):
    if li[i] == li[i+cnt]:
        idx = i
        break

p = (k-idx)%cnt
print(li[idx+p])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
visited = [-1] * N
visited[0] = 0
next = A[0]
for i in range(1, K+1):
    present = next
    visited[present - 1] = i

    next = A[present - 1]
    if visited[next - 1] != -1:
        loopLength = visited[present - 1] - visited[next - 1] + 1
        cnt = i
        rest = (K - cnt) % loopLength
        present = visited.index(visited[next - 1] + rest - 1)
        break

print(present)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1

while t[s - 1] == -1:
  if count == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]
else:
  z = (k - t[s - 1]) % (count - t[s - 1])
  s = t.index(t[s - 1] + z + 1)
print(s)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
q = list(map(int, input().split()))
v = [-1]*(n+1)
p = []
a = 1
c = 0
while 1:
    if v[a] != -1:
        l = c-v[a]
        if k < c:
            print(p[k])
        else:
            print(p[(k-v[a])%l+v[a]])
        break
    v[a] = c
    c += 1
    p.append(a)
    a = q[a]

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

num = list(map(int, input().split()))
t = [-1] * n
count = 0
s = 1

while t[s - 1] == -1:
  if count == k:
    break

  t[s - 1] = count
  count += 1
  s = num[s - 1]
else:
  z = (k - t[s - 1]) % (count - t[s - 1])
  print(t.index(t[s - 1] + z + 1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [-1]*(N+1)
path = []
at = 1
count = 0
while True:
    if visited[at] != -1:
        loop = count-visited[at]
        if K < count: # (K-visited[at])//loopが1以上であることを保証
            print(path[K]+1) # 条件 K < visited[at]なら、(K-visited[at])//loopが0以上であることを保証。どっちでもよさそう。
        else:
            print(path[(K-visited[at])%loop+visited[at]]+1)
        break
    visited[at] = count
    count += 1
    path.append(at)
    at = A[at]

Submission status: Runtime Error

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

route ={1}
cur = 1
for i in range(k):
    goto = a[cur - 1]
    x = len(route)
    route.add(goto)
    y = len(route)
    if x == y:
        route = list(route)
        pre_goto = route.index(goto)
        dif = len(route) - pre_goto
        rest_step = (k - pre_goto) % dif
        print(route[pre_goto + rest_step])
        break
    cur = goto
else:
    print(route[k])

Submission status: Runtime Error

Solution code:

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

town_loop = [1]

for i in range(k):
    next_town = alist[town_loop[i]-1]

    try:
        loop_start = town_loop.index(next_town)
        break
    except ValueError:
        town_loop.append(next_town)

if len(town_loop) == k-1:
    print(town_loop[k-1])
else:
    o = len(town_loop) - loop_start
    print(town_loop[((k-loop_start) % o) + loop_start])

Submission status: Runtime Error

Solution code:

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

town_loop = [1]

for i in range(k):
    next_town = alist[town_loop[i]-1]

    try:
        loop_start = town_loop.index(next_town)
        break
    except ValueError:
        town_loop.append(next_town)
        
if len(town_loop) == k-1:
    print(town_loop[k-1])
else:
    o = len(town_loop) - loop_start
    print(town_loop[((k+1) % o) + loop_start])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A = list(map(int,input().split()))
B = [0]*N
loop=0
now=0
C=[]
n=0
while(True):
    if B[now]==0:
        B[now]=1
        C.append(now+1)
        loop+=1
        now=A[now]-1
    else:
        n=A[now]
        break
if loop<K:
    res=(K-C.index(n)+1)%(loop-C.index(n)+1)
    D=C[C.index(n)-1:]
    print(D[res])
else:
    print(C[K])

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A = list(map(int,input().split()))
B = [-1]*N
loop=0
now=0
C=[]
n=0
while(True):
    if B[now]==-1:
        B[now]=1
        C.append(now+1)
        loop+=1
        now=A[now]-1
    else:
        n=A[now]
        break
if loop<=K:
    res=(K-C.index(n)+1)%(loop-C.index(n)+1)
    D=C[C.index(n)-1:]
    print(D[res])
else:
    print(C[K])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

a = [int(x)-1 for x in input().split()]

v = []
vc = [0]*n
b = 0
c = 0

while True:
    if vc[b]>0:
        stop = v.index(b)
        break
    v.append(b)
    vc[b] += 1
    b = a[b]
    c += 1



if k>c:
    r = (k-stop)%(c-stop)
    print(v[stop+r]+1)
else:
    print(v[k]+1)

Submission status: Runtime Error

Solution code:

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

a = [int(x)-1 for x in input().split()]

v = []
b = 0
stop = -1

for i in range(n):
    if b not in v:
        v.append(b)
        b = a[b]
    else:
        stop = v.index(b)
        break

v1 = v[:stop]
v2 = v[stop:]
c1 = len(v1)
c2 = len(v2)

if k>c1:
    r = (k-c1)%c2
else:
    r = k
    
print(v[stop+r]+1)

Submission status: Runtime Error

Solution code:
def get_keys(d, val):
    return [k for k, v in d.items() if v == val]
    
def main():
    read = lambda: sys.stdin.readline().rstrip()
    
    N, K  = map(int,read().split())
    A = list(map(int,read().split()))
    
    g = collections.defaultdict(int)
    for i, val in enumerate(A):
        g[i + 1] = val
        
    v = collections.defaultdict(int)
    cnt = 0
    cur = 1
    p = 0
    mp = 0
    while True:
        if v[cur] > 0:
            p = cnt - v[cur]
            mp = v[cur]
            break
        v[cur] = cnt
        cnt += 1
        cur = g[cur]
    
    print(get_keys(v, (K-mp)%p + mp)[0] if K >= mp else get_keys(v, K)[0])


 
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [1]
c = a[1]
d = 0
e=0
length = 0
first = 0
ans = 0


while c not in b:
    b += [c]
    c = a[c]
    d = c
    e+=1

if d != 0:
    length = e - b.index(d)
    first = b.index(d)
if k <= first:
    ans = b[k]
elif d == 0:
    ans = 1
else:
    ans = b[(k - first) % length + first]
print(ans)

Submission status: Runtime Error

Solution code:
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])

Submission status: Runtime Error

Solution code:
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]]
  if next in visited:
    loop_end=n+1
    loop_start=visited.index(n)
    period=loop_end-loop_start
  visited.append(next)
#print(visited) 
#print(period)
if k<n+1:
  print(visited[k])
  exit()
else:
  print(visited[k%period])

Submission status: Runtime Error

Solution code:
import sys

def Destination(N, K, DestinationList, TravelHistory = [1] ):
    """
    for Num in range(N):
        Destination = DestinationList[ TravelHistory[-1]-1]
        TravelHistory.append(Destination)
        if(Destination in TravelHistory[:-1]):
            Pos = TravelHistory[:-1].index(Destination)
            Period = len(TravelHistory)-Pos-1
            break;
    """
    Pos_First = 0
    Pos_Second = 0
    Min = min(N, K)
    for Num in range(Min):
        Destination = DestinationList[ TravelHistory[-1]-1]

        if(Destination in TravelHistory):
            Pos_First = TravelHistory.index(Destination)
            Pos_Second = len(TravelHistory)
            break

        TravelHistory.append(Destination)
    """
    for Num in range(N):
        if(TravelHistory[Num] in TravelHistory[Num+1:]):
            Pos_First = Num
            Pos_Second = TravelHistory[Num+1:].index( TravelHistory[Num] ) + Num+1

            break
    """
    Period = Pos_Second-Pos_First
    #print(Pos_First, Pos_Second, Period)



    Times_K = (K-Pos_First)%Period
    Pos_K = TravelHistory[Pos_First+Times_K]
    return Pos_K


if(__name__ == '__main__'):
    In = input().split()

    N= int(In[0].strip())
    K= int(In[1].strip())

    DestinationList = [0]*N
    In = input().split()
    for Num in range(len(In)):
        DestinationList[Num]= int(In[Num])  # [M] -> value
    print(Destination(N, K, DestinationList) )

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()

#【ループ回数】200000
for i in range(1,len(A)):
    print("Baah")
if k <= len(B):
    print("Baah")
else:
    k = k- len(B)
    #x = k % len(route)
    if x ==0:
        print("Baah")
    else:
        print("Baah")

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()

#【ループ回数】200000
for i in range(1,len(A)):
    now_city=A[now_city]
    #【ループ回数】B 内で now_city があるかを探索
    #if now_city in B:
        #route=B[C[now_city]-1:i-1]
    #    break
    B.append(now_city)
    C[now_city]=i
if k <= len(B):
    print(B[k-1])
else:
    k = k- len(B)
    #x = k % len(route)
    if x ==0:
        #print(route[-1])
    else:
        #print(route[x-1])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=[0]+A
B=[]
C=[0]+[-1]*n
now_city=1
seen=set()

#【ループ回数】200000
for i in range(1,len(A)):
    now_city=A[now_city]
    #【ループ回数】B 内で now_city があるかを探索
    #if now_city in B:
    #    route=B[C[now_city]-1:i-1]
    #    break
    B.append(now_city)
    C[now_city]=i
if k <= len(B):
    print(B[k-1])
else:
    k = k- len(B)
    x = k % len(route)
    if x ==0:
        print(route[-1])
    else:
        print(route[x-1])

Submission status: Runtime Error

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

mp = dict()
for ix in range(N):
    mp[ix+1] = A[ix]

hist = set()
cur = 1
cnt = 0

while True:
    cur = mp[cur]
    cnt += 1

    if cur in hist:
        break
    else:
        hist.add(cur)

cycle_sp = cur
cycle_l = 0

while True:
    cur = mp[cur]
    cycle_l += 1

    if cur == cycle_sp:
        cycle_l -= 1
        break

rest = (K-cnt) % cycle_l

while rest > 0:
    cur = mp[cur]
    rest -= 1

print(cur)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
now=1
count=0
a=0
l=[1]
b=0
c=0
q=[True]*N
while a==0and count<K:
    now=A[now-1]
    count+=1
    if q[now-1]:
        a=1
    else:
        l.append(now)
        q[now-1]=False
        
b=l.index(now)
c=count-b
if count==K:
    print(now)
else:
    n= ((K-b)%c)
    print(l[b+n])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [0] + A

L = [[] for _ in range(N)]
L[0] = 1
prev = 1
count = 1
for i in range(1, N):
    now = A[prev]
    if now == 1:
        break
    elif now == prev:
        flag_inf = True
        ans_inf = now
    else:
        L[i] = now
        prev = now
        count += 1
#        print(L)
L = L[:count]
print(L)

if flag_inf:
    print(ans_inf)
else:
    print(L[K%count])

Submission status: Runtime Error

Solution code:
import numpy as np
N, K = (int(i) for i in input().split())  
A = [int(i) - 1 for i in input().split()] 
N = len(A)

def get_keys_from_value(d, val):
    return [k for k, v in d.items() if v == val][0]

now = 0
lis = []
p = {}
for i in range(N):
  now = A[now]
  if now in p:
    break
  
  p[now] = i
  lis.append(now)

if K < i:
  print(lis[i] + 1)
else:
  print(lis[(K-i-1) % (i - p[now]) + p[now]] + 1)

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=map(int,input().split())
l=list(map(int,input().split()))
kiseki=deque()
tori=[-1]*n
#町1からスタート
u=1
kiseki.append(1)
for i in range(len(l)):
  u=l[u-1]
  if kiseki.count(u)==0:
    kiseki.append(u)
  else:
    kiseki.append(u)
    break
kuri=[]
while True:
  if u == kiseki[0]:
    kiseki.pop()
    break
  kuri.append(kiseki.popleft())

if k<=len(kuri):
  print(kuri[k])
else:
  K=k-len(kuri)
  amari=K%len(kiseki)
  print(kiseki[amari])

Submission status: Runtime Error

Solution code:
def main():
    nums = list(map(int, input().split()))
    n, k = nums[0], nums[1]
    graph = {}
    out = list(map(int, input().split()))
    for i in range(n):
        graph[i + 1] = out[i]
    
    cycle_length = None
    visited = {}
    ans = None

    def go(x, rem):
        while rem:
            x = graph[x]
            rem -= 1
        
        nonlocal ans
        ans = x
        return x

    def dfs(i, rem):
        #print("visit", i)
        nonlocal ans
        if rem == 0:
            ans = i
            return
        if i in visited:
            nonlocal cycle_length
            cycle_length = visited[i] - rem
            steps = rem % cycle_length
            ans = go(i, steps)
            return 
        visited[i] = rem
        dfs(graph[i], rem - 1)
        return

    dfs(1, k)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A_i = list(map(int, input().split()))

place = 1
road = [0 for i in range(N)]
cycle = 0

road[0] = 1

while K != 0:
    place = A_i[place - 1]
    K -= 1

    if road[place - 1] == 1:
        loop_place = place
        break
    road[place - 1] = 1

while K != 0:
    loop_place = A_i[loop_place - 1]
    cycle += 1

    if place == loop_place:
        break

K = K % cycle

while K != 0:
    place = A_i[place - 1]
    K -= 1

print(place)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A_i = list(map(int, input().split()))

place = 1
road = [0 for i in range(N)]
cycle = 0

road[0] = 1

while K != 0:
    place = A_i[place - 1]
    K -= 1

    if road[place - 1] == 1:
        loop_place = place
        break
    road[place - 1] = 1

while 1:
    loop_place = A_i[loop_place - 1]
    cycle += 1

    if place == loop_place:
        break

K = K % cycle

while K != 0:
    place = A_i[place - 1]
    K -= 1

print(place)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A_i = list(map(int, input().split()))

place = 1
road = []
cycle = 0

while K != 0:
    road.append(place)
    place = A_i[place - 1]
    K -= 1

    if place in road:
        loop_place = place
        break

while 1:
    loop_place = A_i[loop_place - 1]
    cycle += 1

    if place == loop_place:
        break

K = K % cycle

while K != 0:
    place = A_i[place - 1]
    K -= 1

print(place)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A_i = list(map(int, input().split()))

place = 1
road = []
cycle = 0
begin = 0

while K != 0:
    road.append(place)
    place = A_i[place - 1]
    K -= 1

    if place in road:
        cycle = len(road) - road.index(place)
        begin = road.index(place)
        break

K = begin + (K - begin) % cycle

while K !=0:
    place = A_i[place - 1]
    K -= 1

print(place)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
iran=[]
l=list(map(int,input().split()))
#n,k=6,727202214173249351
#l=[6,5,2,5,3,2]
for i in range(N):
  if l.count(i+1)==0:
    iran.append(i+1)
kiseki=[]
#町1からスタート
u=1
kiseki.append(1)
for i in l:
  u=l[u-1]
  if kiseki.count(u)==0:
    kiseki.append(u)
  else:
    kiseki.append(u)
    break
kuri=kiseki[kiseki.index(u):len(kiseki)-1]
if k<=len(kiseki)-(len(kuri)+1):
  print(kiseki[k])
else:
  K=k-(len(kiseki)-(len(kuri)+1))
  amari=K%len(kuri)
  print(kuri[amari])

Submission status: Runtime Error

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

TownlistA = [1]
TownlistB = []

j = 1
loopB = 0
for i in range(N + 1):
    t = A[j - 1]
    if (t not in TownlistA) and (loopB == 0):
        TownlistA.append(t)

    else:
        loopB = 1
        if t not in TownlistB:
            TownlistB.append(t)
        else:
            break
    j = t
    if i > K:
        break

if len(TownlistB) == 0:
    print(TownlistA[N])
else:
    print(TownlistB[(K - len(TownlistA)) % len(TownlistB)])

Submission status: Runtime Error

Solution code:
N, K = (int(x) for x in input().split())
trip = input().split()

now = 1
way = [now]

for k in range(K):
  now = int(trip[now-1])
  way.append(now)
  if way.count(now) == 2:
    index = way.index(now)
    if index != 0:
      del way[:index]
      a = (K-index) % (len(way)-1)
      break
print(way[a])

Submission status: Runtime Error

Solution code:
N, K = (int(x) for x in input().split())
trip = input().split()
 
now = 1
way = []
 
for k in range(K): 
  now = int(trip[now-1])
  way.append(now)
  if way.count(now) == 2:
    a = K % len(way)
    break

print(way[a])

Submission status: Runtime Error

Solution code:
, K = map(int, input().split())
A = list(map(int, input().split()))

TownlistA = [1]
TownlistB = []

j = 1
for i in range(N + 1):
    t = A[j-1]
    if t not in TownlistA:
        TownlistA.append(t)
        j = t
    else:
        if t not in TownlistB:
            TownlistB.append(t)
            j = t

print(TownlistB[(K - len(TownlistA)) % len(TownlistB)])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
graph = [set() for i in range(N)]
for i, a in enumerate(A):
    graph[i].add(a - 1)

stack = [0]
seen = {0}
path = []
dist = [-1] * N
while stack:
    cur = stack.pop()
    if dist[cur] != -1:
        break
    dist[cur] = len(path)
    path.append(cur)
    for nei in graph[cur]:
        if nei not in seen:
            stack.append(nei)
            seen.add(nei)

not_loop = graph[cur].pop()
loop = len(path) - not_loop
if K < not_loop:
    print(path[K] + 1)
else:
    K -= not_loop
    K %= loop
    print(path[not_loop + K - 1] + 1)

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
ls_pass = list(map(int,input().split()))
ls_pass.insert(0,0)
a=1
b=0
ls_past=[1]
for i in range(1,N+1):
  b=a
  a=ls_pass[a]
  if b==a:
    print(a)
    break
  elif a in ls_past:
    c=ls_past.index(a)
    ls_cicle=ls_past[c:i]
    print(ls_cicle[(K-i)%(i-c)])
    break
  elif i=K:
    print(a)
    break
  else:
    ls_past.append(a)

Submission status: Runtime Error

Solution code:
#D
N,K = map(int,input().split())
A = list(map(int,input().split()))
genzai = 1
kiseki = []
for i in range(K):
    kiseki.append(genzai)
    genzai = A[genzai-1]
    if s in kiseki:
        s = kiseki.index(genzai)
        loop = len(kiseki)-s
        amari = (K-s)%loop
        genzai = kiseki[s+amari] 
        break
    else:
        pass
print(genzai)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
root = [1]
for i in range(1500):
    next_num = a[root[-1]]
    root.append(next_num)

if k < n+1:
    print(root[k])

else: 
    loop_end = n 
    loop_start = n-1
    while(root[loop_start]!=root[loop_end]):
        loop_start -= 1
    period = loop_end - loop_start
    k %= period
    while k < n:
        k += period
    print(root[k])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
root = [1]
for i in range(n+1000):
    next_num = a[root[-1]]
    root.append(next_num)

if k < n+1:
    print(root[k])

else: 
    loop_end = n 
    loop_start = n-1
    while(root[loop_start]!=root[loop_end]):
        loop_start -= 1
    period = loop_end - loop_start
    k %= period
    while k < n:
        k += period
    print(root[k])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
root = [1]
for i in range(1000):
    next_num = a[root[-1]]
    root.append(next_num)

if k < n+1:
    print(root[k])
    exit()

loop_end = n 
loop_start = n-1
while(root[loop_start]!=root[loop_end]):
    loop_start -= 1
period = loop_end - loop_start
k %= period
while k < n:
    k += period
print(root[k])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
telep = list(map(int, input().split()))

def memorize(f):
  table = {}
  def func(*args):
    if not args in table:
      table[args] = f(*args)
    return table[args]
  return func

@memorize
def tel(i): #i回テレポやったときの地点
  if i == 1:
    return telep[i-1]
  return telep[tel(i-1)-1]
  
# N回回したときにループする点を探す。
check = [1]
for i in range(1, n+1):
  if not tel(i) in check:
    check.append(tel(i))
  else: #i回目でかぶりが見つかった.roop = i -(最初のかぶりのリスト番号)
    s = check.index(tel(i))
    roop = i - s
    break
    
#テレポs回目からループ開始。roop回数でループする。(s=2, roop=2のとき、tel(2k+2)=tel(2))

def tel_n(i): #テレポi回目での地点。0から。
  if i == 0:
    return 1
  elif i <= s+roop-1:
    return tel(i)
  else:
    for r in range(s, s+roop):
      if (i-r)%roop == 0:
        return tel(r)
      
print(tel_n(k))

Submission status: Runtime Error

Solution code:
n, k = map(int, list(input().split()))
a = list(map(int, input().split()))
#テレポートの処理
y = 1
list_y =[1]
for i in range(k - 1):
    x = a[y - 1]
    y = a[x - 1]
    list_y.append(y)
#重複要素で脱出させる
    if len(list_y) != len(set(list_y)):
        break
#roop回でループする
roop = len(list_y) - list_y.index(list_y[len(list_y) - 1]) - 1
#k回処理すればいい
amari = len(list_y) - roop - 1
k = (k - amari) %roop + amari
#再び処理させる
y = 1
for i in range(k - 1):
    x = a[y - 1]
    y = a[x - 1]

print(y)

Submission status: Runtime Error

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

y = 1
list_y =[1]
for i in range(k - 1):
    x = a[y - 1]
    y = a[x - 1]
    list_y.append(y)

    if len(list_y) != len(set(list_y)):
        break    #重複要素で脱出

#roopを定義したい

roop = len(list_y) - list_y.index(list_y[len(list_y) - 1]) - 1

amari = len(list_y) - roop - 1
k = (k - amari) %roop + amari

y = 1
for i in range(k - 1):
    x = a[y - 1]
    y = a[x - 1]

print(y)