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

def make_root(A):
    ro = []
    ro.append(A[0])
    end=False
    for n in range(0,N):
        if A[ro[n]-1] in ro:
            ro.append(A[ro[n]-1])
            end=True
            break
        else:
            ro.append(A[ro[n]-1])
    return ro, end

# rの作成
r, end = make_root(A)
# repeatの開始インデックス
repeat_index = r.index(r[-1]) 
# repeatを構成する要素の数
repeat_n = len(r) - r.index(r[-1]) - 1


if end is True:
    if (repeat_index > K):
        print(r[K-1])
    else:
        print(r[repeat_index + (K-repeat_index) % repeat_n])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
f=lambda:map(int,input().split())
n,k=f()
a=list(f())
lv=[-1]*n
lv[0]=0
p=0
for i in range(n):
  t=a[p]-1
  if lv[t]>=0:
    l,r=lv[t],i+1
    break
  lv[t]=i+1
  p=t
print(lv,l,r)
d,m=divmod(k-l,r-l)
print(d,m)
print(lv.index(l+(k-l)%(r-l) if k>l else k)+1)

Submission status: Wrong Answer

Solution code:
f=lambda:map(int,input().split())
n,k=f()
a=list(f())
lv=[-1]*n
lv[0]=0
p=0
for i in range(n):
  t=a[p]-1
  if lv[t]>=0:
    l,r=lv[t],i+1
    break
  lv[t]=i+1
  p=t
print(lv.index(l+(k-l)%(r-l))+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = [0] + [int(i) for i in input().split(" ")]
b = [0,1] + [0]*(n-1)
c = "1"
nex = 1
for i in range(1,n+1):
    nex = a[nex]
    if b[nex] == 1:
        whe = c.index(str(nex))
        lo = len(c)-whe
        break
    b[nex] = 1
    c += str(nex)
if whe+1 >= k:
    print(c[k])
else:
    k_ = k - whe
    print(c[whe:][k_%lo])

Submission status: Wrong Answer

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:
            if i > K:
                break
    j = t


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

Submission status: Wrong Answer

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)
        j = t
    else:
        loopB = 1
        if t not in TownlistB:
            TownlistB.append(t)
            j = t
        else:
            break

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

Submission status: Wrong Answer

Solution code:
import sys

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

now = 0
vis = []
taken = [0]*(n+1)

while taken[now] == 0:
	vis.append(now)
	taken[now] = 1
	now = ara[now]-1

cycleLen = len(vis) - vis.index(now)

if k < vis.index(now):
	print(vis[k])
	sys.exit()

k -= vis.index(now)

k %= cycleLen

print(vis[k + vis.index(now) ] + 1)

Submission status: Wrong Answer

Solution code:
import sys

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

now = 0
vis = []
taken = [0]*n

while taken[now] == 0:
	vis.append(now)
	taken[now] = 1
	now = ara[now]-1

cycleLen = len(vis) - vis.index(now)

if k < vis.index(now):
	print(vis[k])
	sys.exit()

k -= vis.index(now)

k %= cycleLen

print(vis[k + vis.index(now) ] + 1)

Submission status: Wrong Answer

Solution code:
import sys

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

now = 0
vis = []

while now not in vis:
	vis.append(now)
	now = ara[now]-1

cycleLen = len(vis) - vis.index(now)

if k < vis.index(now):
	print(vis[k])
	sys.exit()

k -= vis.index(now)

k %= cycleLen
# print( k  , vis.index(now), k + vis.index(now) - 1)
print(vis[k + vis.index(now) ] + 1)

Submission status: Wrong Answer

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)
        j = t
    else:
        loopB = 1
        if t not in TownlistB:
            TownlistB.append(t)
            j = t

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

Submission status: Wrong Answer

Solution code:
N, 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: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = []
ne = a[0] - 1
for i in range(2 * n + 1000):
    ne = a[ne] - 1
    if ne in l:
        break
    l.append(ne)
print(l[(k + 1) % len(l)] + 1)

Submission status: Wrong Answer

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

L=[]
S=set([1])
L.append(1)
i=1
for _ in range(N+1):
   i=A[i-1]
   if i in S: break
   L.append(i)
   S.add(i)

l=len(L)
K+=1
print(L[-(K%l+1)])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

