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: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    went = [0] * N
    went[0] = 1
    Flag = True
    check = [0]
    cnt = 1
    now = 0
    while Flag and (cnt <= K + 1):
        now = A[now] - 1
        if went[now] == 1:
            ind = check.index(now) - 1
            cnt = cnt - ind - 1
            Flag = False
        else:
            check.append(now)
            went[now] = 1
            cnt += 1
            continue

    if len(check) > K:
        print(check[K] + 1)
    else:
        K = (K - ind) % cnt
        print(check[ind + K] + 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d={}
cnt=[]
m=0
ans=0
for i in range(n):
  if a[m] in d:
    ans+=1
    cnt.append(a[m])
    if ans==k:
      print(a[m])
    break
  else:
    d[a[m]]=0
    cnt.append(a[m])
    ans+=1
    if ans==k:
      print(a[m])
      break
    m=a[m]-1
if ans!=k:
  print(cnt[(k-ans)%len(cnt)])

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
A = list(map(int, input().split()))
Q = deque()
Q.append(0)

Seen = [-1] * N
Seen[0] = 0
loop = 0
point = 0
while True:
    now = Q.popleft()
    nex = A[now]
    nex -= 1
    if Seen[nex] == -1:
        Seen[nex] = Seen[now] + 1
        Q.append(nex)
    elif Seen[nex] != -1:
        first = Seen[nex]
        loop = Seen[now] - Seen[nex] + 1
        point = nex
        break

p = (K - first) % loop
Q.clear()
Q.append(point)
ans = point
while p > 0:
    no = Q.popleft()
    ans = A[no]
    ans -= 1
    Q.append(ans)
    p -= 1
print(ans + 1)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,m=list(map(int,input().split()))
a=np.array(list(map(int,input().split())))
b=np.zeros(n)

mod = 10**6 + 7


tmp = 0
for i in range(mod):
  if b[tmp] == 2:
    break
  b[tmp] += 1
  tmp = a[tmp] -1
  
s = len(a[b==1])
t = len(a[b==2])

m = (m-s)%t + s
tmp = 0
for i in range(m):
  tmp = a[tmp] -1
  
print(tmp+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
n, k = map(int, input().split())
a = list(map(int, input().split()))

length = [-1] * n
path = [-1, -1]
route = []


def func(now, depth):
    global path, length, a, route
    route.append(now)
    if length[now] == -1:
        length[now] = depth
        func(a[now]-1, depth+1)
    else:
        path = [length[now], depth - length[now]]


if (k == 1):
    print(a[0])
else:
    func(0, 0)

    print(route[path[0]+(k-path[0]) % path[1]]+1)

Submission status: Wrong Answer

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

TeList = list(map(int, input().split()))

loop = [0]

Tmp = 0
TmpList = [0]

ResultList = []
while 'True':
    Tmp = TeList[Tmp]  - 1
    if Tmp in TmpList:
        ResultList = TmpList[TmpList.index(Tmp):]
        break
    else:
        TmpList.append(Tmp)

FirstStep = len(TmpList) - len(ResultList)

if K < len(ResultList):
    print(TmpList[K] + 1)
else:
    print(ResultList[(K - FirstStep) % len(ResultList)] + 1)

Submission status: Wrong Answer

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

history = []
visited = set()
cur = 0
for i in range(n):
    if cur in visited:
        x = history.index(cur)
        y = len(history) - x
        print(history[x+(k-x)%y] + 1)
        exit()

    visited.add(cur)
    history.append(cur)
    cur = a[cur]

print(history[k-1]+1)

Submission status: Wrong Answer

Solution code:
import sys

input = lambda: sys.stdin.readline()


def cin_int_list():
    return [int(x) for x in input().split()]


def cin_int_iter():
    return (int(x) for x in input().split())


def cin_int():
    return int(input())


def cout_int_iter(a):
    print(' '.join(map(str, a)))


def iota(n, start=0):
    return list(range(start, n))


def cin_digits_list():
    return [ord(x) - ord('0') for x in input()]


def main():
    n, k = cin_int_iter()

    g = {}

    cycle_starts = None
    for u, x in enumerate(cin_int_list()):
        v = x - 1
        if v in g:
            cycle_starts = v

        g[u] = v

    if cycle_starts:
        cycle_len = 0

        u = cycle_starts
        cycle = []
        while g[u] != cycle_starts:
            cycle.append(u)
            u = g[u]
            cycle_len += 1

        before_cycle = []
        u = 0

        while g[u] != cycle_starts:
            before_cycle.append(u)
            u = g[u]

        if k < len(before_cycle):
            print(before_cycle[k] + 1)
            return

        k -= len(before_cycle)

        print(cycle[k % cycle_len] + 1)
        return

    u = 0
    while k:
        u = g[u]
        k -= 1

    print(u + 1)

main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    N, K = readnums()
    A = list(readnums())
    l = []
    for i in range(N):
        if not i:
            l.append(A[i])
        else:
            a = A[l[i - 1] - 1]
            if a in l:
                d = l.index(a)
                loop = l[d:]
                K -= d
                print(loop[K % len(loop) - 1])
                break
            else:
                l.append(a)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

now = 1
a = [0]+a
count = 0
went = [1]
for i in range(n):
    now = a[now]
    count += 1
    went.append(now)
    if now == 1:
        break

now = 1
ans = k % count
print(went[ans])

Submission status: Wrong Answer

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]

n, k = map(int, input().split())
A = list(map(int, input().split()))
num = 1
root = []

while True:
    root.append(num)
    if root.count(num) == 2:
        break

    num = A[num - 1]

len = len(root)
first = root.index(num) + 1
syuuki = len - first

k = k - (len - 1)
k = k % syuuki
ans = root[root.index(num) + k]

#print(len)
#print(first)
#print(syuuki)
#print(k)
print(ans)
#print(A)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
arr = list(map(int, input().split()))
check = [0]*N
index = 0
cnt = 0
while True:
    if check[index] == 1:
        break
    check[index] = 1
    index = arr[index]-1
    cnt += 1

index2 = 0
first_cnt = 0
while True:
    if index2 == index:
        break
    index2 = arr[index2]-1
    first_cnt += 1
if first_cnt == 0:
    ans_cnt = K % cnt
    index = 0
    cnt = 0
    while True:
        if cnt == ans_cnt:
            break
        index = arr[index]-1
        cnt += 1
    print(index+1)
else:
    ans_cnt = (K-cnt) % first_cnt

    index = 0
    cnt = 0
    while True:
        if cnt == first_cnt:
            break
        index = arr[index]-1
        cnt += 1
    print(index+1)

Submission status: Wrong Answer

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

a = [i - 1 for i in a]
l = []
used = [0 for _ in range(n)]
used[0] = 0

c = 0
while 1:
    l.append(c)
    if used[c] == 1 and l:
        break
    used[c] = 1
    c = a[c]


end = l[-1]
p = 0
for i in range(len(l)):
    if l[i] == end:
        l = l[i:-1]
        p = i
        break

b = (k-p) % len(l)
print(l[b]+1)

Submission status: Wrong Answer

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

order = []
now = 1
count = 0
d = defaultdict(int)


for i in range(n):
    if d[now]!=0:
        s=d[now]-1
        g=i
        break
    order.append(now)
    d[now] = i+1
    now = a[now-1]
    
k-=s
loop = order[s:g]
print(loop[k%len(loop)])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=tuple(map(int,input().split()))
state=[1]
for _ in range(2*n+1):
    state.append(A[state[-1]-1])
count={}
for i in range(2*n+1):
    if state[i] in count:
        if i<2*n and state[i+1]in count:
            start=count[state[i+1]]
            end=i+2
        else:
            start=count[state[i]]
            end=i+1
    else:
        count[state[i]]=i+1
k -= start
k%=(end-start)
print(state[k+start])

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] -= 1
li = [False] * N
f = True
now = 0
l = []
l2 = [0] * N
i = 0
while f:
    l.append(now)
    li[now] = True
    l2[now] = i
    if li[A[now]]:
        f = False
        num = len(l) - l2[A[now]]
        n = l2[A[now]]
    i += 1
    now = A[now]
