input stringlengths 20 127k | target stringlengths 20 119k | problem_id stringlengths 6 6 |
|---|---|---|
N, K = list(map(int, input().split()))
T_list = list(map(int, input().split()))
T_list = [T_list[i]-1 for i in range(N)]
count_list = []
for s_point in range(N):
point = s_point
count = 0
i = 0
while i < N:
point = T_list[point]
if point == s_point:
count = i+1
break
i += 1
count_list.append(count)
point = 0
while count_list[point] == 0:
point = T_list[point]
count += 1
L = (K-count) % count_list[point]
i = 0
while i < L:
point = T_list[point]
i += 1
print((point+1)) | N, K = list(map(int, input().split()))
T_list = list(map(int, input().split()))
T_list = [T_list[i]-1 for i in range(N)]
point = 0
syuuki = 0
if K < N:
i = 0
while i < K:
point = T_list[point]
i += 1
else:
i = 0
while i < N:
point = T_list[point]
if point == 0:
syuuki = i+1
break
i += 1
second_point = point
if syuuki != 0:
L = K % syuuki
else:
j = 0
while j < N:
point = T_list[point]
if point == second_point:
syuuki = j+1
break
j += 1
L = (K-i) % syuuki
k = 0
while k < L:
point = T_list[point]
k += 1
print((point+1)) | p02684 |
N,K=list(map(int,input().split()))
t=list(map(int,input().split()))
g=[1]
h={1}
for i in range(N-1):
x=t[g[i]-1]
g.append(x)
if x in h:
break
h.add(x)
if K>len(g):
a=g.index(g[-1])
b=len(g)-a-1
K=(K-a)%b+a
print((g[K])) | N,K=list(map(int,input().split()))
t=list(map(int,input().split()))
g=[1]
for i in range(N-1):
g.append(t[g[i]-1])
if K>len(g):
a=g.index(g[-1])
b=len(g)-a-1
K=(K-a)%b+a
print((g[K])) | p02684 |
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
visited=[0]*n
if k<=n:
now=0
nex=0
loop_start=0
target=0
for i in range(n):
now=nex
visited[now]+=1
nex=a[now]-1
if i+1==k:
print((nex+1))
exit()
now=0
nex=0
loop_start=0
target=0
lis=[]
flag=False
for i in range(2*n):
now=nex
visited[now]+=1
nex=a[now]-1
if flag:
if now+1 not in lis:
lis.append(now+1)
continue
else:
break
if visited[now]==2:
loop_start=i
target=now+1
lis.append(now+1)
flag=True
dif=len(lis)
first=loop_start-dif
k-=first
print((lis[k%dif]))
| n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
visited=[0]*n
if k<=n:
now=0
nex=0
loop_start=0
target=0
for i in range(n):
now=nex
visited[now]+=1
nex=a[now]-1
if i+1==k:
print((nex+1))
exit()
now=0
nex=0
loop_start=0
target=0
lis=[]
s=set()
flag=False
for i in range(2*n):
now=nex
visited[now]+=1
nex=a[now]-1
if flag:
if now+1 not in s:
lis.append(now+1)
s.add(now+1)
continue
else:
break
if visited[now]==2:
loop_start=i
target=now+1
lis.append(now+1)
s.add(now+1)
flag=True
dif=len(lis)
first=loop_start-dif
k-=first
print((lis[k%dif]))
| p02684 |
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[0]*(N+1)
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N+1):
B[i]=A[B[i-1]-1]
# if c==B[i]:break
# if A.count(B[i])>1 and d==0:c=B[i];d+=1
d=B.index(B[-1])+1
#print(B.index(B[N-1]))
#print(B,d)
#print(d+1+(K-d-1)%(N-d-1))
#print(B[K],K,d,(K+1-d),(N+1-d))
if K<=N:print((B[K]));exit()
if (K+1-d)%(N+1-d)==0:print((B[-1]))
else:print((B[d-1+(K+1-d)%(N+1-d)]))
#print(B[d+1+(K-d-1)%(N-d-1)]-1) | N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[0]*(N+1)
B[0]=1;c=0;d=0
if B[0] in A:c=B[0]
for i in range(1,N+1):
B[i]=A[B[i-1]-1]
d=B.index(B[-1])+1
if K<=N:print((B[K]));exit()
#if (K+1-d)%(N+1-d)==0:print(B[-1])
print((B[d-1+(K+1-d)%(N+1-d)]))
| p02684 |
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
N, K = MAP()
A = [a-1 for a in LIST()]
MAX = 64
nxt = list2d(MAX, N, -1)
for i, a in enumerate(A):
nxt[0][i] = a
for k in range(1, MAX):
for i in range(N):
nxt[k][i] = nxt[k-1][nxt[k-1][i]]
ans = 0
for k in range(MAX-1, -1, -1):
if K >> k & 1:
ans = nxt[k][ans]
print((ans+1))
| import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def doubling(MAX, A):
""" ダブリング """
N = len(A)
nxt = list2d(MAX, N, -1)
for i, a in enumerate(A):
nxt[0][i] = a
for k in range(1, MAX):
for i in range(N):
nxt[k][i] = nxt[k-1][nxt[k-1][i]]
return nxt
N, K = MAP()
A = [a-1 for a in LIST()]
MAX = 64
nxt = doubling(MAX, A)
ans = 0
for k in range(MAX-1, -1, -1):
if K >> k & 1:
ans = nxt[k][ans]
print((ans+1))
| p02684 |
N,K = list(map(int,input().split(' ')))
A = list(map(int,input().split(' ')))
# 戻ってくるまでのリスト
R=[]
Now=1
cnt=0
for i in range(N):
if Now in R:
Start=R.index(Now)
End = cnt
break
else:
R.append(Now)
Now=A[Now-1]
cnt+=1
if K < End:
ans=R[K]
else:
key=Start+(K-Start)%(End-Start)
ans=R[key]
print(ans)
#print(Start,End)
#print(R)
#Start+K(End-Start) | N,K = list(map(int,input().split(' ')))
A = list(map(int,input().split(' ')))
# 戻ってくるまでのリスト
R=[]
Rs=set()
Now=1
cnt=0
for i in range(N):
if Now in Rs:
Start=R.index(Now)
End = cnt
break
else:
R.append(Now)
Rs.add(Now)
Now=A[Now-1]
cnt+=1
if K < End:
ans=R[K]
else:
key=Start+(K-Start)%(End-Start)
ans=R[key]
print(ans)
#print(Start,End)
#print(R)
#Start+K(End-Start) | p02684 |
N,K = list(map(int, input().split()))
List = list(map(int, input().split()))
a=[-1]*N
b=1
for i in range(N):
if not b in a:
a[i]=b
b=List[b-1]
else:
c=i
break
#print(a)
#print(b,c)
num=a.index(b)
#print(T,num)
T=c-num
if K<num:
print((a[K]))
else:
print((a[num:][(K-num)%T])) | N,K = list(map(int, input().split()))
List = list(map(int, input().split()))
a=[]
for i in range(N):
a.append(-1)
#print(a)
b=1
for i in range(N):
if a[b-1]==-1:
a[b-1]=i+1
b=List[b-1]
else:
c=i
break
#print(a)
#print(b,c)
num=a[b-1]-1
T=c-num
#print(T)
e=1
if K<num:
for i in range(K):
e=List[e-1]
print(e)
else:
qw=(K-num)%T
List_new=[]
for i in range(T):
List_new.append(b)
b=List[b-1]
print((List_new[qw])) | p02684 |
def resolve():
'''
code here
'''
import collections
N, K = [int(item) for item in input().split()]
A_list = [int(item) for item in input().split()]
bit_k = bin(K)[2:]
telepo_list = collections.deque([A_list])
for _ in range(len(bit_k)-1):
temp_list = telepo_list[-1]
new_telep = collections.deque([])
for i in temp_list:
new_telep.append(temp_list[i-1])
telepo_list.append(new_telep)
# print(telepo_list)
goto = 1
for i in range(len(bit_k)):
temp_list = telepo_list.popleft()
if ((K >> i) & 1):
goto = temp_list[goto -1]
# print(i, goto)
print(goto)
if __name__ == "__main__":
resolve()
| def resolve():
'''
code here
'''
N, K = [int(item) for item in input().split()]
A_list = [int(item) for item in input().split()]
bit_k = bin(K)[2:]
telepo_list = [A_list]
for _ in range(len(bit_k)-1):
temp_list = telepo_list[-1]
new_telep = [temp_list[i-1] for i in temp_list]
telepo_list.append(new_telep)
# print(telepo_list)
goto = 1
for i in range(len(bit_k)):
temp_list = telepo_list[i]
if ((K >> i) & 1):
goto = temp_list[goto - 1]
# print(i, goto)
print(goto)
if __name__ == "__main__":
resolve()
| p02684 |
#4問目
import sys
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
#すでに移動したことのある場所
already = [False] * N
step = 0
now = 1
count = 0
while(already[now-1] != True):
already[now - 1] = True
now = A[now - 1]
count += 1
if count == K:
print(now)
exit(0)
# count はstartから2度現れる点までの移動数
#ループに入るまで何回ステップがあったか調べる
# loop := ループに含まれる辺の個数
loop = 0
cycle = 0
if(now == 1):
step = 0
cycle = count
loop = K % cycle
else:
step = 0
temp = 1
while(temp != now):
temp = A[temp - 1]
step += 1
cycle = count - step
loop= (K - step) % cycle
temp = 1
for i in range(step + loop):
temp = A[temp - 1]
print(temp)
| #4問目
import sys
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
#すでに移動したことのある場所
already = [False] * N
step = 0
now = 1
count = 0
while(already[now-1] != True):
already[now - 1] = True
now = A[now - 1]
count += 1
if count == K: # ループに入る前にKになったらそれで終了
print(now)
exit(0)
# now := ループの開始地点
# count := 地点 1 から now までの移動数
# step := ループに入るまでにワープする回数
# cycle := ループに含まれる辺の個数
# loop := (K-step) % cycle
# (残りの(K-step)回ループの中で回したときに ループしないで終わる回数)
loop = 0
cycle = 0
if(now == 1):
step = 0
cycle = count
loop = K % cycle
else:
step = 0
temp = 1
while(temp != now):
temp = A[temp - 1]
step += 1
cycle = count - step
loop= (K - step) % cycle
temp = 1
for i in range(step + loop):
temp = A[temp - 1]
print(temp) | p02684 |
import heapq
def binary_search(data, value):
left = 0 # 探索する範囲の左端を設定
right = len(data) - 1 # 探索する範囲の右端を設定
while left <= right:
mid = (left + right) // 2 # 探索する範囲の中央を計算
if data[mid] == value:
# 中央の値と一致した場合は位置を返す
return mid
elif data[mid] < value:
# 中央の値より大きい場合は探索範囲の左を変える
left = mid + 1
else:
# 中央の値より小さい場合は探索範囲の右を変える
right = mid - 1
return -1 # 見つからなかった場合
n, k = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
way = [0]
hq = [0]
heapq.heapify(hq)
posi = 0
cycle = 0
while True:
if a[posi] in way:
rest = way.index(a[posi])
cycle -= (rest - 1)
break
way.append(a[posi])
heapq.heappush(hq, a[posi])
posi = a[posi]
cycle += 1
# print(way)
# print(cycle)
if k < len(way):
print((way[k] + 1))
else:
print((way[(k-rest) % cycle + rest] + 1)) | import heapq
def binary_search(data, value):
left = 0 # 探索する範囲の左端を設定
right = len(data) - 1 # 探索する範囲の右端を設定
while left <= right:
mid = (left + right) // 2 # 探索する範囲の中央を計算
if data[mid] == value:
# 中央の値と一致した場合は位置を返す
return mid
elif data[mid] < value:
# 中央の値より大きい場合は探索範囲の左を変える
left = mid + 1
else:
# 中央の値より小さい場合は探索範囲の右を変える
right = mid - 1
return -1 # 見つからなかった場合
n, k = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
way = [0]
hq = [0]
heapq.heapify(hq)
posi = 0
cycle = 0
while True:
if binary_search(hq, a[posi]) != -1:
rest = way.index(a[posi])
cycle -= (rest - 1)
break
way.append(a[posi])
heapq.heappush(hq, a[posi])
posi = a[posi]
cycle += 1
# print(way)
# print(cycle)
if k < len(way):
print((way[k] + 1))
else:
print((way[(k-rest) % cycle + rest] + 1)) | p02684 |
NK = list(map(int, input().split()))
N = NK[0]
K = NK[1]
A = list(map(int, input().split()))
done = [1]
now = 1
for i in range(1,N+1):
nexttown = A[now-1]
#print(now, "->", nexttown)
if nexttown not in done:
done.append(nexttown)
now = nexttown
else:
break
start = done.index(nexttown)
end = len(done)-1
before = start #ループに入るまでの転移数
loop = done[start:]
if K < before:
saisyuu = done[K]
else:
nokori = K-before
n_loop = int(nokori/len(loop))
hasuu = nokori%len(loop)
loopfirst = A[done[-1]-1]
saisyuu = loop[hasuu]
#print("1から",before,"回転移して",loopfirst,"に到着した後、大きさ",loop,"のループを",n_loop,"回転移して",loopfirst,"にいます。")
#print("その後、",hasuu,"回の転移をして",saisyuu,"に到着します。")
print(saisyuu)
| NK = list(map(int, input().split()))
N = NK[0]
K = NK[1]
A = list(map(int, input().split()))
done = [1]
donedic = {}
donedic[1] = 1
now = 1
for i in range(1,N+1):
nexttown = A[now-1]
#print(now, "->", nexttown)
try:
a = donedic[nexttown]
except:
donedic[nexttown] = 1
done.append(nexttown)
now = nexttown
else:
break
start = done.index(nexttown)
end = len(done)-1
before = start #ループに入るまでの転移数
loop = done[start:]
if K < before:
saisyuu = done[K]
else:
nokori = K-before
n_loop = int(nokori/len(loop))
loopfirst = done[start]
hasuu = nokori%len(loop)
saisyuu = loop[hasuu]
#print("1から",before,"回転移して",loopfirst,"に到着します。")
#print("その後、大きさ",len(loop),"のループを",n_loop,"回転移して",loopfirst,"にいます。")
#print("その後、",hasuu,"回の転移をして",saisyuu,"に到着します。")
print(saisyuu)
| p02684 |
n,k = list(map(int,input().split()))
a = [0] + list(map(int,input().split())) #1_indexed
tele = [1]
appear = []
while True:
next_number = a[tele[len(tele)-1]]
if not next_number in appear:
tele.append(next_number)
appear.append(next_number)
else:
repeat = tele.index(next_number)
tele_1 = tele[:repeat]
tele_2 = tele[repeat:]
break
if k < len(tele_1):
print((tele_1[k]))
else:
k_new = (k-len(tele_1)+1) % len(tele_2)
print((tele_2[k_new-1])) | n,k = list(map(int,input().split()))
a = [0] + list(map(int,input().split())) #1_indexed
tele = [1]
used = [False for i in range(n)]
while True:
next_number = a[tele[len(tele)-1]]
if not used[next_number-1]:
tele.append(next_number)
used[next_number-1] = True
else:
repeat = tele.index(next_number)
tele_1 = tele[:repeat]
tele_2 = tele[repeat:]
break
if k < len(tele_1):
print((tele_1[k]))
else:
k_new = (k-len(tele_1)+1) % len(tele_2)
print((tele_2[k_new-1])) | p02684 |
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
for num,i in enumerate(a):
d[num+1] = i
t = []
i = 1
j = 1
while(i not in t):
t.append(i)
i = d[i]
for num,j in enumerate(t):
if j == i:
b = num
break
a = 1
if k < b+1:
ans = t[k]
else:
k = (k-b)%(len(t)-b)
ans = t[b+k]
print(ans) | n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
for num,i in enumerate(a):
d[num+1] = i
t = {}
t_ = {}
i = 1
j = 0
while(i not in t):
t[i] = j
t_[j] = i
i = d[i]
j += 1
a = 1
b = t[i]
if k < b+1:
ans = t_[k]
else:
k = (k-b)%(len(list(t.keys()))-b)
ans = t_[b+k]
print(ans) | p02684 |
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
A = [A[k]-1 for k in range(N)]
now = 0
for k in range(1000):
if (K>>k)&1 == 1:
now = A[now]
A = [A[A[l]] for l in range(N)]
print((now+1))
| N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
A = [A[k]-1 for k in range(N)]
now = 0
for k in range(70):
if (K>>k)&1 == 1:
now = A[now]
A = [A[A[l]] for l in range(N)]
print((now+1))
| p02684 |
n, k = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
c = [None] * 200000
p = 0
st = 0
ed = 0
i = 0
while True:
c[i] = p
i += 1
p = a[p]
if p in c:
st = c.index(p)
ed = i
break
if k < ed:
print((c[k] + 1))
else:
k -= st
k %= ed - st
print((c[st + k] + 1)) | n, k = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
c = [None] * 200000
cc = set()
p = 0
st = 0
ed = 0
i = 0
while True:
c[i] = p
cc.add(p)
i += 1
p = a[p]
if p in cc:
st = c.index(p)
ed = i
break
if k < ed:
print((c[k] + 1))
else:
k -= st
k %= ed - st
print((c[st + k] + 1)) | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
num = 1
li = [1]
flag = [True] * n
flag[0] = False
for i in range(k):
num = a[num-1]
if flag[num-1]:
li.append(num)
flag[num-1] = False
else:
break
d = li.index(num)
ans = (k-d)%(len(li)-d)+d
print((li[ans])) | n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
tt = [0] * n
tt[0] = 1
ans_l = [1]
i = 1
for j in range(k):
i = a[i-1]
if tt[i-1] == False:
tt[i-1] = 1
ans_l.append(i)
else:
break
d = ans_l.index(i)
k -= d
ans = k % (len(ans_l)-d)
print((ans_l[ans+d]))
| p02684 |
n, k = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
pos = 1
seen = [1]
while True:
if A[pos] in seen:
seen.append(A[pos])
break
seen.append(A[pos])
pos = A[pos]
length = len(seen)
initial, loop = 0, 0
for i in range(length):
if seen[i] == seen[-1]:
initial = i
loop = length - 1 - initial
break
if k < length:
print((seen[k]))
exit()
kk = (k - initial) % loop
print((seen[initial + kk])) | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
order = [-1] * (n + 1)
s = []
v = 1
while order[v] == -1:
order[v] = len(s)
s.append(v)
v = a[v - 1]
c = len(s) - order[v]
l = order[v]
if k < l:
print((s[k]))
else:
k -= l
k %= c
print((s[l + k])) | p02684 |
import sys
def mink(numk,l):
x = 1
for _ in range(k):
x = a[x-1]
return x
input = sys.stdin.readline
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
# kが小さい時
if k <= 10**5:
print((mink(k,a)))
sys.exit()
# ans = 0
z = 1
p = [1]
for _ in range(k):
z = a[z-1]
if z in p:
p.append(z)
break
p.append(z)
# 周期の来る前の回数
num = p.index(p[-1])
p = p[num:]
# 周期
pn = len(p)-1
k -= num
# 回数/周期のあまり
anum = k%pn
print((p[anum]))
| import sys
input = sys.stdin.readline
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
p = [1]
for i in range(n):
p.append(a[p[i]-1])
if k <= n:
print((p[k]))
else:
# 周期の来る前の回数
num = p.index(p[-1])
p = p[num:]
# 周期
pn = len(p)-1
k -= num
# 回数/周期のあまり
ans = k%pn
print((p[ans])) | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
past = [0]*min(K, 2*10**5)
now = 1
step = 0
for i in range(K):
past[i] = now
now = A[now-1]
if now in past:
step += 1
break
if step > 0:
id = past.index(now)
step = i+1 - id
p = (K-id) % step
now = past[id+p]
print(now) | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
past = [0]*min(K, 2*10**5)
ps = set()
now = 1
step = 0
for i in range(K):
past[i] = now
ps.add(now)
now = A[now-1]
if now in ps:
step += 1
break
if step > 0:
id = past.index(now)
step = i+1 - id
p = (K-id) % step
now = past[id+p]
print(now)
| p02684 |
from collections import deque
N, K = list(map(int, input().split()))
K -= 1
A = list([int(x) - 1 for x in input().split()])
seen = deque()
now = 0
for i in range(N):
seen.append(now)
to = A[now]
now = to
if now in seen:
seen.append(now)
break
loop_s = seen.index(now)
loop_end = seen.index(now, loop_s + 1)
size = loop_end - loop_s
if K <= loop_s:
print((seen[K+1] + 1))
else:
K -= loop_s
print((seen[loop_s + 1 + K % size] + 1)) | N, K = list(map(int, input().split()))
K -= 1
A = list([int(x) - 1 for x in input().split()])
seen = [None]*N
seen_set = set()
now = 0
for i in range(N):
seen[i] = now
seen_set.add(now)
to = A[now]
now = to
if now in seen_set:
seen[i+1] = now
loop_s = seen.index(now)
loop_end = i + 1
size = loop_end - loop_s
break
if K <= loop_s:
print((seen[K+1] + 1))
else:
K -= loop_s
print((seen[loop_s + 1 + K % size] + 1)) | p02684 |
N, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
now = 0
seen = []
while True:
seen.append(now)
teleporter = A[now]
A[now] = 'used'
if teleporter == 'used':
break
else:
now = teleporter
loop_s = seen.index(seen[-1])
size = len(seen) - loop_s - 1
if K <= loop_s:
print((seen[K] + 1))
else:
print((seen[loop_s +(K - loop_s) % size] + 1)) | N, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
now = 0
seen = []
while True:
seen.append(now)
teleporter = A[now]
A[now] = 'used'
if teleporter == 'used':
break
else:
now = teleporter
loop_s = seen.index(now)
size = len(seen) - loop_s - 1
if K <= loop_s:
print((seen[K] + 1))
else:
print((seen[loop_s +(K - loop_s) % size] + 1)) | p02684 |
N, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
v = [[-1]*N for i in range(61)]
v[0] = A[:]
now = 0
for i in range(60):
for j in range(N):
v[i+1][j] = v[i][v[i][j]]
if K>>i & 1:
now = v[i][now]
print((now + 1)) | N, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
v = [A[:]]
now = 0
for i in range(60):
tmp = v[-1]
v.append([tmp[x] for x in tmp])
if K>>i & 1:
now = v[i][now]
print((now + 1)) | p02684 |
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
A=[-1]+A
still=[1]
before=1
rS=-1
root=[1]
while True:
dest=A[before]
if dest in still:
rS=dest
break
else:
still.append(dest)
root.append(dest)
before=dest
#print(root,rS)
num=0
for i in root:
if i==rS:
break
num+=1
#print(num)
if K<=num:
print((root[K]))
else:
K-=num
root2=root[num:]
print((root2[K%len(root2)]))
| N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
A=[-1]+A
still=[0]*(N+1)
still[1]=1
before=1
rS=-1
root=[1]
while True:
dest=A[before]
if still[dest]==1:
rS=dest
break
else:
still[dest]=1
root.append(dest)
before=dest
#print(root,rS)
num=0
for i in root:
if i==rS:
break
num+=1
#print(num)
if K<=num:
print((root[K]))
else:
K-=num
root2=root[num:]
print((root2[K%len(root2)]))
| p02684 |
N,K = list(map(int, input().split()))
A = [int(x)-1 for x in map(int, input().split())]
l = []
place = 0
while True:
if K==0:
print((place+1))
exit()
l.append(place)
place = A[place]
K -= 1
if place in l:
ind = l.index(place)
count = len(l) - ind
print((l[ind + K%count]+1))
exit() | N,K = list(map(int, input().split()))
A = [int(x)-1 for x in map(int, input().split())]
flag = [False]*N
l = []
place = 0
while True:
if K==0:
print((place+1))
exit()
l.append(place)
flag[place]=True
place = A[place]
K -= 1
if flag[place]:
ind = l.index(place)
count = len(l) - ind
print((l[ind + K%count]+1))
exit() | p02684 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
def main():
N,K = list(map(int, readline().split()))
A = [int(i) for i in readline().split()]
d = [0] * (N+1)
d[1] = 1
now = 1
next_ = A[0]
c = [0] * (N+1)
cnt = 0
while d[next_] == 0:
d[next_] = d[now] + 1
now = next_
next_ = A[next_-1]
cnt += 1
c[now] = cnt
s = c[next_]
loop = cnt - s
if K <= s:
ans = c[1:].index(K) + 1
else:
n = (K - s) % (loop + 1)
ans = c[1:].index(s + n) + 1
print(ans)
if __name__ == "__main__":
main()
| import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
def main():
N,K = list(map(int, readline().split()))
A = [int(i) - 1 for i in readline().split()]
p = 0
route = []
seen = set()
while True:
route.append(p)
seen.add(p)
p = A[p]
if p in seen:
break
loop_start = route.index(p)
loop_len = len(route) - loop_start
if K <= loop_start:
ans = route[K] + 1
else:
r = (K - loop_start) % loop_len
ans = route[loop_start + r] + 1
print(ans)
if __name__ == "__main__":
main()
| p02684 |
N, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
path = [1]
current_pos = 1
detect_loop = False
while not detect_loop:
current_pos = A[current_pos - 1]
if current_pos in path:
detect_loop = True
path.append(current_pos)
loop = path[(idx := path.index(current_pos)):-1]
straight = path[:idx]
if K < len(straight):
print((straight[K]))
else:
print((loop[(K - len(straight)) % len(loop)]))
| N, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
path = [1]
checked = [False] * N
detect_loop = False
current_pos = 1
while not detect_loop:
current_pos = A[current_pos - 1]
if checked[current_pos - 1]:
detect_loop = True
path.append(current_pos)
checked[current_pos - 1] = True
loop = path[(idx := path.index(current_pos)):-1]
straight = path[:idx]
if K < len(straight):
print((straight[K]))
else:
print((loop[(K - len(straight)) % len(loop)]))
| p02684 |
import sys
readline = sys.stdin.readline
n, k = list(map(int, readline().split()))
A = [i - 1 for i in map(int, readline().split())]
m = 64
d = [A]
pre = A
for i in range(m):
current = [pre[pre[j]] for j in range(n)]
d.append(current)
pre = current
route = 0
for i in range(m-1, -1, -1):
if k >= (x := 1 << i):
route = d[i][route]
k -= x
print((route+1))
| import sys
readline = sys.stdin.readline
n, k = list(map(int, readline().split()))
A = [i - 1 for i in map(int, readline().split())]
m = 60
d = [A]
pre = A
for i in range(m):
current = [pre[pre[j]] for j in range(n)]
d.append(current)
pre = current
route = 0
for i in range(m-1, -1, -1):
if k >= (x := 1 << i):
route = d[i][route]
k -= x
print((route+1))
| p02684 |
N, K = list(map(int, input().split()))
A = list(map(int,input().split()))
A.insert(0,-1)
root = [1]
next = A[1]
rf = 0
for _ in range(K):
if next in root:
rf = root.index(next)
root = root[rf:]
break
root.append(next)
next = A[next]
print((root[(K - rf) % len(root)]))
| N, K = list(map(int, input().split()))
A = list(map(int,input().split()))
A.insert(0,-1)
root = [1]
for _ in range(N-1):
root.append(A[root[-1]])
if K > N:
start = root.index(root[-1])
index = (K - start) % (len(root) - start - 1) + start
ans = root[index]
else:
ans = root[K]
print(ans)
| p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = [None] * N
i = 0
cur = 0
m = None
while True:
if r[cur] is not None:
m = i - r[cur]
K -= ((K-i)//m)*m
r[cur] = i
i += 1
if i == K:
print((A[cur]))
exit()
cur = A[cur] - 1 | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
r = [None] * N
i = 0
cur = 0
m = None
while True:
if m is None and r[cur] is not None:
m = i - r[cur]
K = i + (K-i)%m
else:
r[cur] = i
if i == K:
print((cur+1))
exit()
i += 1
cur = A[cur] - 1 | p02684 |
from collections import deque
import sys
n, k = list(map(int, input().split()))
a = list([int(x)-1 for x in input().split()])
visited = deque()
i = 0
cnt = 0
while True:
if cnt == k:
print((i+1))
sys.exit()
if i in visited:
break
visited.append(i)
i = a[i]
cnt += 1
si = visited.index(i)
loop = list(visited)[si:]
n_loop = len(loop)
left_k = k - cnt
print((loop[left_k % n_loop] + 1)) | import sys
n, k = list(map(int, input().split()))
a = list([int(x)-1 for x in input().split()])
is_visited = [0] * n
visited = list()
i = 0
cnt = 0
while True:
if cnt == k:
print((i+1))
sys.exit()
if is_visited[i]:
break
is_visited[i] = 1
visited.append(i)
i = a[i]
cnt += 1
si = visited.index(i)
loop = visited[si:]
n_loop = len(loop)
left_k = k - cnt
print((loop[left_k % n_loop] + 1)) | p02684 |
a, b = list(map(int, input().split()))
c = list(map(int, input().split()))
itta = []
mati = 1
i = 0
while True:
if mati in itta:
saisyo = mati
break
else:
itta.append(mati)
mati = c[mati-1]
i += 1
risuto = []
while True:
if mati in risuto:
break
else:
risuto.append(mati)
mati = c[mati-1]
if b <= i-1:
print((itta[b]))
else:
print((risuto[(b-i) % len(risuto)])) | a, b = list(map(int, input().split()))
c = list(map(int, input().split()))
itta2 = set([])
mati = 1
i = 0
itta = []
while True:
if mati in itta2:
saisyo = mati
break
else:
itta2.add(mati)
itta.append(mati)
mati = c[mati-1]
i += 1
risuto2 = set([])
risuto = []
while True:
if mati in risuto2:
break
else:
risuto2.add(mati)
risuto.append(mati)
mati = c[mati-1]
if b <= i-1:
print((itta[b]))
else:
print((risuto[(b-i) % len(risuto)])) | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
t = 0
now = 1
footprint = [1]
while t < K:
if A[now-1] not in footprint:
footprint.append(A[now-1])
now = A[now-1]
t += 1
else:
s = footprint.index(A[now-1])
l = t - s + 1
e = (K-s) % l
now = footprint[s+e]
break
print(now) | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
t = 0
now = 1
footprint = [1]
past = [0]*N
past[0] = 1
while t < K:
if past[A[now-1]-1] == 0:
now = A[now-1]
footprint.append(now)
past[now-1]=1
t += 1
else:
s = footprint.index(A[now-1])
l = t - s + 1
e = (K-s) % l
now = footprint[s+e]
break
print(now) | p02684 |
(N,K) = list(map(int,input().split()))
A = list(map(int,input().split()))
route = [1]
past = 1
for i in range(K):
past = A[past-1]
if past in route:
loop = (route.index(past),i+1)
flag = 0
break
else:
flag = 1
route.append(past)
if flag == 1:
print((route[-1]))
else:
print((route[loop[0]+(K-loop[0])%(loop[1]-loop[0])]))
| (N,K) = list(map(int,input().split()))
A = list(map(int,input().split()))
route = [1]
App = [0]*N
past = 1
for i in range(K):
past = A[past-1]
if App[past-1]==1:
loop = (route.index(past),i+1)
flag = 0
break
else:
App[past-1] = 1
flag = 1
route.append(past)
if flag == 1:
print((route[-1]))
else:
print((route[loop[0]+(K-loop[0])%(loop[1]-loop[0])]))
| p02684 |
N, K = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
B = [1]
i = 1
n = 0
l = 1
while 1:
i = A[i]
if i not in B:
B.append(i)
l = l + 1
if l == K + 1:
print(i)
break
else:
n = B.index(i)
print((B[n + (K-n) % (l-n)]))
break | N, K = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
B = [-1 for i in range(N+1)]
B[1] = 0
n = 1
m = 0
while 1:
n = A[n]
m += 1
if m == K:
print(n)
break
if B[n] == -1:
B[n] = m
else:
a = (K - m) % (m - B[n])
for i in range(a):
n = A[n]
print(n)
break | p02684 |
n,k = list(map(int,input().split()))
dic = {}
for key, a in zip(list(range(1, n+1)), list(map(int,input().split()))):
dic[key] = a
keynum = 0
log = [1]
next = dic[1]
log.append(next)
k -= 1
while k:
next = dic[next]
if next in log:
keynum = log.index(next)
k -= 1
break
log.append(next)
k -= 1
looplen = len(log[keynum:])
if k < len(log[keynum:]):
print((log[-1]))
else:
res = k%looplen
print((log[keynum+res])) | n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
dic = {}
for key, a in zip(list(range(1, n+1)), a):
dic[key] = a
log = [-1]*(n+1)
next = dic[1]
step = 1
log[next] = step
k -= 1
while k:
next = dic[next]
step += 1
k -= 1
if log[next] != -1:
k %= log[next] - step
else:
log[next] = step
print(next) | p02684 |
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
dic = {}
for key, a in zip(list(range(1, n+1)), a):
dic[key] = a
log = [-1]*(n+1)
next = dic[1]
step = 1
log[next] = step
k -= 1
while k:
next = dic[next]
step += 1
k -= 1
if log[next] != -1:
k %= log[next] - step
else:
log[next] = step
print(next) | n,k = list(map(int,input().split()))
a = [0]
a.extend(list(map(int,input().split())))
l = [None]*(n+1)
now = 1
step = 0
l[now] = 0
while k:
now = a[now]
step += 1
k -= 1
if l[now] is not None:
looplen = (step - l[now])
k = k%looplen
else:
l[now] = step
print(now) | p02684 |
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
# = input()
# = int(input())
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
#
# c = collections.Counter()
ord_ = [-1] * (n + 1)
s = []
v = 1
while ord_[v] == -1:
ord_[v] = len(s)
s.append(v)
v = a[v-1]
c = len(s) - ord_[v]
l = ord_[v]
if k < l:
print((s[k]))
else:
k -= l
k %= c
print((s[l+k]))
| #!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
# = input()
# = int(input())
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
#
# c = collections.Counter()
ord_ = [-1] * (n + 1) # 何回目に訪れたか
s = []
current = 0
while ord_[current] == -1:
ord_[current] = len(s)
s.append(current)
current = a[current] - 1
c = len(s) - ord_[current]
l = ord_[current]
if k < l:
print((s[k] + 1))
else:
k -= l
k %= c
print((s[l+k] + 1))
| p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
history = []
pos = 0
cnt = 0
while pos not in history and cnt < k:
history.append(pos)
pos = a[pos] - 1
cnt += 1
if cnt == k:
print((pos + 1))
else:
cnt_before = history.index(pos)
print((history[cnt_before + (k - cnt_before) % (cnt - cnt_before)] + 1)) | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
history = []
mark = set()
pos = 0
cnt = 0
while pos not in mark and cnt < k:
history.append(pos)
mark.add(pos)
pos = a[pos] - 1
cnt += 1
if cnt == k:
print((pos + 1))
else:
cnt_before = history.index(pos)
print((history[cnt_before + (k - cnt_before) % (cnt - cnt_before)] + 1)) | p02684 |
def main():
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
A = [0] + A
seen = {}
cnt = 0
dst = 1
while K:
if dst in seen:
cycle = cnt - seen[dst]
K %= cycle
seen = {}
else:
seen[dst] = cnt
dst = A[dst]
cnt += 1
K -= 1
print(dst)
if __name__ == "__main__":
main()
| def main():
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
A = [0] + A
seen = {}
find_cnt = {}
cnt = 0
dst = 1
while K:
if dst in seen:
cycle = cnt - seen[dst]
K %= cycle
print((find_cnt[seen[dst]+K]))
return
else:
seen[dst] = cnt
find_cnt[cnt] = dst
dst = A[dst]
cnt += 1
K -= 1
print(dst)
if __name__ == "__main__":
main()
| p02684 |
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
root = [1]
for i in range(n):
t = a[root[i] - 1]
root.append(t)
if (t in root[:-1]):
break
x = root.index(root[-1])
bef = root[0:x]
#print(bef)
loop = root[x:-1]
#print(loop)
#print(root)
if (k < len(bef)):
print((bef[k]))
else:
p = ((k - len(bef)) % len(loop))
#print(p)
print((loop[p])) | n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
root = [1]
for i in range(n):
t = a[root[i]-1]
if (t == 0):
break
a[root[i]-1] = 0
root.append(t)
x = root.index(root[-1])
bef = root[0:x]
#print(bef)
loop = root[x:-1]
#print(loop)
#print(root)
if (k < len(bef)):
print((bef[k]))
else:
p = ((k - len(bef)) % len(loop))
#print(p)
print((loop[p])) | p02684 |
n,k=list(map(int,input().split()))
teleport=list(map(int,input().split()))
double_counter=set()
# mojiretu="1"
mojiretu=["1"]
index=1
double_counter.add(index)
flag=False
for i in range(k):
tmp=teleport[index-1]
if tmp in double_counter:
flag=True
break
else:
double_counter.add(tmp)
# mojiretu+=str(tmp)
mojiretu.append(str(tmp))
index=tmp
else:
print(tmp)
if flag:
# index=mojiretu.find(str(tmp))
# roop_str=mojiretu[index:]
# length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
# print(roop_str[length])
Li = mojiretu.index(str(tmp))
Ri = len(mojiretu)
idx = Li + (k - Li) % (Ri - Li)
print((mojiretu[idx]))
| n,k=list(map(int,input().split()))
teleport=list(map(int,input().split()))
double_counter=set()
# mojiretu="1"
mojiretu=["1"]
index=1
double_counter.add(index)
flag=False
for i in range(k):
tmp=teleport[index-1]
if tmp in double_counter:
flag=True
break
else:
double_counter.add(tmp)
# mojiretu+=str(tmp)
mojiretu.append(str(tmp))
index=tmp
else:
print(tmp)
if flag:
# index=mojiretu.find(str(tmp))
# roop_str=mojiretu[index:]
# length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
# print(roop_str[length])
index = mojiretu.index(str(tmp))
roop_str=mojiretu[index:]
length=k%len(roop_str)-len(mojiretu[:index])%len(roop_str)
print((roop_str[length]))
| p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
l = [1]
lnew = [1]
for i in range(n):
lnew.append(a[l[i]-1])
if lnew[-1] in l == True:
break
else:
l.append(lnew[-1])
del l[-1]
lindex = l.index(l[-1])
del l[-1]
r = len(l) - lindex
rlist = l[lindex:]
if k >= lindex:
kmod = (k - lindex) % r
ans = rlist[kmod]
else:
ans = l[k]
print(ans) | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
l = [1]
b = [1] + [0] * (n-1)
for i in range(n):
b[a[l[i]-1]-1] += 1
if b[a[l[i]-1]-1] == 2:
break
else:
l.append(a[l[i]-1])
lindex = l.index(a[l[i]-1])
r = len(l) - lindex
rlist = l[lindex:]
if k >= lindex:
kmod = (k - lindex) % r
ans = rlist[kmod]
else:
ans = l[k]
print(ans) | p02684 |
if __name__=="__main__":
N, K = list(map(int, input().split(' ')))
_town_list = input().split(' ')
town_list = [int(n) for n in _town_list]
next_town = town_list[0]
visited_town = [1]
visited_town.append(next_town)
counter = 1
while True:
counter += 1
next_town = town_list[next_town-1]
if counter == K:
print(next_town)
break
if next_town in visited_town:
repeated_town_idx = visited_town.index(next_town)
loop_num = counter - repeated_town_idx
print((visited_town[(K-repeated_town_idx)%loop_num + repeated_town_idx]))
break
visited_town.append(next_town) | if __name__=="__main__":
N, K = list(map(int, input().split(' ')))
_town_list = input().split(' ')
town_list = [int(n) for n in _town_list]
next_town = town_list[0]
visited_town = set()
visited_town.add(1)
visited_town.add(next_town)
lst_visited_town = [1]
lst_visited_town.append(next_town)
counter = 1
while True:
counter += 1
next_town = town_list[next_town-1]
if counter == K:
print(next_town)
break
if next_town in visited_town:
repeated_town_idx = lst_visited_town.index(next_town)
loop_num = counter - repeated_town_idx
print((lst_visited_town[(K-repeated_town_idx)%loop_num + repeated_town_idx]))
break
lst_visited_town.append(next_town)
visited_town.add(next_town)
| p02684 |
import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N,K = LI()
A = [0] + LI()
his = [0] * (N+1)
s = 1
cnt = 1
his[1] = 1
while his[A[s]] == 0:
s = A[s]
cnt += 1
his[s] = cnt
if K >= cnt:
s = A[s]
cnt -= his[s] - 1
k = (K - his[s] + 1) % cnt
else:
k = K
s = 1
for i in range(k):
s = A[s]
print(s)
if __name__ == '__main__':
main() | import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N,K = LI()
A = [0] + LI()
his = [0] * (N+1)
s = 1
cnt = K
while cnt > 0:
his[s] = cnt
s = A[s]
cnt -= 1
if his[s] > cnt:
cnt = cnt % (his[s]-cnt)
print(s)
if __name__ == '__main__':
main() | p02684 |
n,k=list(map(int,input().split()))
loop_=[1]
lst=list(map(int,input().split()))
for i in range(k):
num=lst[loop_[-1]-1]
if num in loop_:
tar=num
break
else:
loop_.append(num)
if i==k-1:
print((loop_[k]))
exit()
loop__=loop_[loop_.index(tar):]
if k<=len(loop_):
print((loop_[k]))
else:
num=k+1-len(loop_)
print((loop__[num%len(loop__)-1])) | n,k=list(map(int,input().split()))
loop_=[1]
l_set=set()
lst=list(map(int,input().split()))
tar=None
for i in range(k):
num=lst[loop_[-1]-1]
if num in l_set:
tar=num
break
else:
loop_.append(num)
l_set.add(num)
if tar==None:
print((loop_[k]))
exit()
loop__=loop_[loop_.index(tar):]
num=k+1-len(loop_)
try:
print((loop__[num%len(loop__)-1]))
except:
print((1)) | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
i = 0
his = []
if K >= N:
for j in range(N):
i = A[i]-1
his.append(i)
K -= 1
for k in range(1, j):
if his[j] == his[j-k]:
K = K % k
break
while K > 0:
i = A[i]-1
K -= 1
print((i+1)) | N, K = list(map(int, input().split()))
A = [int(x) for x in input().split()]
when = [-1 for _ in range(N+1)]
new = 1
i = 0
while when[new] == -1:
when[new] = i
new = A[new - 1]
i += 1
line = when[new]
roop = i - when[new]
if K > line:
K = (K - line) % roop
for j in range(N+1):
when[j] -= line
for j in range(N+1):
if when[j] == K:
print(j)
break | p02684 |
N, K = list(map(int, input().split()))
A = [int(i) for i in input().split()]
visit_time = [None] * N
now = 0
visit_time[now] = 0
loop_start_city = None
loop_last_city = None
loop_first = None
loop_second = None
for i in range(1, 2 * 10 ** 5 + 1):
last = now
now = A[now]-1
if visit_time[now] is not None:
loop_start_city = now
loop_last_city = last
loop_first = visit_time[now]
loop_second = i
break
else:
visit_time[now] = i
if loop_start_city is not None and K > loop_first:
K += 1
K -= loop_first
K %= (loop_second - loop_first)
now = last
else:
now = 0
for i in range(K):
now = A[now]-1
print((now+1))
| N, K = list(map(int, input().split()))
A = [int(a)-1 for a in input().split()]
order = [-1] * N
s = []
now = 0
c = 0
a = None
b = None
while True:
order[now] = c
s.append(now)
now = A[now]
c += 1
if order[now] >= 0:
a = c - order[now]
b = order[now]
break
if K <= b:
print((s[K] + 1))
else:
K -= b
K %= a
print((s[K + b] + 1))
| p02684 |
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
visited=[]
x=0
now=1
next_point=a[now-1]
for cnt in range(min(n,k)):
now=next_point
next_point=a[now-1]
if now in visited:
x=visited.index(now)
break
else:
visited.append(now)
new_visited=visited[x:]
k-=len(visited)-len(new_visited)
#print(visited)
#print(new_visited)
print((new_visited[k%len(new_visited)-1]))
| N,K=list(map(int,input().split()))
A=[int(a)-1 for a in input().split()]
city=0
if K<=N:
for k in range(K):
city=A[city]
print((city+1))
else:
visited=[-1]*N
for k in range(K):
if visited[city]!=-1:
roop_start=visited[city]
cycle_num=k-roop_start
break
visited[city]=k
city=A[city]
K=(K-roop_start)%cycle_num+roop_start
city=0
for k in range(K):
city=A[city]
print((city+1))
| p02684 |
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
machi = 1
toutatu = []
while not machi in set(toutatu):
toutatu.append(machi)
machi = a[machi-1]
saigo = machi
num = toutatu.index(saigo)
unchi = toutatu[num:]
cnt = len(unchi)
num = toutatu.index(saigo)
if num > k:
print((toutatu[k]))
else:
print((unchi[(k-num)%cnt])) | n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
machi = 1
toutatu = []
pypyosoi = set()
while not machi in pypyosoi:
toutatu.append(machi)
pypyosoi.add(machi)
machi = a[machi-1]
saigo = machi
num = toutatu.index(saigo)
unchi = toutatu[num:]
cnt = len(unchi)
if num > k:
print((toutatu[k]))
else:
print((unchi[(k-num)%cnt]))
#pypyが遅いのは自分のコードが悪いだけごめんねpypyちゃん | p02684 |
from collections import deque
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
roop_find_list = [0]
i = 0
count = 0
for _ in range(n):
if a[i]-1 in roop_find_list:
break
else:
roop_find_list.append(a[i]-1)
i = a[i] - 1
count += 1
b = roop_find_list.index(a[i]-1)
if k < b:
print((roop_find_list[k]+1))
else:
k-=b
e = k % (count+1-b)
print((roop_find_list[b+e]+1))
| n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
pos = 0
visit = [0] * n
move = []
loop = []
while visit[pos] != 2:
if visit[pos] == 0:
move.append(pos)
else:
loop.append(pos)
visit[pos] += 1
pos = a[pos] - 1
if len(move) > k:
print((move[k]+1))
else:
print((loop[(k-(len(move)-len(loop)))%len(loop)]+1))
| p02684 |
n,k,*a=map(int,open(0).read().split())
d=[0]+[-1]*n
s=0
for i in range(k):
s=a[s]-1
if(j:=d[s])>-1:
exec((k-j)%(i+1-j)*'s=a[s]-1;')
exit(print(s+1))
d[s]=i+1
print(s+1)
| n,k,*a=list(map(int,open(0).read().split()))
d=[2*k]*n
s=0
while k:s=a[s]-1;d[s]=k=~-k%(d[s]+1-k)
print((s+1)) | p02684 |
N, K = list(map(int, input().split()))
A = [[-1] * (N + 1) for i in range(100)]
A[0] = [0] + list(map(int, input().split()))
# nowから2**i先まで移動したとこ
def double(now, i):
global A
if A[i][now] != -1:
return A[i][now]
# nowから2**(i-1)移動して、そこからさらに2**(i-1)移動する
A[i][now] = double(double(now, i - 1), i - 1)
return A[i][now]
# nowからk移動した場所を求める
def rec(now, k):
if k == 0:
return now
# if k == 1:
# return A[now]
log2k = len(bin(k)) - 3
return rec(double(now, log2k), k - 2 ** log2k)
print((rec(1, K)))
| N, K = list(map(int, input().split()))
A = [[-1] * (N + 1) for i in range(100)]
A[0] = [0] + list(map(int, input().split()))
# nowから2**i先まで移動したとこ
def double(now, i):
global A
if A[i][now] != -1:
return A[i][now]
# nowから2**(i-1)移動して、そこからさらに2**(i-1)移動する
A[i][now] = double(double(now, i - 1), i - 1)
return A[i][now]
# nowからk移動した場所を求める
def rec(now, k):
if k == 0:
return now
log2k = len(bin(k)) - 3
return rec(double(now, log2k), k - 2 ** log2k)
print((rec(1, K)))
| p02684 |
N, K = list(map(int, input().split()))
A = [[-1] * (N + 1) for i in range(100)]
A[0] = [0] + list(map(int, input().split()))
# nowから2**i先まで移動したとこ
def double(now, i):
global A
if A[i][now] != -1:
return A[i][now]
# nowから2**(i-1)移動して、そこからさらに2**(i-1)移動する
A[i][now] = double(double(now, i - 1), i - 1)
return A[i][now]
# nowからk移動した場所を求める
def rec(now, k):
if k == 0:
return now
log2k = len(bin(k)) - 3
return rec(double(now, log2k), k - 2 ** log2k)
print((rec(1, K)))
| n, k = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
# 訪問順
B = [1]
# 訪問済み
C = [False] * (n + 1)
C[1] = True
while True:
B.append(A[B[-1]])
if C[B[-1]]:
break
C[B[-1]] = True
s = B.index(B[-1])
if k < s:
print((B[k]))
exit()
B = B[B.index(B[-1]):-1]
print((B[(k - s) % len(B)])) | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
pos = 0
visit = [0]*n
move = []
roop = []
while visit[pos] != 2:
if visit[pos] == 0:
move.append(pos)
else:
roop.append(pos)
visit[pos] += 1
pos = a[pos] - 1
if len(move) > k:
print((move[k]+1))
else:
print((roop[(k-(len(move)-len(roop))) % len(roop)]+1))
| n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
num = 1
li = [1]
flag = [True]*n
flag[0] = False
for i in range(k):
num = a[num-1]
if flag[num-1]:
li.append(num)
flag[num-1] = False
else:
break
d = li.index(num)
ans = (k-d) % (len(li)-d)+d
print((li[ans]))
| p02684 |
def main():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
# ループしているところを見つける
path = [1]
current = 0
looping = False
loop_st = -1
loop_ed = -1
for i in range(n):
_next = a[current]
if _next in path:
loop_st = path.index(_next)
loop_ed = len(path)
looping = True
break
path.append(_next)
current = _next-1
if looping:
if loop_ed - 1 >= k:
print((path[k]))
else:
len_loop = loop_ed - loop_st
z = (k - loop_st) % len_loop
print((path[loop_st + z]))
else:
print((path[k]))
main()
| def main():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
# ループしているところを見つける
path = [1]
dic = [0] * n
dic[0] = 1
current = 0
looping = False
loop_st = -1
loop_ed = -1
for i in range(n):
_next = a[current]
if dic[_next-1] == 1:
loop_st = path.index(_next)
loop_ed = len(path)
looping = True
break
dic[_next-1] = 1
# if _next in path:
# loop_st = path.index(_next)
# loop_ed = len(path)
# looping = True
# break
path.append(_next)
current = _next-1
if looping:
if loop_ed - 1 >= k:
print((path[k]))
else:
len_loop = loop_ed - loop_st
z = (k - loop_st) % len_loop
print((path[loop_st + z]))
else:
print((path[k]))
main()
| p02684 |
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
import sys
sys.setrecursionlimit(10**9)
def f(L):
if A[L[-1]-1] not in L:
L_next = L + [A[L[-1]-1]]
return f(L_next)
else:
return L
L = [1]
P = f(L)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
if k+1 <= j:
print((P[k]))
else:
print((P[j+(k-j)%l])) | import sys
sys.setrecursionlimit(10**6)
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [1]
S = {1}
def f(L, S):
last = L[-1]-1
if A[last] not in S:
L.append(A[last])
S |= {A[last]}
return f(L, S)
else:
return L
P = f(L, S)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
if k+1 <= j:
print((P[k]))
else:
print((P[j+(k-j)%l])) | p02684 |
import sys
sys.setrecursionlimit(10**6)
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [1]
S = {1}
def f(L, S):
last = L[-1]-1
if A[last] not in S:
L.append(A[last])
S |= {A[last]}
return f(L, S)
else:
return L
P = f(L, S)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
if k+1 <= j:
print((P[k]))
else:
print((P[j+(k-j)%l])) | n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [1]
S = {1}
while A[L[-1]-1] not in S:
last = L[-1]-1
L.append(A[last])
S |= {A[last]}
a = A[L[-1]-1]
j = L.index(a)
r = len(L)
l = r - j
if k+1 <= j:
print((L[k]))
else:
print((L[j+(k-j)%l]))
| p02684 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
def f(i):
if i == 1:
return A
L = [-1]*(n+1)
if i & 1:
F = f(i-1)
for j in range(n+1):
L[j] = A[F[j]]
else:
F = f(i//2)
for j in range(n+1):
L[j] = F[F[j]]
return L
F = f(k)
ans = F[1]
print(ans) | n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
D = [1]
seen = [0]*n
seen[0] = 1
now = 0
while True:
now = A[now] - 1
D.append(now+1)
if seen[now]:
break
seen[now] = 1
f = D.index(D[-1])
if k < f:
print((D[k]))
else:
cycle = len(D) - f - 1
k -= f
print((D[f + k%(cycle)])) | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = 0
c = [0]
for i in range(k):
b = a[b]-1
if b in c:
d = c.index(b)
e = i-d+1
k = k - len(c)+e
c = c[-e:]
print((c[k%e]+1))
exit(0)
else:
c.append(b)
print((b+1)) | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = 0
if k<=n:
for i in range(k):
b = a[b]-1
print((b+1))
exit(0)
c = [0]
for i in range(n):
b = a[b]-1
c.append(b)
d = c[:-1].index(c[-1])
c = c[d:-1]
k = k-d
print((c[k%len(c)]+1))
| p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
res = [0]
tmp = 0
tf = [-1 for i in range(n)]
tf[0] = 0
for i in range(n):
tmp = a[tmp] - 1
if tf[tmp] != -1:
start = tmp
break
else:
res = res + [tmp]
tf[tmp] = i+1
lenret = i+1
stridx = tf[start]
cycle = lenret - stridx
if stridx >= k:
print((res[k]+1))
exit()
else:
k = k - stridx
tmp = k % cycle
print((res[stridx+tmp]+1))
| n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
res = [0]
tmp = 0
tf = [-1 for i in range(n)]
tf[0] = 0
for i in range(n):
tmp = a[tmp] - 1
if tf[tmp] != -1:
start = tmp
break
else:
res.append(tmp)
tf[tmp] = i+1
lenret = i+1
stridx = tf[start]
cycle = lenret - stridx
if stridx >= k:
print((res[k]+1))
exit()
else:
k = k - stridx
tmp = k % cycle
print((res[stridx+tmp]+1))
| p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
dist = [-1 for i in range(n)]
dist[0] = 0
now_town = 0
flg = 0 # ループが生じているかを判定するフラグ
for i in range(k):
# print(i)
next_town = a[now_town] - 1
if dist[next_town] != -1:
flg = 1
diff = (i+1) - dist[next_town]
const = dist[next_town]
break
dist[next_town] = i + 1
now_town = next_town
# print(dist)
# print("diff", diff)
# print("const", const)
if flg == 0:
print((next_town+1))
elif flg == 1:
c = (k - const) % diff
# print("c =", c)
# print("next_town = ", next_town)
if c == 0:
print((next_town+1))
else:
now_town = next_town
# print(now_town)
for i in range(c):
next_town = a[now_town] - 1
now_town = next_town
# print(now_town)
print((now_town+1))
| n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
dist = [-1 for i in range(n)]
dist[0] = 0
now_town = 0
freq = 0
for i in range(k):
next_town = a[now_town] - 1
if dist[next_town] != -1:
freq = (i+1) - dist[next_town]
const = dist[next_town]
break
dist[next_town] = i + 1
now_town = next_town
if freq == 0: # ループが発生していない場合
print((next_town+1))
else: # ループが発生している場合
freq_ = (k - const) % freq
if freq_ == 0:
print((next_town+1))
else:
now_town = next_town
for i in range(freq_):
now_town = a[now_town] - 1
print((now_town+1))
| p02684 |
# import numpy as np
# import queue
# import heapq
def main():
N, K = list(map(int, input().split()))
As = list(map(int, input().split()))
f_loop = [1]
passed = set(f_loop)
next_idx = 1
while True:
next_town = As[next_idx-1]
if next_town in passed:
idx_head = f_loop.index(next_town)
n_loop = len(f_loop) - idx_head
break
passed.add(next_town)
f_loop.append(next_town)
next_idx = next_town
if idx_head + n_loop <= K:
target = idx_head + (K - idx_head - n_loop) % n_loop
else:
target = K
print((f_loop[target]))
if __name__ == "__main__":
main()
| def main():
_, n_teleport = list(map(int, input().split()))
towns = [0] + list(map(int, input().split()))
visited_list = []
visited_set = set()
next_town = 1
while 1:
if next_town in visited_set:
loop_start_at = visited_list.index(next_town)
loop_length = len(visited_list) - loop_start_at
break
visited_list.append(next_town)
visited_set.add(next_town)
next_town = towns[next_town]
if n_teleport < loop_start_at:
end_point = n_teleport
else:
end_point = loop_start_at + (n_teleport - loop_start_at) % loop_length
print((visited_list[end_point]))
if __name__ == "__main__":
main()
| p02684 |
n,k=list(map(int,input().split()))
m=list(map(int,input().split()))
a=[]
p=1
while p not in a:
a.append(p)
p=m[p-1]
l=a.index(p)
if k<len(a):
print((a[k]))
else:
k-=l
print((a[l+k%(len(a)-l)]))
|
n,k=list(map(int,input().split()))
m=list(map(int,input().split()))
a=[]
p=1
vis=[0]*n
while vis[p-1]==0:
a.append(p)
vis[p-1]=1
p=m[p-1]
l=a.index(p)
if k<len(a):
print((a[k]))
else:
k-=l
print((a[l+k%(len(a)-l)]))
| p02684 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(100000000)
stp = -1
def main():
n, k = list(map(int, readline().split()))
a = [int(i)-1 for i in readline().split()]
order = []
ex = set()
def dfs(node):
global stp
order.append(node)
ex.add(node)
nx = a[node]
if nx in ex:
stp = nx
return
dfs(nx)
dfs(0)
cnt = order.index(stp)
if k < cnt:
print((order[k]+1))
else:
m = len(order[cnt:])
idx = (k-cnt)%m
print((order[cnt:][idx]+1))
if __name__ == '__main__':
main()
| import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10000000)
s = 0
def main():
n, k = list(map(int, readline().split()))
a = [int(i)-1 for i in readline().split()]
done = [0 for _ in range(n)]
cycle = []
def dfs(x):
global s
if done[x] == 1:
s = x
return
nx = a[x]
done[x] = 1
cycle.append(x)
dfs(nx)
dfs(0)
start_idx = cycle.index(s)
ln = len(cycle)
if k < ln:
print((cycle[k]+1))
else:
k -= start_idx
idx = start_idx+(k%(ln-start_idx))
print((cycle[idx]+1))
if __name__ == '__main__':
main()
| p02684 |
N,K=list(map(int,input().split()))
P=list(map(int,input().split()))
A={}
for i in range(N):
A[i+1]=P[i]
f=True
U=[]
v=1
k=0
while (v not in set(U)) and (k<=K):
U.append(v)
v=A[v]
k+=1
if v not in U:
print((U[-1]))
else:
g=U.index(v)
U=U[g:]
K-=g
m=len(U)
print((U[K%m]))
| N,K=list(map(int,input().split()))
P=list(map(int,input().split()))
A={k+1:P[k] for k in range(N)}
U=list()
S=set()
x=1
k=0
while (x not in S) and (k<=K):
U.append(x)
S.add(x)
x=A[x]
k+=1
if k>K:
print((U[-1]))
else:
d=U.index(x)
V=U[d:]
print((V[(K-d)%len(V)]))
| p02684 |
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque
n,k = [int(x) for x in stdin.readline().rstrip().split()]
a = [0] + [int(x) for x in stdin.readline().rstrip().split()]
m = 0
b = [1]
c = 1
for i in range(n):
c = a[c]
if c not in b:
b.append(c)
if i == k-1:
print(c)
sys.exit()
else:
break
idx = b.index(c)
bn = len(b[idx:])
sn = len(b[:idx])
# print(b,idx,sn,bn)
kk = (k-sn) % bn
print((b[idx+kk])) | from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque
n,k = [int(x) for x in stdin.readline().rstrip().split()]
a = [0] + [int(x) for x in stdin.readline().rstrip().split()]
m = 0
b = [1]
visited = [-1 for i in range(n+1)]
c = 1
visited[1] = 1
for i in range(n):
c = a[c]
if visited[c] != 1:
b.append(c)
visited[c] = 1
if i == k-1:
print(c)
sys.exit()
else:
break
idx = b.index(c)
bn = len(b[idx:])
sn = len(b[:idx])
# print(b,idx,sn,bn)
kk = (k-sn) % bn
print((b[idx+kk])) | p02684 |
n,k =list(map(int,input().split()))
a=list(map(int,input().split()))
a = [1] + a
def next(i):
return a[i]
z = [0] * (n+1)
place = 1
T = 0
interval = 0
if k < n:
for i in range(k):
place = next(place)
print(place);exit()
place = 0
k += 1
for i in range(n):
place = next(place)
if z[place] != 0:
interval = T - z[place] + 1
break
z[1] = 1
T += 1
z[place] = T
m = (k-z[place])%interval
for i in range(m):
place = next(place)
print(place)
| import sys
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9 + 7
input = sys.stdin.readline
def inp():
return int(eval(input()))
def inpl():
return list(map(int,input().split()))
def inpm():
return list(map(int,input().split()))
n,k =list(map(int,input().split()))
a=[1] + inpl()
b=[-1] * (n+1); b[1] = 0
place = 1
turn = 0
while b[a[place]] == -1:
place = a[place]
turn += 1
b[place] = turn
r = (k - turn) % (b[place] - b[a[place]] + 1)
if k < turn:
print((b.index(k)))
else:
for _ in range(r):
place = a[place]
print(place)
| p02684 |
n, k = list(map(int, input().split()))
A = list([int(x)-1 for x in input().split()])
bit = k.bit_length()
table = [[0]*n for _ in range(bit)]
table[0] = A
for i in range(1, bit):
for j in range(n):
table[i][j] = table[i-1][table[i-1][j]]
ans = 0
for i in reversed(list(range(bit))):
if (1 << i) <= k:
k -= 1 << i
ans = table[i][ans]
print((ans+1)) | n, k = list(map(int, input().split()))
A = list([int(x)-1 for x in input().split()])
history = [-1]*n
cur = 0
for i in range(n):
if history[cur] != -1:
init_cnt = history[cur]
loop_cnt = i - history[cur]
break
history[cur] = i
cur = A[cur]
loop = k if k <= init_cnt else init_cnt+((k-init_cnt) % loop_cnt)
print((history.index(loop)+1)) | p02684 |
n,k = (int(a) for a in input().split())
A = list(map(int,input().split()))
t = [1]
x = 1
while len(t) == len(set(t)) :
t.append(A[x-1])
x = t[-1]
l = [i for i , x in enumerate(t) if x == t[-1]]
if k < l[0] :
print((t[k]))
else :
k = k-l[0]
ans = k%(l[1]-l[0])
print((t[l[0] + ans])) | n,k = (int(a) for a in input().split())
A = [-1] + list(map(int,input().split()))
t = [1]
flag =[0,1] + [0]*(n-1)
x = A[1]
while flag[x] == 0 :
t.append(x)
flag[x] = 1
x = A[x]
if k <= len(t) -1 :
print((t[k]))
else :
l = t[t.index(x):]
k = k - (len(t) - 1)
k %= len(l)
print((l[k-1]))
| p02684 |
n,k = list(map(int,input().split()))
a = [0]*n
a = [int(s) for s in input().split()]
nxt = a[0]
seen = [0]*n
seen[0] = 1
#load = []
res = 0
flag = 0
for i in range(k-1):
#print(seen)
#print(nxt)
if not(nxt in seen):
seen[i+1] = nxt
nxt = a[nxt-1]
else:
root = i - seen.index(nxt) + 1
#print(root)
#print(k-1-i)
zansa = (k-1-i)%root
res = seen[seen.index(nxt)+zansa]
flag = 1
break
if(flag == 1):
print(res)
else:
print(nxt) | n,k = list(map(int,input().split()))
a = [0]*n
a = [int(s) for s in input().split()]
nxt = a[0]
seen = [1]
load = set()
load.add(1)
res = 0
flag = 0
for i in range(k-1):
#print(seen)
#print(nxt)
if not(nxt in load):
seen.append(nxt)
load.add(nxt)
nxt = a[nxt-1]
else:
root = i - seen.index(nxt) + 1
#print(root)
#print(k-1-i)
zansa = (k-1-i)%root
res = seen[seen.index(nxt)+zansa]
flag = 1
break
if(flag == 1):
print(res)
else:
print(nxt) | p02684 |
import sys
sys.setrecursionlimit(10**9)
def dfs(num,lis,count,target):
visited[num]=count
n_city=A[num]
if visited[n_city]!=-1:
lis.append(num)
if target<=count:
print((lis[target]+1))
else:
rep=len(lis)-visited[n_city]
target-=visited[n_city]
amari=target%rep
temp=lis[visited[n_city]:]
print((temp[amari]+1))
exit()
else:
dfs(n_city,lis+[num],count+1,target)
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
A=list([x-1 for x in A])
visited=[-1]*N
dfs(0,[],0,K) | import sys
sys.setrecursionlimit(10**9)
def dfs(num,lis,count,target):
visited[num]=count
n_city=A[num]
if visited[n_city]!=-1:
lis.append(num)
if target<=count:
print((lis[target]+1))
else:
rep=len(lis)-visited[n_city]
target-=visited[n_city]
amari=target%rep
temp=lis[visited[n_city]:]
print((temp[amari]+1))
exit()
else:
lis.append(num)
dfs(n_city,lis,count+1,target)
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
A=list([x-1 for x in A])
visited=[-1]*N
dfs(0,[],0,K) | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
s = [-1] * N
i = 0
cur = 0
m = 0
while True:
if s[cur] != -1 and m != 1:
m = i - s[cur]
K = i + (K - i) % m
else:
s[cur] = i
if i == K:
print((cur+1))
exit()
i += 1
cur = A[cur] - 1
| N, K = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
s = [0] * (N+1)
p = 1
h = []
for i in range(K):
h.append(p)
s[p] = 1
p = A[p]
if s[p] != 0:
break
if i == K-1:
print(p)
exit()
else:
period = len(h) - h.index(p)
rem = (K - h.index(p)) % period
print((h[h.index(p):][rem]))
| p02684 |
b,k = list(map(int,input().split()))
a = list(map(int,input().split()))
i = 0
his = []
n = 0
his.append(0)
while(1):
#print(his)
i = a[i] - 1
if i not in his:
his.append(i)
else:
n = len(his)-his.index(i)
break
m = len(his) - n
p = 0
if k > m:
p = (k-m)%n + m
else:
p = k
i = 0
#print(n,m,p)
for j in range(p):
#print(a[i])
i = a[i] - 1
print((i+1))
| b,k = list(map(int,input().split()))
a = list(map(int,input().split()))
i = 0
check = [True]*b
his = []
n = 0
his.append(0)
while(1):
#print(his)
i = a[i] - 1
if check[i]:
check[i] = False
his.append(i)
else:
n = len(his)-his.index(i)
break
m = len(his) - n
p = 0
if k > m:
p = (k-m)%n + m
else:
p = k
i = 0
#print(n,m,p)
for j in range(p):
#print(a[i])
i = a[i] - 1
print((i+1))
| p02684 |
def D():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
next = a[0]
path = [0]
while not next in path:
k -= 1
if k==0:
print(next)
break
path.append(next)
next = a[next - 1]
if k!=0:
cyclic = path[path.index(next):]
print((cyclic[k%(len(cyclic))-1]))
D() | def D():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
next = a[0]
path = [1]
visited = [True] + [False]*(n-1)
while not visited[next-1]:
k -= 1
if k==0:
print(next)
break
path.append(next)
visited[next-1] = True
next = a[next-1]
if k!=0:
cyclic = path[path.index(next):]
print((cyclic[k%(len(cyclic))-1]))
D() | p02684 |
def resolve():
N, K = list(map(int, input().split(" ")))
A = [int(x) - 1 for x in input().split(" ")]
history = []
current = 0
if K < N:
for i in range(K):
current = A[current]
print((current + 1))
return True
else:
while True:
history.append(current)
current = A[current]
if current in history:
loop_length = len( history[history.index(current):] )
if loop_length == 0:
print((current + 1))
return True
length_to_loop = len( history[:history.index(current)] )
additional_way = (K-length_to_loop)%loop_length
print((history[length_to_loop + additional_way] + 1))
return True
if __name__ == "__main__":
resolve() | def resolve():
N, K = list(map(int, input().split(" ")))
A = [int(x) - 1 for x in input().split(" ")]
current = 0
if K < N:
for i in range(K):
current = A[current]
print((current + 1))
return True
else:
history = [False] * N
load_map = []
while True:
load_map.append(current)
current = A[current]
if history[current]:
loop_length = len( load_map[load_map.index(current):] )
if loop_length == 0:
print((current + 1))
return True
length_to_loop = len( load_map[:load_map.index(current)] )
additional_way = (K-length_to_loop)%loop_length
print((load_map[length_to_loop + additional_way] + 1))
return True
else:
history[current] = True
if __name__ == "__main__":
resolve() | p02684 |
# -*- coding: utf-8 -*-
N, K = list(map(int, input().split()))
A_list = list(map(int, input().split()))
ans_list = [1]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
now_town = A_list[before_town - 1]
ans_list.append(now_town)
before_town = now_town
if N >= K:
print((ans_list[K]))
else:
ans_list = sorted(set(ans_list), key = ans_list.index)
loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
loop_len = len(ans_list)
ans_list = ans_list[loop_start_idx:]
mod_num = (K - loop_len) % (loop_len - loop_start_idx)
print((ans_list[mod_num])) | # -*- coding: utf-8 -*-
N, K = list(map(int, input().split()))
A_list = list(map(int, input().split()))
ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
now_town = A_list[before_town - 1]
if history_list[now_town - 1] == 1:
break
ans_list.append(now_town)
before_town = now_town
if len(ans_list) > K:
print((ans_list[K]))
else:
loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
loop_len = len(ans_list)
ans_list = ans_list[loop_start_idx:]
mod_num = (K - loop_len) % (loop_len - loop_start_idx)
print((ans_list[mod_num])) | p02684 |
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.insert(0,0)
start = 1
now = start
c = 0
flag = [0]*(n+1)
while c <= k and flag[now] == 0:
#print(now)
flag[now] = 1
c+= 1
now = a[now]
#print(c, now)
if c == k:
print(now)
else:
k-=c
moto = now
now = a[now]
cc = 1
while now != moto:
now = a[now]
cc+=1
nokori = k % cc
for i in range(nokori):
now = a[now]
print(now)
| n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.insert(0,0)
start = 1
now = start
c = 0
flag = [-1]*(n+1)
while c < k and flag[now] == -1:
#print(now)
flag[now] = c
c+= 1
now = a[now]
#print(c, now)
if c == k:
print(now)
else:
k-=c
cc = c-flag[now]
nokori = k % cc
for i in range(nokori):
now = a[now]
print(now)
| p02684 |
#167
#d
n,k = list(map(int, input().split()))
ln = list(map(int, input().split()))# まちi にいるとき次に町ln[i-1]へいく
visit = [0]*n
now= 1
visit[0] =1
before =[1]
while max(visit) <= 1:
now = ln[now -1]
before.append(now)
visit[now-1] += 1
roops = before.index(now)
roop_ln = before[roops:-1]
mae = before[:roops]
if k <len(mae):
print((mae[k]))
elif len(mae) <= k :
t = k- len(mae)
print((roop_ln[t%len(roop_ln)])) | N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
roop = []
while visit[pos]!=2:
if visit[pos]==0:
move.append(pos)
else:
roop.append(pos)
visit[pos] += 1
pos = A[pos] - 1
if len(move)>K:
print((move[K]+1))
else:
print((roop[(K-(len(move)-len(roop)))%len(roop)]+1)) | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
visited = [1]
now = 1
step = 0
for i in range(k):
now = a[now-1]
step += 1
if now not in visited:
visited.append(now)
else:
ind = visited.index(now)
loop = visited[ind:]
tmp = (k-step) % len(loop)
now = loop[tmp]
break
print(now)
| n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
visited = [1]
is_visited = [0] * n
now = 1
step = 0
for i in range(k):
now = a[now-1]
step += 1
if is_visited[now-1] == 0:
is_visited[now-1] = 1
visited.append(now)
else:
ind = visited.index(now)
loop = visited[ind:]
tmp = (k-step) % len(loop)
now = loop[tmp]
break
print(now)
| p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
i2p = {0:1}
p2i = {1:0}
cur = A[0]
i = 1
while True:
i2p[i] = cur
p2i[cur] = i
if A[cur-1] in list(i2p.values()):
break
cur = A[cur-1]
i+=1
i = p2i[A[cur-1]]
num_loop = len(i2p) - i
if K < i:
print((i2p[K]))
else:
print((i2p[i+(K-i)%num_loop])) | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
i2p = {0:1}
p2i = {1:0}
cur = A[0]
is_loop = {}
for i in range(N+1):
is_loop[i] = False
i = 1
while True:
i2p[i] = cur
p2i[cur] = i
is_loop[cur] = True
if is_loop[A[cur-1]]:
break
cur = A[cur-1]
i+=1
i = p2i[A[cur-1]]
num_loop = len(i2p) - i
if K < i:
print((i2p[K]))
else:
print((i2p[i+(K-i)%num_loop])) | p02684 |
n,k = list(map(int, input().split()))
a = list(map(int,input().split()))
ans = 1
c = [1]
for j in range(1,2*n):
i = a[c[-1]-1]
if i in set(c):
g = i
break
c.append(i)
s = c.index(g)
r = len(c)-s
if k <=s:
print((c[k]))
else:
b = (k-s)%r
print((c[s+b])) | n,k = list(map(int, input().split()))
a = list(map(int,input().split()))
ans = 1
c = [1]
d = [0]*(n+5)
d[1] = 1
for j in range(1,2*n):
i = a[c[-1]-1]
if d[i] == 1:
g = i
break
# if i in set(c):
# g = i
# break
d[i] = 1
c.append(i)
s = c.index(g)
r = len(c)-s
if k <=s:
print((c[k]))
else:
b = (k-s)%r
print((c[s+b])) | p02684 |
n, k = list(map(int, input().split()))
a_li = list(map(int, input().split()))
loop_li = []
a = 1
while a not in loop_li:
loop_li.append(a)
a = a_li[a-1]
length = len(loop_li)
if k<length:
print((loop_li[k]))
else:
bp = a_li[loop_li[-1]-1] # back point
bp_idx = loop_li.index(bp)
t = length - bp_idx
amari = (k-bp_idx)%t
print((loop_li[bp_idx+amari])) | n, k = list(map(int, input().split()))
a_li = list(map(int, input().split()))
loop_li = []
loop_set = set([])
a = 1
while a not in loop_set:
loop_li.append(a)
loop_set.add(a)
a = a_li[a-1]
length = len(loop_li)
if k<length:
print((loop_li[k]))
else:
bp = a_li[loop_li[-1]-1] # back point
bp_idx = loop_li.index(bp)
t = length - bp_idx
amari = (k-bp_idx)%t
print((loop_li[bp_idx+amari])) | p02684 |
n = input().split()
N = int(n[0])
K = int(n[1])
listb = [1]
C = 1
A = list(map(int, input().split()))
for i in range(N):
C = A[C-1]
if C not in listb:
listb.append(C)
else:
D = listb.index(C)
listb.append(C)
break
L = len(listb) -1
if K <= N:
print((listb[K]))
else:
if L-D == 1 or L-D == 0:
print(C)
else:
M = (K-D) % (L-D)
print((listb[(D+M)%L])) | n = input().split()
N = int(n[0])
K = int(n[1])
listb = [1]
listc = [0]*N
C = 1
A = list(map(int, input().split()))
for i in range(N):
C = A[C-1]
if listc[C-1] == 0:
listb.append(C)
listc[C-1] = 1
else:
D = listb.index(C)
listb.append(C)
break
L = len(listb) -1
if K <= N:
print((listb[K]))
else:
if L-D == 1 or L-D == 0:
print(C)
else:
M = (K-D) % (L-D)
print((listb[(D+M)%L])) | p02684 |
N, K = list(map(int,input().split()))
A = list(map(int, input().split()))
dic = {1:0}
now = 1
flag = 0
T = 0
log = [1]
for i in range(K):
now = A[now-1]
if now in log:
flag=1
T=i+1-dic[now]
break
dic[now] = i+1
log.append(now)
if flag!=1:
print(now)
else:
print((log[(K-dic[now])%T+dic[now]])) | N, K = list(map(int,input().split()))
A = list(map(int, input().split()))
dic = {1:0}
now = 1
flag = 0
T = 0
log = [1]
for i in range(K):
now = A[now-1]
#print(i+1,now)
if now in dic:
flag=1
T=i+1-dic[now]
log = log[dic[now]:]
break
dic[now] = i+1
log.append(now)
if flag!=1:
print(now)
else:
print((log[(K-dic[now])%T])) | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
pos = -1
seen = [False] * n
hist = []
cycle = []
i = 0
while(seen[i] == False):
seen[i] = True
hist.insert(0, i)
ne = a[i]-1
i = ne
if (seen[i] == True):
pos = i
preCircle = len(hist)
cnt = 0
while(len(hist) != 0):
b = hist[0]
cycle.append(b)
cnt += 1
hist.pop(0)
if (b == pos):
break
preCircle -= cnt
cycle = list(reversed(cycle))
if (k < preCircle):
print((hist[-k-1] + 1))
else:
k -= preCircle
ans = k % len(cycle)
print((cycle[ans] + 1))
| n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
pos = -1
seen = [False] * n
last = [-1] * n
hist = []
i = 0
cnt = 0
while(seen[i] == False):
seen[i] = True
last[i] = cnt
cnt += 1
hist.append(i)
ne = a[i]-1
i = ne
if (seen[i] == True):
pos = i
cycle = hist[last[pos]:]
preCircle = last[pos]
if (k <= preCircle):
print((hist[k] + 1))
else:
k -= preCircle
ans = k % len(cycle)
print((cycle[ans] + 1))
| p02684 |
# -*- coding: utf-8 -*-
# 入力を整数に変換して受け取る
def input_int():
return int(eval(input()))
# マイナス1した値を返却
def int1(x):
return int(x) - 1
# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
return list(map(int, input().split()))
# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
return tuple(map(int, input().split()))
# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
return tuple(map(int1, input().split()))
def main():
n, k = input_to_int_map()
a = {i + 1: int(val) for i, val in enumerate(input().split())}
place = 1
place_goneds = [place]
i = 0
loop_start_index = 0
while i < n:
place = a[place]
goned = False
for j, place_goned in enumerate(place_goneds):
if place_goned == place:
loop_start_index = j
goned = True
break
if goned:
break
place_goneds.append(place)
i += 1
if k < len(place_goneds):
return place_goneds[k]
move_cnt = k - loop_start_index
loop_list = place_goneds[loop_start_index:]
return loop_list[move_cnt % len(loop_list)]
if __name__ == "__main__":
print((main()))
| # -*- coding: utf-8 -*-
# 入力を整数に変換して受け取る
def input_int():
return int(eval(input()))
# マイナス1した値を返却
def int1(x):
return int(x) - 1
# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
return list(map(int, input().split()))
# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
return tuple(map(int, input().split()))
# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
return tuple(map(int1, input().split()))
def main():
n, k = input_to_int_map()
a = {i + 1: int(val) for i, val in enumerate(input().split())}
place = 1
visit = {place}
place_visits = [place]
for i in range(k):
place = a[place]
if place in visit:
left = place_visits.index(place)
return place_visits[left + (k - left) % (i + 1 - left)]
else:
visit.add(place)
place_visits.append(place)
return place
if __name__ == "__main__":
print((main()))
| p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
dic = {}
list_town = []
town = 1
for i, v in enumerate(A):
dic[i+1] = v
for _ in range(K):
list_town.append(town)
town = dic[town]
if town in list_town:
stop_twon = town
break
if N <= K:
list_first_split = list_town[:list_town.index(stop_twon)]
list_second_split = list_town[list_town.index(stop_twon):]
print((list_second_split[(K - (len(list_first_split))) % len(list_second_split)]))
else:
print(town) | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
dic = {}
list_town = []
set_town = set()
town = 1
for i, v in enumerate(A):
dic[i+1] = v
for _ in range(K):
list_town.append(town)
set_town.add(town)
town = dic[town]
if town in set_town:
stop_twon = town
break
if N <= K:
list_first_split = list_town[:list_town.index(stop_twon)]
list_second_split = list_town[list_town.index(stop_twon):]
print((list_second_split[(K - (len(list_first_split))) % len(list_second_split)]))
else:
print(town) | p02684 |
def main():
n,k,*a = list(map(int,open(0).read().split()))
c = [0]
visit = [False]*n
visit[0] = True
for i in range(n):
nt = a[c[-1]]-1
if visit[nt]:
ws = c.index(nt)
wst = c[:ws]
wrt = c[ws:]
wr = len(wrt)
else:
c.append(nt)
visit[nt] = True
if ws > k:
ans = wst[k]
else:
ans = wrt[(k-ws)%wr]
print((ans+1))
main() | def main():
n,k,*a = list(map(int,open(0).read().split()))
c = [0]
visit = [False]*n
visit[0] = True
for i in range(n):
nt = a[c[-1]]-1
if visit[nt]:
ws = c.index(nt)
wst = c[:ws]
wrt = c[ws:]
wr = len(wrt)
break
else:
c.append(nt)
visit[nt] = True
if ws > k:
ans = wst[k]
else:
ans = wrt[(k-ws)%wr]
print((ans+1))
main() | p02684 |
N,K = list(map(int,input().split()))
A=list(map(int,input().split()))
P=[1]
now=1
for i in range(K):
now=A[now-1]
if now in P:
x = i+1
y = P.index(now)
break
elif i == K-1:
print(now)
exit()
else:
P.append(now)
c=(K-y)%(x-y)
print((P[y+c])) |
N,K = list(map(int,input().split()))
A=list(map(int,input().split()))
P=[1]
S=[0]*N
S[0]=1
now=1
for i in range(K):
now=A[now-1]
if S[now-1]==1:
x = i+1
y = P.index(now)
break
elif i == K-1:
print(now)
exit()
else:
P.append(now)
S[now-1]=1
c=(K-y)%(x-y)
print((P[y+c])) | p02684 |
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
import math
#from math import gcd
#inf = 10**17
#mod = 10**9 + 7
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
db = [a]
cnt = k.bit_length()
for c in range(1, cnt):
temp = []
for i in range(n):
temp.append(db[c-1][db[c-1][i]-1])
db.append(temp)
res = 1
for i in range(cnt):
if (k>>i) & 1:
res = db[i][res-1]
print(res)
if __name__ == '__main__':
main() | def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
import math
#from math import gcd
#inf = 10**17
#mod = 10**9 + 7
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
db = [a]
cnt = k.bit_length()
for c in range(1, cnt):
temp = [0]*n
for i in range(n):
temp[i] = db[c-1][db[c-1][i]-1]
db.append(temp)
res = 1
for i in range(cnt):
if (k>>i) & 1:
res = db[i][res-1]
print(res)
if __name__ == '__main__':
main() | p02684 |
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
import math
#from math import gcd
#inf = 10**17
#mod = 10**9 + 7
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
db = [a]
cnt = k.bit_length()
for c in range(1, cnt):
temp = [0]*n
for i in range(n):
temp[i] = db[c-1][db[c-1][i]-1]
db.append(temp)
res = 1
for i in range(cnt):
if (k>>i) & 1:
res = db[i][res-1]
print(res)
if __name__ == '__main__':
main() | def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
import math
#from math import gcd
#inf = 10**17
#mod = 10**9 + 7
n,k = list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
# visited[i]:町iにvisited[i]回目の遷移で到達
visited = [0]*(n+1)
step = 0
i = 1
while k:
i = a[i]
step += 1
k -= 1
if visited[i] != 0:
k = k % (step - visited[i])
for _ in range(k):
i = a[i]
print(i)
break
visited[i] = step
else:
print(i)
if __name__ == '__main__':
main() | p02684 |
from sys import stdin
import math
n,k = list(map(int,stdin.readline().split()))
a = list(map(int,stdin.readline().split()))
d = math.floor(math.log2(k))
next = [[-1 for _ in range(n)] for _ in range(d+1)]
for i in range(n):
next[0][i] = a[i] - 1
for j in range(d):
for i in range(n):
if next[j][i] == -1:
continue
next[j+1][i] = next[j][next[j][i]]
cur = 0
i = 0
while k > 0:
if k&1:
cur = next[i][cur]
k >>= 1
i += 1
print((cur+1))
| from sys import stdin
n,k = list(map(int,stdin.readline().split()))
a = list(map(int,stdin.readline().split()))
for i in range(n):
a[i] -= 1
d = {}
d[0] = 0
cur = 0
step = 0
while step < k:
cur = a[cur]
step += 1
if cur in d:
looplen = step - d[cur]
step += looplen * ((k-step)//looplen)
else:
d[cur] = step
print((cur + 1))
| p02684 |
from sys import stdin
n,k = list(map(int,stdin.readline().split()))
a = list(map(int,stdin.readline().split()))
for i in range(n):
a[i] -= 1
d = {}
d[0] = 0
cur = 0
step = 0
while step < k:
cur = a[cur]
step += 1
if cur in d:
looplen = step - d[cur]
step += looplen * ((k-step)//looplen)
else:
d[cur] = step
print((cur + 1))
| from sys import stdin
n,k = list(map(int,stdin.readline().split()))
a = list(map(int,stdin.readline().split()))
for i in range(n):
a[i] -= 1
d = {}
d[0] = k
cur = 0
while k > 0:
cur = a[cur]
k -= 1
if cur in d:
looplen = d[cur] - k
k %= looplen
else:
d[cur] = k
print((cur + 1))
| p02684 |
def resolve():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
roop=0
iti=0
rirekin=0
rireki=[]
while roop==0:
iti=a[iti]-1
if iti in rireki:
mae=rireki.index(iti)
Mae=rireki[:mae]
del rireki[:mae]
roop=rirekin-mae
break
rireki.append(iti)
rirekin+=1
if k <= mae:
print((Mae[k-1]+1))
else:
k-=mae
print((rireki[(k%roop)-1]+1))
resolve() | def resolve():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
check=[-1]*n
iti=0
rireki=[]
while True:
iti=a[iti]-1
if check[iti]==0:
mae=rireki.index(iti)
Mae=rireki[:mae]
Loop=rireki[mae:]
roop=len(Loop)
break
rireki.append(iti)
check[iti]=0
if k <= mae:
print((Mae[k-1]+1))
else:
k-=mae
print((Loop[(k%roop)-1]+1))
resolve() | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
X = 0
town = 1
counted = [0 for _ in range(N)]
for k in range(N):
X += 1
counted[k] = town
town = A[town-1]
if town in counted:
ini = counted.index(town)
Q = X - ini
break
if K >= ini:
num = (K-ini) % Q
print((counted[ini+num]))
else:
print((counted[K]))
| N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
X = 0
town = 1
counted = [0 for _ in range(N)]
List = [0 for _ in range(N)]
for k in range(N):
X += 1
counted[k] = town
town = A[town-1]
if List[town-1] == 1:
ini = counted.index(town)
Q = X - ini
break
else:
List[town-1] = 1
if K >= ini:
num = (K-ini) % Q
print((counted[ini+num]))
else:
print((counted[K])) | p02684 |
import sys
n, k = list(map(int, input().split()))
ara = [0]
ara += list(map(int, input().split()))
now = 1
vis = []
taken = [0]*(n+1)
while taken[now] == 0:
vis.append(now)
taken[now] = 1
now = ara[now]
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)])) | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# mp = dict()
# for ix in range(N):
# mp[ix+1] = A[ix]
hist = []
exists = [0] * (N+1)
cur = 1
cnt = 0
# ここがおかしい?
# while True:
# cur = mp[cur]
# cnt += 1
# # K -= 1
# if exists[cur] != 0:
# break
# else:
# exists[cur] = 1
# hist.append(cur)
A = [0] + A
# print(A)
while exists[cur] == 0:
hist.append(cur)
exists[cur] = 1
cur = A[cur]
cycle_sp = cur
# cycle_l = 0
cycle_sp_ix = hist.index(cycle_sp)
import sys
if K < cycle_sp_ix:
print((hist[K]))
sys.exit()
cycle_l = len(hist) - cycle_sp_ix
# while True:
# cur = mp[cur]
# cycle_l += 1
# if cur == cycle_sp:
# break
rest = (K-cycle_sp_ix) % cycle_l
# print("cnt:", cnt)
# print("K:", K)
# print("cycle_l:", cycle_l)
# print("rest:", rest)
print((hist[rest + cycle_sp_ix]))
# while rest != 0:
# cur = mp[cur]
# rest -= 1
# print(cur)
"""
# hist = [1]
hist = {1: 0}
while True:
hk = hist.keys()[-1]
if not mp[hk] in hist.keys():
# hist.append(mp[hist[-1]])
hist[mp[hk]] = 0
else:
# found
cstart = hist.index(mp[hk])
cend = len(hist)
cycle_l = cend - cstart
# print("hist:", hist)
# print("cstart:", cstart)
# print("cend:", cend)
# print("cycle_l:", cycle_l)
ans_i = (K - cstart) % cycle_l
# print("ans_i:", ans_i)
ans = hist[cstart + ans_i]
print(ans)
break
""" | p02684 |
from collections import defaultdict
def main():
n, k = list(map(int, input().split(" ")))
a = list([int(i)-1 for i in input().split(" ")])
s = [0]
d = defaultdict(lambda:0)
x = a[0]
for i in range(n):
s.append(x)
x = a[x]
bb=None
for i in range(n):
d[s[i]] += 1
if d[s[i]] ==2:
bb=s[i]
break
cc = s.index(bb)
s[cc]=-1
dd = s.index(bb)
loop_len = dd-cc
s[cc]=s[dd]
if bb ==None or k < cc:
print((s[k]+1))
else:
y = (k-cc) % loop_len
print((s[y+cc]+1))
if __name__ == "__main__":
main() | from collections import defaultdict
def main():
n, k = list(map(int, input().split(" ")))
a = list(map(int, input().split(" ")))
s = []
ord = [-1]*(n+1)
u = 1
while ord[u] == -1:
ord[u] = len(s)
s.append(u)
u = a[u-1]
l = len(s) - ord[u]
if k < ord[u]:
print((s[k]))
else:
print((s[(k-ord[u])%l+ord[u]]))
'''
def main():
n, k = map(int, input().split(" "))
a = list(map(lambda i: int(i)-1, input().split(" ")))
s = [0]
d = defaultdict(lambda:0)
x = a[0]
for i in range(n):
s.append(x)
x = a[x]
bb=None
for i in range(n):
d[s[i]] += 1
if d[s[i]] ==2:
bb=s[i]
break
cc = s.index(bb)
s[cc]=-1
dd = s.index(bb)
loop_len = dd-cc
s[cc]=s[dd]
if bb ==None or k < cc:
print(s[k]+1)
else:
y = (k-cc) % loop_len
print(s[y+cc]+1)
'''
if __name__ == "__main__":
main() | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
idx = A[0]
move = []
move.append(idx)
cycle = []
for _ in range(K-1):
idx = A[idx-1]
if idx in move:
cycle = move[move.index(idx):]
break
move.append(idx)
if cycle:
print((cycle[(K - 1- (len(move)-len(cycle)))%len(cycle)]))
else:
print((move[K-1])) | N, K = list(map(int, input().split()))
A = [int(i) for i in input().split()]
R = [False]*(N+1)
D = []
p = 1
while True:
if R[p]:
break
D.append(p)
R[p] = True
p = A[p-1]
m = D.index(p)
c = len(D)-m
if K <= m:
print((D[K]))
else:
print((D[m+(K-m) % c]))
| p02684 |
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline
n,k,*a = list(map(int,read().split()))
for i in range(n): a[i] -= 1
def pow2(a,n):
if n==0: return list(range(n))
if n==1: return a
if n%2:
return mul(a,pow2(a,n-1))
else:
x = pow2(a,n//2)
return mul(x,x)
def mul(a,b):
return [a[bi] for bi in b]
x = pow2(a,k)
print((x[0]+1)) | # coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline
n,k,*a = list(map(int,read().split()))
for i in range(n): a[i] -= 1
na = [0]*n
v = 0
while k:
if k&1: v = a[v]
for j, aj in enumerate(a):
na[j] = a[aj]
a,na = na,a
i += 1
k >>= 1
print((v+1)) | p02684 |
from collections import Counter
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k <= 10 ** 6:
i = A[i] - 1
B.append(i + 1)
k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None
while k < 10 ** 6:
if B[k] == idx and cnt == 1:
k_s = k
break
elif B[k] == idx:
k_f = k
cnt += 1
k += 1
if k_s and k_s < K:
k_max = (K - B.index(idx)) % (k_s - k_f) + B.index(idx)
else:
k_max = K
i = k = 0
while k < k_max:
i = A[i] - 1
k += 1
print((i + 1)) | from collections import Counter
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
i = k = 0
B = [1]
while k <= N:
i = A[i] - 1
B.append(i + 1)
k += 1
S = Counter(B)
idx = S.most_common(1)[0][0]
i = k = cnt = 0
k_s = None
while k < N:
if B[k] == idx and cnt == 1:
k_s = k
break
elif B[k] == idx:
k_f = k
cnt += 1
k += 1
if k_s and k_s < K:
k_max = (K - B.index(idx)) % (k_s - k_f) + B.index(idx)
else:
k_max = K
i = k = 0
while k < k_max:
i = A[i] - 1
k += 1
print((i + 1)) | p02684 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.