cur = 0     # 現在地
period = 0  # 繰り返し周期
rest = 0    # 繰り返し周期に入らない最初のテレポート回数
counter = defaultdict(int)   # 2回目を通るときの検出用
first_index = defaultdict(int)  # 1回目に通ったテレポート回数

counter[0] = 1
first_index[0] = 0
for i in range(1, N):
    cur = A[cur] - 1
    counter[cur] += 1
    if counter[cur] == 2:
        period = i - first_index[cur]
        rest = first_index[cur]
        break
    else:
        first_index[cur] = i
if K <= rest + period:
    for i in range(K):
        cur = A[cur] - 1
    print(cur+1)
else:
    for i in range((K - rest) % period):
        cur = A[cur] - 1
    print(cur+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

cur = 0     # 現在地
period = 0  # 繰り返し周期
rest = 0    # 繰り返し周期に入らない最初のテレポート回数
counter = defaultdict(int)   # 2回目を通るときの検出用
first_index = defaultdict(int)  # 1回目に通ったテレポート回数

counter[0] = 1
first_index[0] = 0
for i in range(1, N):
    cur = A[cur] - 1
    counter[cur] += 1
    if counter[cur] == 2:
        period = i - first_index[cur]
        rest = first_index[cur]
        break
    else:
        first_index[cur] = i
if K <= rest + period:
    for i in range(K):
        cur = A[cur] - 1
    print(cur)
else:
    for i in range((K - rest) % period):
        cur = A[cur] - 1
    print(cur+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

cur = 0     # 現在地
period = 0  # 繰り返し周期
rest = 0    # 繰り返し周期に入らない最初のテレポート回数
counter = defaultdict(int)   # 2回目を通るときの検出用
first_index = defaultdict(int)  # 1回目に通ったテレポート回数

counter[0] = 1
first_index[0] = 0
for i in range(1, N):
    cur = A[cur] - 1
    counter[cur] += 1
    if counter[cur] == 2:
        period = i - first_index[cur]
        rest = first_index[cur]
        break
    else:
        first_index[cur] = i

for i in range((K - rest) % period):
    cur = A[cur] - 1
print(cur+1)

Submission status: Wrong Answer

Solution code:

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

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

b = a[0]-1
c = [0]
c.append(b)
lc = 2

while lc < n+1:   
    b = a[b]-1
    if b in c:
        pivot = c.index(b)
        break
    else:
        c.append(b)
    lc += 1


d2 = lc-pivot
d1 = pivot
g = (k-d1)%d2
    
print(c[pivot+g]+1)

Submission status: Wrong Answer

Solution code:

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

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

b = a[0]-1
c = [0]
c.append(b)

while len(c) < n+1:   
    b = a[b]-1
    if b in c:
        pivot = c.index(b)
        break
    else:
        c.append(b)


d2 = len(c)-pivot
d1 = pivot
g = (k-d1)%d2
    
print(c[g+pivot]+1)

Submission status: Wrong Answer

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

cyc_len = 1

sp,fp = arr[0],arr[arr[0]]

while sp!=fp:
    sp = arr[sp]
    fp = arr[arr[fp]]
    cyc_len+=1

fp = 0
start_len = 0
while fp!= sp:
    fp = arr[fp]
    sp = arr[sp]
    start_len+=1

sp = 0

for _ in range(start_len):sp=arr[sp]

tot_cyc = (k-start_len)%cyc_len
# print(sp)
for _ in range(tot_cyc):
    sp = arr[sp]
    # print(sp)

# print(start_len,cyc_len)
print(sp+1)

Submission status: Wrong Answer

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

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

b = a[0]-1
c = [0]
c.append(b)

while len(c) < n+1:   
    b = a[b]-1
    if b in c:
        pivot = c.index(b)
        break
    else:
        c.append(b)

left = c[:pivot]
right = c[pivot:]
d1 = len(left)
d2 = len(right)

g = (k-d1)%d2
    
print(right[g]+1)

Submission status: Wrong Answer

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

cyc_len = 1

sp,fp = arr[0],arr[arr[0]]

while sp!=fp:
    sp = arr[sp]
    fp = arr[arr[fp]]
    cyc_len+=1

fp = 0
start_len = 0
while fp!= sp:
    fp = arr[fp]
    sp = arr[sp]
    start_len+=1

sp = 0

for _ in range(start_len):sp=arr[sp]

tot_cyc = k%cyc_len
for _ in range(tot_cyc-1):sp = arr[sp]

print(sp)

Submission status: Wrong Answer

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

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

b = a[0]-1
c = 1

while c < n:   
    b = a[b]-1
    c = c+1
    if b == 0:
        break
g = k%c

d = a[0]-1
i = 1
while i<g:
    d = a[d] - 1
    i = i+1
    
print(d+1)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
a=list(map(int, input().split()))
z=1
num=[1]
n=0
for x in range(k):
    z=a[z-1]
    n+=1
    if z in num:
        if z==1:
            num.append(1)
        break
    num.append(z)
num=num[1:]
ke=num.index(z)
if k<=ke:
    for x in range(k):
        z=a[z-1]
if k>ke:
    k-=ke
    k=k%(n-ke)
    k+=ke
    for x in range(k):
        z=a[z-1]
print(z)

Submission status: Wrong Answer

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,K):
  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
  else:
    ls_past.append(a)
else:
  print(ls_past)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def make_root(A):
    ro = []
    ro.append(A[0])
    end=False
    for n in range(0,N):
        if A[ro[n]-1] in ro:
            ro.append(A[ro[n]-1])
            end=True
            break
        else:
            ro.append(A[ro[n]-1])
    return ro, end

r, end = make_root(A)
repeat_index = r.index(r[-1]) 
repeat_n = len(r) - r.index(r[-1]) 
if end is True:
    print(r[repeat_index + K % repeat_n])
else:
    print(-1)

Submission status: Wrong Answer

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

def make_root(A):
    ro = []
    ro.append(A[0])
    end=False
    while (end==False):
        for n in range(0,N):
            if A[ro[n]-1] in ro:
                ro.append(A[ro[n]-1])
                end=True
                break
            else:
                ro.append(A[ro[n]-1])
    return ro

r = make_root(A)
repeat_index = r.index(r[-1]) 
repeat_n = len(r) - r.index(r[-1]) 
print(r[repeat_index + K % repeat_n])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
port = list(map(int, input().split()))
visited = [1]
current = 1

while port[current-1] not in visited:
    visited.append(port[current-1])
    current = port[current-1]

s = visited.index(port[current-1])
bubun = visited[s:]

g = (k-s) % len(bubun)
print(bubun[g])

Submission status: Wrong Answer

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

list = []
list.append(1)
last = 0
for n in range(N):
	inp = A[list[-1] - 1]
	if inp in list:
		last = inp
		break
	else:
		list.append(inp)

head = list.index(last)

remain = (K - head) % (len(list) - head)

print(list[head + remain])


##C
#N,M,X = map(int,input().split())
#C = []
#A = []

#for c in range(N):
#	p = list(map(int,input().split()))
#	C.append(p[0])
#	A.append(p[1:])

#minCost = sum(C)
#result = -1

#for i in range(2 ** N):
#	rikai = [0] * M
#	cost = 0
#	for n in range(N):
#		if i >> n & 1 == 0:
#			continue
#		cost+=C[n]		
#		for m in range(M):
#				rikai[m]+=A[n][m]
	
				
#	ok = True
#	if all(x >= X for x in rikai):
#		if result == -1:
#			result = cost
#		else:
#			result = min(result,cost)


#print(result)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
count = 1
machi = 0
machi_list = [0]
next_machi = x[machi] - 1
machi_dict = [0] * n
machi_dict[0] = 1
while True:
    if machi_dict[next_machi] == 1:
        break
    count += 1
    print(machi_dict)
    machi_dict[next_machi] = 1
    machi_list.append(next_machi)
    machi = next_machi
    next_machi = x[machi] - 1
z = 0
for i in range(0, count):
    if machi_list[i] == next_machi:
        z = i
        break
loop_machi_list = machi_list[z:]
if k < z:
    machi = machi_list[k] + 1
    print(machi)
else:
    k = k - z
    machi = loop_machi_list[k % len(loop_machi_list)] + 1
    print(machi)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = [int(i) for i in input().split()]

    db = []
    for _ in range(60):
        l = [0] * N
        db.append(l)
    
    for n in range(N):
        db[0][n] = A[n] - 1
    
    for k in range(1, 60):
        for n in range(N):
            db[k][n] = db[k - 1][db[k - 1][n]]
    
    a = []
    for i in range(60):
        if K>>i & 1:
            a.append(i)

    now = 0
    for i in reversed(a):
        now = db[i][now]
    print(now + 1)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    A = [int(i) for i in input().split()]

    db = []
    for _ in range(60):
        l = [0] * N
        db.append(l)
    
    for n in range(N):
        db[0][n] = A[n] - 1
    
    for k in range(1, 60):
        for n in range(N):
            db[k][n] = db[k - 1][db[k - 1][n]]
    
    a = []
    for i in range(60):
        if K>>i & 1:
            a.append(i)

    now = 0
    for i in a:
        now = db[i][now]
    print(now + 1)

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split());a=[0]+a;p=1
while k:p=k%2*p and a[p];a=[a[b]for b in a];k//=2
print(p)