k = (K-n) % num
print(l[n+k]+1)

Submission status: Wrong Answer

Solution code:

n,m=list(map(int,input().split(" ")))

arr=list(map(int,input().split(" ")))
d={}
for i in range(n):
	d[i]=[]
for i in range(n):
	d[i].append(arr[i]-1)

from collections import deque
def bfs(start,n,visited,m):
	# count=0

	# l2=[i+1]
	q=[start]
	q=deque(q)
	# count+=1
	moves=[]
	moves.append(start)
	visited[start]=1
	left=-1
	fl=0
	while (len(q)!=0 and fl==0):
		y=d[q[0]]
		for j in y:
			if(visited[j]==0):
				moves.append(j)
				visited[j]=1
				# l2.append(j)
				q.append(j)

				# count+=1
			else:
				left=j
				fl=1
				break



		q.popleft()

	if(left!=-1):
		pre=[]
		post=[]
		for i in range(len(moves)):
			if(moves[i]==left):
				pre=moves[:i]
				post=moves[i:]
				break
		if(m<=len(pre)):
			print(pre[m-1]+1)
		else:
			m-=len(pre)
			print(post[m%len(post)]+1)


	else:
		print(1)
visited=[0]*n
bfs(0,n,visited,m)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [int(x)-1 for x in input().split()]

