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 += ... | 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 ... | 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
ne... | 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
ne... | 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))
... | 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)... | 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)... | 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]
el... | 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
... | 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:
pri... | 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... | 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_li... | 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]... | 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)
#... | #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(... | 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
... | 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
... | 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... | 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
... | 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_nu... | 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 =... | 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)%(le... | 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)
an... | 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... | 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[... | 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)... | 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((... | 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
... | 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(... | 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
... | 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) % ... | 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 = ... | 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... | 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)
... | 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(... | 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)... | 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:
... | 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:
... | 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
... | 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(plac... | 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[... | 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)
... | 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(cur... | 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)
... | 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):
... | 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):
... | 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]
pri... | 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... | 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]
... | 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)
... | 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:
brea... | 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([])
... | 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 =... | 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... | 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((... | (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.a... | 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]
... | 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
b... | 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[n... | 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[n... | 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
pri... | 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... | #!/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) # 何回目に訪れたか... | 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 - cn... | 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((... | 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] = ... | 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]))
... | 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))... | 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... | 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_cou... | 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_cou... | 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 >= lin... | 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:
kmo... | 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_t... | 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... | 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]... | 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
whil... | 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(t... | 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()... | 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... | 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):
... | 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... | 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
i... | 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:... | 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
... | 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((touta... | 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:
pr... | 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_... | 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))
... | 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, ... | 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, ... | 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, ... | 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.in... | 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)... | 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)... | 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... | 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... | 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 - ... | 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 ... | 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 ... | 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)
f... | 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
... | 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+... | 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
... | 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_... | 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
... | 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:
... | 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_leng... | 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... | 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 = []
... | 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
... | 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)%le... | 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):
... | 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)... | 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)
... | 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()
... | 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 <... | 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 els... | 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)
... | 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:
ro... | 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... | 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-=vis... | 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-=vis... | 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
cu... | 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.... | 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 +... | 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) ... | 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:
cycl... | 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)
visit... | 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... | 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... | 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_li... | # -*- 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:
bre... | 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
... | 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... | 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 = befor... | 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:
... | 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-s... | 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.ind... | 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:
prin... | 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]
... | 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 = l... | 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)
... | 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] # bac... | 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 = 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)
... | 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]... | 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:
... | 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
whi... | 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):
po... | 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():
re... | # -*- 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():
re... | 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:
... | 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 ... | 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... | 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]... | 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... |
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)
... | 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... | 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... | 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... | 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... | 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]... | 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-st... | 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-st... | 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... | 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]
... | 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:]
r... | 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
... | 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... | 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]))
... | 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:
# brea... | 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... | 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]:... | 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(... | 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... | # 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
... | 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 cn... | 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:
... | p02684 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.