Submission status: Wrong Answer

Solution code:
def main():
    N , K = list(map(int,input().split()))
    A = list(map(int,input().split()))
    A.insert(0,0)
    local = 1
    tmp = []
    tmp.append(local)
    for k in range(N):
        local = A[local]
        if local in tmp:
            result = len(tmp) - tmp.index(local)
            break
        else:
            tmp.append(local)
    
    for k in range((K-tmp.index(local)) % result):
        local = A[local]
    print(local)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N , K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.insert(0,0)
local = 1
tmp = []
tmp.append(local)
for k in range(N):
    local = A[local]
    if local in tmp:
        result = len(tmp) - tmp.index(local)
        break
    else:
        tmp.append(local)

for k in range((K-tmp.index(local)) % result):
    local = A[local]
print(local)

Submission status: Wrong Answer

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

N,K = map(int,readline().split())
A = [0] + list(map(int,readline().split()))

now = 1
while K > 0:
  if K & 1:
    now = A[now]
  A = [A[i] for i in range(len(A))]
  K >>= 1

print(now)

Submission status: Wrong Answer

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])

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

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

print(y)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [0] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0#ループの最初
for i in range(n):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    
    if t[now_city-1]==1:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  f_l=i
        #  a = 1
         b = 1
        #  s_l_city=now_city
        #  print("ループの最初の数字",now_city,"最初は"+str(f_l)+"回目","周期は",sycle)
         break
    t[now_city-1]+=1
    f[now_city-1]=i