visited = [0] * n
visit_list = [0]
visit = 0
while True:
    visit_list.append(a[visit])
    visit = a[visit]
    if visited[visit] == 1:
        break
    visited[a[visit]] = 1

print(visit_list[k%len(visit_list)]+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=tuple(map(int,input().split()))
state=[1]
for _ in range(2*n+1):
    state.append(A[state[-1]-1])
count={}
for i in range(2*n+1):
    if state[i] in count:
        if i<n and state[i+1]in count:
            start=count[state[i+1]]
            end=i+2
        else:
            start=count[state[i]]
            end=i+1
    else:
        count[state[i]]=i+1
k -= start
k%=(end-start)
print(state[k+start])

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import sys
sys.setrecursionlimit(4100000)

n,m= map(int,readline().split())
a = list(map(int,readline().split()))

if a[0] == 1:
  print(1)
  exit()
  
cnt = n
for i,ai in enumerate(a):
  if i+1 == ai:
    cnt -= 1
    
k = m % cnt
if k == 0:
  print(1)
  exit()
  
def dfs(now,cc):
    if cc == k:
      return now
    else:
      next = a[now]
      next -= 1
      return dfs(next,cc+1)
            
print(dfs(0,0)+1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    a=list(map(lambda x:int(x)-1,readline().split()))

    flags=[0]*n

    now=0
    cnt=1
    while True:
        flags[now]=cnt
        cnt+=1
        nex=a[now]
        if flags[nex]!=0:
            last=now
            break
        else:
            now=nex

    f_cnt=cnt-1
    cnt=0
    while True:
        nex=a[now]
        if nex==last:
            cnt+=1
            break
        else:
            now=nex
            cnt+=1

    k=k-(f_cnt-cnt)
    k%=cnt
    now=a[last]
    for _ in range(k):
        nex=a[now]
        now=nex

    print(now+1)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
A = list(map(int,input().split()))
A = [a-1 for a in A]

path = []
passed = set([])
p = 0
while 1:
  if k ==0:
    print(p)
    exit()
  if p not in passed:
    path.append(p)
    passed.add(p)
    p = A[p]
    k -= 1
  else:
    break
path = path[path.index(p):]
term = len(path)
k %= term
print(path[k]+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

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

r = []
u = [False for _ in range(n)]
def dfs(x):
  if u[x]:
    return x
  else:
    u[x] = True
    r.append(x)
    return dfs(a[x]-1)

b = dfs(0)
for i in range(len(r)):
  if b == r[i]:
    break
#print(b,i,r)
print(r[i+(k-i)%(len(r)-i)]+1)

Submission status: Wrong Answer

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]

n, k = map(int, input().split())
A = list(map(int, input().split()))
num = 1
root = []

while True:
    root.append(num)
    if root.count(num) == 2:
        break

    num = A[num - 1]

len = len(root)
first = root.index(num) + 1
syuuki = len - first

k = k - (len - 1)
k = k % syuuki
ans = root[root.index(num) + k] 

print(len)
print(first)
print(syuuki)
print(k)
print(ans)
#print(A)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d={}
cnt=[]
m=0
ans=0
for i in range(n):
  if a[m] in d:
    ans+=1
    cnt.append(a[m])
    if ans==k:
      print(a[m])
    break
  else:
    d[a[m]]=0
    cnt.append(a[m])
    ans+=1
    if ans==k:
      print(a[m])
      break
    m=a[m]-1
if ans!=k:
  print(cnt[k%len(cnt)])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
test_pos = 1
sub = 0
loop = 0
city = [0 for i in range(n)]

while True:
    test_pos = a[test_pos-1]
    city[test_pos-1] += 1
    sub += 1
    if city[test_pos-1] == 2:
        loop_start = test_pos
        break

while True:
    test_pos = a[test_pos-1]
    loop += 1
    if test_pos == loop_start:
        break

k -= sub
k %= loop
pos = loop_start

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

print(pos)

Submission status: Wrong Answer

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

import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000000)
from collections import deque


seen = [0 for i in range(n)]

hist = deque([])  # 頂点の訪問履歴
pos = -1  # サイクルの1成分 -1はサイクル未発見


def dfs(x, p):  # pはxの親
    global pos  # global!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    seen[x] = 1
    hist.append(x)  # 訪れたらhistにappend
    if seen[A[x]-1] == 1:
        pos = A[x]-1
        return  # サイクルを発見したら、即すべてのdfsを終える
    # 残りはseen[x]=0のみ
    dfs(A[x]-1, x)
    if pos != -1: return  # 帰りがけ、サイクルが検出済みなら何もしない

    hist.pop()  # stackの末尾を消す
    # ループなく平和に終わったらpopできる
    return  # まあなくてもいいけどreturnは終了であることを強調


dfs(0, -1)

# この状態だと、サイクルに入る前に通った枝葉がhistに入ってる。
# 枝葉が終わったら、posからループの順番に頂点番号が入ってる。
cycle = []
while hist:
    qwe = hist.pop()
    cycle.append(qwe)
    if qwe == pos: break

roop = len(cycle)
sen = seen.count(1)

if k<=n:
    no = 0
    for i in range(k):
        no = A[no]
    print(no)
else:
    tmp = k-sen+roop
    tmp%=roop
    print(cycle[-tmp-1]+1)

Submission status: Wrong Answer

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

lc = [1]

i = 1
while lc.count(lc[i-1]) == 1:
    if i == N:
        break
    else:
        lc.append(la[lc[i-1]-1])
        i += 1


lpst = lc.index(lc[-1])
lpnum = len(lc) - lpst - 1
loclst = (K - lpst) % lpnum + lpst

print(lc[loclst])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i)-1 for i in input().split()]
counter = 1
cur = 0
mem = [0]
while True:
   if counter == k:
      ans = cur +1
      break
   cur = a[cur]
   mem.append(cur)
   counter += 1
   if cur == 0:
      k = k%counter
      ans = mem[k]
      break
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
m = 1
for i in range(k%n):
  m = a[m-1]