# print(t)
# print(f_l)
# print(f_l,sycle,s_l_city)

if b==1:
    # now_city=s_l_city
    for _ in range((k-f_l)%sycle):
        
        now_city = s[now_city-1]
    # print((k-f_l)%sycle)
    print(now_city)
else:
    # print("ループなし")
    now_city=1
    for _ in range(k):
        now_city=s[now_city-1]
    print(now_city)

Submission status: Wrong Answer

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

y = 1
for i in range(k - 1):
    x = a[y - 1]
    y = a[x - 1]
    
    
    if i > 10 ** 18:
        break



print(y)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [0] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0#ループの最初
for i in range(n):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    
    if t[now_city-1]==1:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  f_l=i
        #  a = 1
         b = i
        #  s_l_city=now_city
        #  print("ループの最初の数字",now_city,"最初は"+str(f_l)+"回目","周期は",sycle)
         break
    t[now_city-1]+=1
    f[now_city-1]=i
# print(t)
# print(f_l)
# print(f_l,sycle,s_l_city)

if k>b:
    # now_city=s_l_city
    for _ in range((k-f_l)%sycle):
        
        now_city = s[now_city-1]
    print((k-f_l)%sycle)
    print(now_city)
else:
    # print("ループなし")
    now_city=1
    for _ in range(k):
        now_city=s[now_city-1]
    print(now_city)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [0] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0#ループの最初
for i in range(n):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    
    if t[now_city-1]==1:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  f_l=i
        #  a = 1
         b = i
        #  s_l_city=now_city
         print("ループの最初の数字",now_city,"最初は"+str(f_l)+"回目","周期は",sycle)
         break
    t[now_city-1]+=1
    f[now_city-1]=i
# print(t)
# print(f_l)
# print(f_l,sycle,s_l_city)

if k>b:
    # now_city=s_l_city
    for _ in range((k-f_l)%sycle):
        
        now_city = s[now_city-1]
    print((k-f_l)%sycle)
    print(now_city)
else:
    print("ループなし")
    now_city=1
    for _ in range(k):
        now_city=s[now_city-1]
    print(now_city)

Submission status: Wrong Answer

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

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

print(y)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [0] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0#ループの最初
for i in range(n):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    
    if t[now_city-1]==1:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  f_l=i
        #  a = 1
         b = 1
         s_l_city=now_city
         break
    t[now_city-1]+=1
    f[now_city-1]=i
# print(t)
# print(f_l)
# print(f_l,sycle,s_l_city)
if b==1:
    now_city=s_l_city
    for j in range((k-f_l)%sycle):
        now_city = s[now_city-1]
    print(now_city)
else:
    now_city=1
    for _ in range(k):
        now_city=s[now_city]
    print(now_city)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [1] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0#ループの最初
for i in range(n):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    
    if t[now_city-1]==1:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
        #  f_l=f[now_city-1]
         f_l=i
        #  a = 1
         b = 1
         s_l_city=now_city
         break
    t[now_city-1]+=1
    f[now_city-1]=i
# print(t)
# print(f_l)
# print(f_l,sycle,s_l_city)
if b==1:
    now_city=s_l_city
    for j in range((k-f_l)%sycle):
        now_city = s[now_city-1]
    print(now_city)
else:
    now_city=1
    for _ in range(k):
        now_city=s[now_city]
    print(now_city)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [1] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0#ループの最初
for i in range(n):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    
    if t[now_city-1]==1:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  a = 1
         b = 1
         s_l_city=now_city
         break
    t[now_city-1]+=1
    f[now_city-1]=i
# print(t)
# print(f_l,sycle,s_l_city)
if b==1:
    now_city=s_l_city
    for j in range((k-f_l)%sycle):
        now_city = s[now_city-1]
    print(now_city)
else:
    now_city=1
    for _ in range(k):
        now_city=s[now_city]
    print(now_city)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [0] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0#ループの最初
for i in range(n-1):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    t[now_city-1]+=1
    if t[now_city-1]==2:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  a = 1
         b = 1
         s_l_city=now_city
         break
    f[now_city-1]=i
# print(t)
# print(f_l,sycle,s_l_city)
if b==1:
    now_city=s_l_city
    for j in range((k-f_l)%sycle):
        now_city = s[now_city-1]
    print(now_city)
else:
    now_city=1
    for _ in range(k):
        now_city=s[now_city]
    print(now_city)

Submission status: Wrong Answer

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

history = {i+1: -1 for i in range(N)}
now = 1
history[now] = 0
start = -1
end = -1
for i in range(K):
    now = A[now - 1]
    if history[now] >= 0:
        start = history[now]
        end = i + 1
        break
    history[now] = i + 1

if start < 0 or end < 0:
    print(now)
else:
    index = (K - start) % (end - start)
    print(A[start:end][index])

Submission status: Wrong Answer

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

flag = [True]*n
root = [1]
now = 0
while flag[now]:
    flag[now] = False
    now = A[now] -1
    root.append(now+1)

to_p = root.index(root[-1])
p2p = root[to_p:-1]
looplen = len(p2p)

if k < looplen:
    print(root[k])
else:    
    k -= to_p
    k %= looplen
    print(p2p[k])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int,input().split()))
flag = [True]*n
root = [1]
now = 0

while flag[now]:
    flag[now] = False
    now = A[now] -1
    root.append(now+1)

to_p = root.index(root[-1])
p2p = root[to_p:-1]
rooplen = len(p2p)

if k < rooplen:
    print(root[k])
else:    
    idx = (k-to_p)
    idx %= rooplen
    print(p2p[idx])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int,input().split()))
flag = [True]*n
p = 0
rt = [1]

while flag[p]:
    flag[p] = False
    p = A[p] -1
    rt.append(p+1)

to_p = rt.index(rt[-1])
p2p = rt[to_p:-1]
p2p_len = len(p2p)

if k < p2p_len:
    print(rt[k])
else:    
    idx = (k-to_p)%p2p_len
    print(p2p[idx])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int,input().split()))
flag = [True]*n
p = 0
rt = [1]

while flag[p]:
    flag[p] = False
    p = A[p] -1
    rt.append(p+1)

to_p = rt.index(rt[-1])
p2p = rt[to_p:-1]
p2p_len = len(p2p)

if k < p2p_len:
    print(rt[k])
else:
    print(p2p[(k-to_p)%p2p_len])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(i-1 for i in map(int,input().split()))
flag = [True]*n
p = 0
rt = [0]