print(m)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i)-1 for i in input().split()]
cnt = 0
loc = 0
lst = [False]*n
lst[0] = True
while True:
    if lst[a[loc]]:
        x = a[loc]
        break
    loc = a[loc]
    lst[loc] = True
cnt = 0
loc = 0
while True:
    if x == loc: break
    loc = a[loc]
    cnt += 1
loc = x
cnt2 = 0
while True:
    loc = a[loc]
    cnt2 += 1
    if loc == x: break
k -= cnt
k %= cnt2
loc = x
while k > 0:
    loc = a[loc]
    k -= 1
print(loc+1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [1]
s = set()
s.add(1)
now = 1
for _ in range(N):
    if A[now - 1] in s:
        K %= len(L) - L.index(A[now - 1])
        break
    L.append(A[now - 1])
    s.add(A[now - 1])
    now = A[now - 1]
#print(L)
#print(K)
print(L[K])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
flag = True
went = {1}
go = [1]
i = 0
while flag:
  if a[i] not in went:
    go.append(a[i])
    went.add(a[i])
    i = a[i]-1
  else:
    flag = False
loop = go[go.index(a[i]):]
k -= len(go)-len(loop)
ans = k % len(loop)
print(loop[ans])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def binarySearch (arr, l, r, x): 
  
    # Check base case 
    if r >= l: 
  
        mid = l + (r - l) // 2
  
        # If element is present at the middle itself 
        if arr[mid] == x: 
            return mid 
          
        # If element is smaller than mid, then it  
        # can only be present in left subarray 
        elif arr[mid] > x: 
            return binarySearch(arr, l, mid-1, x) 
  
        # Else the element can only be present  
        # in right subarray 
        else: 
            return binarySearch(arr, mid + 1, r, x) 
  
    else: 
        # Element is not present in the array 
        return -1
n,k=map(int,input().split( ))
s=input().split( )
p=[]
g=0
while binarySearch(p,0,len(p)-1,int(g))==-1:
    p.append(int(g))
    g=int(s[int(g)])-1
x=p.index(g)
y=len(p)
u=p[x:y]
k-=x
v=(k+1)%(y-x)
print(u[v-1]+1)

Submission status: Wrong Answer

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

kyori = [-1]*n

now = 0;kyori[now] = 0
jyunkan = 0;v = 0
for i in range(n):
    tugi = a[now] - 1
    if kyori[tugi] != -1:
        jyunkan = kyori[now] + 1
        v = tugi
        break
    kyori[tugi] = kyori[now] + 1
    if kyori[tugi] == k:
        print(tugi + 1)
        sys.exit()
    now = tugi
print(kyori)
k -= kyori[v]
mawaru = jyunkan - kyori[v]
amari = k % mawaru

now = v;cunt = 0
for i in range(amari):
    now = a[now] - 1
    
print(now + 1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
trans_list = list(map(int,input().split()))
cycle = []
cycle.append(1)
temp = trans_list[0]
for i in range(n):
    cycle.append(temp)
    if temp in cycle[0:-2]:
        break
    else:
        temp = trans_list[temp-1]
for i in range(n):
    if cycle[i]==temp:
        first = i
        break;

length = len(cycle)
cycle_length = length-1-first
index = first+(k-first)%cycle_length
print(cycle[index])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
now=0
ima=[]
while True:
    now=a[now-1]
    ima.append(now)
    if now in set(ima[:-1]):
        break
print(ima[k % (len(ima[:-1]))])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ar=list(map(int,input().split()))
vis=[0 for i in range(n+1)]
x=1
seq=[1]
go=0
rep=0
ct=1
while (vis[x]==0):
    vis[x]=ct
    seq.append(ar[x-1])
    x=ar[x-1]
    ct+=1
    if vis[x]!=0:
        rep=ct-vis[x]
        go=vis[x]
if k<=go:
    print(seq[k-1])
else:
    print(seq[go+(k-go)%rep])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 何回で１周するのか
N, K = lr()
A = [0] + lr()
if A[1] == 1:
    print(1); exit()
lap = 1
cur = A[1]
nex = [1, A[1]]
been = set([A[1]])
while cur != 1:
    cur = A[cur]
    lap += 1
    nex.append(cur)
    if cur in been:
        lap2 = 1
        cur2 = A[cur]
        while cur2 != cur:
            cur2 = A[cur2]
            lap2 += 1
        K -= lap
        K %= lap2
        for _ in range(K):
            cur2 = A[cur2]
        print(cur2); exit()
    been.add(cur)

# lap回で１周
K %= lap
answer = nex[K]
print(answer)

Submission status: Wrong Answer

Solution code:
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n,k=n1()
a=[0]
a.extend(n1())

from collections import deque
p=deque([1])
nex=1
for i in range(n+1):
    last=nex
    nex=a[last]
    p.append(nex)
    if nex==1:
        break
p.pop()
length=len(p)
print(p[k%length])

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline

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

graph = []

time = [-1] * N

for i in range(N):
    graph.append([i, A[i]])

now = 0
nex = 0
time[0] = 0
loop = 0

while True:
    now = nex
    nex = A[now] - 1

    if time[nex] == -1:
        time[nex] = time[now] + 1
    else:
        loop = nex
        break

K %= (max(time) - time[loop] + 1)

for t in time:
    if t == K:
        print(A[t])

Submission status: Wrong Answer

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

s = set()
s.add(1)

while(True):
	j = a[j-1]
	if(j in s):
		c+=1
		break
	else:
		s.add(j)
		c+=1

# while((c == 1 and j == 1) or (c != 1 and j != 1)):
# 	j = a[j-1];
# 	c+=1
# c+=1
# j = a[j-1]
# # print(j)
# # print(c)
r = k%c

while(r != 0):
	j = a[j-1]
	r-=1
print(a[j-1])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
cnt = 1
next = a[0]
tmp_list = [a[0]]
roop_list = []
while(True):
    next = a[next-1]
    if next in tmp_list:
        hajimari = tmp_list.index(next)
        roop_list = tmp_list[hajimari:]
        cnt -= hajimari
        break
    tmp_list.append(next)
    cnt +=1
print(roop_list[((k-hajimari)%cnt)-1])

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=tuple(map(int,input().split()))
state=[1]
for _ in range(n+1):
    state.append(A[state[-1]-1])
count={}
for i in range(n+1):
    if state[i] in count:
        if i<n and state[i+1]in count:
            start=count[state[i+1]]
            end=i+2
        else:
            start=count[state[i]]
            end=i+1
    else:
        count[state[i]]=i+1
k -= start
k%=(end-start)
print(state[k+start])

Submission status: Wrong Answer

Solution code:
n,k=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
a.insert(0,-1)

freq=[]
for i in range(n+1):
	freq.append(0)

pos=1
pos=a[pos]
cycle=[]
while freq[pos]==0:
	freq[pos]+=1
	cycle.append(pos)
	pos=a[pos]

flag=0
c1=[]
c2=[]
for i in range(len(cycle)):
	if cycle[i]==pos:
		flag=1
	if flag==0:
		c1.append(cycle[i])
	else:
		c2.append(cycle[i])

l1=len(c1)
l2=len(c2)

if k<=l1:
	print(c1[l1-1])
else:
	k-=l1
	print(c2[k%l2-1])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]

path = [1]
visited = set([1])
now = 1
t = 0

while True:
    now = a[now - 1]
    t += 1
    if now in visited:
        ti = path.index(now)
        tc = t - ti
        cycle = path[ti:]
        break
    else:
        path.append(now)
        visited.add(now)


answer = cycle[(k - t) % tc]
print(answer)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = []
    next = a[0]
    b.append(next)
    for i in range(n-1):
        next = a[next-1]
        b.append(next)
    ans = k%n-1
    print(b[ans])
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

tmp = 1
length = deque()
reached = [-1] * N
while reached[tmp-1] == -1:
    length.append(tmp)
    reached[tmp-1] = 0
    tmp = A[tmp-1]

start, end = length.index(tmp), len(length)
T = end - start

if K <= end:
    print(length[K-1])
else:
    _mod = (K-start) % T
    print(length[start+_mod])

Submission status: Wrong Answer

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

p = 0

if K > N:
    city = [-1]*N
    city[0] = 1
    while city[A[p]-1] == -1:
        city[A[p]-1] = city[p]+1
        p = A[p]-1
    K = (K-city[p]) % (city[p]-city[A[p]-1]+1)

for i in range(K+1):
    p = A[p]-1

print(p+1)

Submission status: Wrong Answer

Solution code:
from copy import copy, deepcopy
from collections import Counter
from math import sqrt, floor, factorial
from itertools import permutations, combinations, combinations_with_replacement
from operator import mul
from functools import reduce
import bisect

MOD = 10**9 + 7
INF = float('inf')

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

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

A = [A[i]-1 for i in range(len(A))]

mem = [0]*N
mem2 = [0]*N

idx = 0
count1 = 0
while True:
    mem[idx] += 1
    if mem[idx] == 2:
        break
    count1 += 1
    idx = A[idx]


idx2 = idx
count2 = 0

count2 += 1
idx2 = A[idx2]

while True:
    if idx2 == idx:
        break
    count2 += 1
    idx2 = A[idx2]

tmp = count1 - count2

e = (K - tmp) % count2

for _ in range(e):
    idx = A[idx]

print(idx+1)

# idx is start

Submission status: Wrong Answer

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

seen = [0]
next = 0

for _ in range(K):
    next = A[next]-1
    if next in seen:
        break
    seen.append(next)

# loop なしの場合
if A[next]-1 not in seen:
    print(A[next])
    exit()

# それ以外
next_ind = seen.index(next)
loop = seen[next_ind:]

mod = (K - next_ind) % len(loop)
ans = loop[mod]+1

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = [int(s) - 1 for s in input().split()]

    pos = 0
    seen = {pos}
    visited = [-1] * N
    visited[0] = 0
    cycle = None
    loop_start = None
    for i in range(1, N + 1):
        npos = A[pos]
        if i == K:
            print(npos + 1)
        if npos in seen:
            loop_start = visited[npos]
            cycle = i - visited[npos]
            break

        seen.add((npos))
        visited[npos] = i
        pos = npos

    L = loop_start + (K - loop_start) % cycle
    pos = 0
    for i in range(L):
        npos = A[pos]
        pos = npos

    print(npos + 1)

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

count = 0
i = 1
position = 1
position_list = [1]
start_loop = 0
while i > 0:
  position = A[position-1]
  count += 1
  if position in position_list:
    start_loop = position_list.index(position)
    position_list.append(position)
    break
  else:
    position_list.append(position)
  	
k = (K - start_loop) % (count - start_loop)
print(position_list[start_loop+k])

Submission status: Wrong Answer

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    n,k=map(int, readline().rstrip().split())
    aa=list(map(int, readline().rstrip().split()))
    p=0

    if k<n:
        for i in range(k):
            p=(aa[p]-1)
        print(p+1)
        return
    for i in range(n):
        p=(aa[p]-1)
    re=k%n
    for i in range(re):
        p=(aa[p]-1)
    print(p+1)



    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Wrong Answer

Solution code:
import sys
from _collections import deque
N,K = map(int,input().split())
A = list(map(int,input().split()))
for i in range(len(A)):
    A[i] = A[i]-1
used = [0]*N
m = 0
c = 0
used[m] = 1

while(True):
    m = A[m]
    c += 1
    if c == K:
        print(m)
        sys.exit()
    if used[m] == 1:
        break
    else:
        used[m] = 1

loop = m
t = 0
c = 0
while(True):
    t = A[t]
    c += 1
    if t == loop:
        break

h = K-c
ans = deque([loop])
c = 0
p = loop
while(True):
    p = A[p]
    c += 1
    if p == loop:
        break
    ans.append(p)

print(ans[h%c]+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] -= 1
L = [0]
t = 0
F = [-1 for _ in range(N)]
F[0] = 0
c = 1
while F[A[t]] == -1:
    t = A[t]
    F[t] = c
    c += 1
    L.append(t)
loop = c - F[A[t]]
buffer = c - loop
NL = L[buffer:]
amari = (K - buffer) % loop
print(NL[amari] + 1)

Submission status: Wrong Answer

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

takahashi = 1
roop = [1]

for _ in range(N):
    if A[takahashi - 1] == 1:
        break
    else:
        takahashi = A[takahashi - 1]
        roop.append(takahashi)
        
print(roop[K % len(roop)])

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] = A[i] -1
G = 0
lis = [0]*N
lis[0] = 1
roop = []
cnt = 0
while True:
    G = A[G]
    cnt +=1
    lis[G] += 1
    if lis[G] == 2:
        roop.append(G)
    elif lis[G] == 3:
        break

roop_cn = len(roop)
diff = cnt - roop_cn*2
K_n = K - diff
amari = K_n % roop_cn
print(roop[amari]+1)