while flag[p]:
    flag[p] = False
    p = A[p]
    rt.append(p)

to_p = rt.index(rt[-1])
p2p = rt[to_p:-1]

if k < len(p2p):
    print(rt[k]+1)
else:
    print(p2p[(k-to_p)%len(p2p)]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(i-1 for i in map(int,input().split()))
flag = [True]*n
p = 0
rt = [0]

while flag[p]:
    flag[p] = False
    p = A[p]
    rt.append(p)

to_p = rt.index(rt[-1])
p2p = rt[to_p:-1]

if k < len(p2p):
    print(rt[k])
else:
    print(p2p[(k-to_p)%len(p2p)]+1)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(i-1 for i in map(int,input().split()))
flag = [True]*n
p = 0
rt = [0]

while flag[p]:
    flag[p] = False
    p = A[p]
    rt.append(p)

to_p = rt.index(rt[-1])
p2p = rt[to_p:-1]
print(p2p[(k-to_p)%len(p2p)]+1)

Submission status: Wrong Answer

Solution code:
from collections import deque 

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

def next_town(graph, now):
    return graph[now-1]

def find_roop(graph):
    n=len(graph)
    visited= deque([1])
    p = 1
    to_p = 0
    for _ in range(n):
        p = next_town(graph,p)
        to_p += 1
        if p not in visited:
            visited.append(p)
        else:
            q = -1
            p2p=0
            while q != p:
                q = visited.pop()
                p2p += 1
            return to_p-p2p, p, p2p
            

to_p, p, p2p = find_roop(A)

ans = p
for _ in range((k-to_p)%p2p):
    ans = next_town(A,ans)
print(ans)

Submission status: Wrong Answer

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

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

explored = [0] * n
explored[0] = 1
t = 0
i = 0
cycle = 0
start = 0
rest = 0
while True:
    t = a[t] - 1
    if explored[t] == 2:
        start = t
        break

    if explored[t] == 1:
        rest = k - i
        explored[t] = 2
        cycle += 1
    else:
        explored[t] = 1
    i += 1

t = start
for _ in range(rest % cycle):
    t = a[t] - 1

print(a[t])

Submission status: Wrong Answer

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

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

explored = [0] * n
explored[0] = 1
t = 0
rest = 0
i = 0
cycle = []
while True:
    t = a[t] - 1
    if explored[t] == 2:
        break

    if explored[t] == 1:
        rest = k - i
        explored[t] = 2
        cycle.append(t)
    else:
        explored[t] = 1
    i += 1

print(a[cycle[rest % len(cycle)]])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [1] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0#ループの最初
for i in range(n-1):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+10):
    now_city = s[now_city-1]
    t[now_city-1]+=1
    if t[now_city-1]==2:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
        #  a = 1
         b = 1
         s_l_city=now_city
         break
    f[now_city-1]=i
# print(t)
# print(f_l,sycle,s_l_city)
if b==1:
    now_city=s_l_city
    for j in range((k-f_l)%sycle):
        now_city = s[now_city-1]
    print(now_city)
else:
    now_city=1
    for j in range(k):
        now_city=s[now_city]
    print(now_city)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [1] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0
for i in range(n-1):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+1):
    now_city = s[now_city-1]
    t[now_city-1]+=1
    if t[now_city-1]==2:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
         a = 1
         b = 1
         s_l_city=now_city
         break
    f[now_city-1]=i
# print(t)
# print(f_l,sycle,s_l_city)
if b==1:
    now_city=s_l_city
    for j in range((k-f_l)%sycle):
        now_city = s[now_city-1]
    print(now_city)
else:
    now_city=1
    for j in range(k):
        now_city=s[now_city]
    print(now_city)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [1] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0
for i in range(n-1):
    t.append(0)
    f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+1):
    now_city = s[now_city-1]
    t[now_city-1]+=1
    if t[now_city-1]==2:
        #  print(i,f[j])
         sycle=i-f[now_city-1]
         f_l=f[now_city-1]
         a = 1
         b = 1
         s_l_city=now_city
         break
    f[now_city-1]=i
# print(t)
# print(f_l,sycle,s_l_city)
if b==1:
    now_city=s_l_city
    for j in range((k-f_l)%sycle):
        now_city = s[now_city-1]
    print(now_city)
else:
    for j in range(k):
        now_city=s[now_city]
    print(now_city)