input stringlengths 20 127k | target stringlengths 20 119k | problem_id stringlengths 6 6 |
|---|---|---|
from collections import defaultdict
def inpl():
return list(map(int, input().split()))
N, K = inpl()
aa = inpl()
s = 1
route = [s]
ed = set([s])
for i in range(1, K + 1):
s = aa[s - 1]
if s in ed:
Li = route.index(s)
Ri = len(route)
idx = Li + (K - Li) % (R... | N, K = list(map(int, input().split()))
al = list(map(int, input().split()))
visited= [1]
ed = set([1])
for i in range(K):
next_town = al[visited[i] - 1]
if next_town in ed:
loop_sta = visited.index((next_town)) # visited_townsにおけるループ開始のインデックス
loop_end = len(visited) #visited_townsにおける... | p02684 |
from sys import stdin
def main():
n, k = list(map(int, stdin.readline().rstrip().split()))
a = [int(x)-1 for x in stdin.readline().rstrip().split()]
n = a[0]
his = [-1, n]
f = his.append
while True:
n = a[n]
if n in his:
f(n)
break
f(... | from sys import stdin
def main():
n, k = list(map(int, stdin.readline().rstrip().split()))
a = [int(x)-1 for x in stdin.readline().rstrip().split()]
n = a[0]
his = [-1, n]
f = his.append
s = set()
while True:
n = a[n]
if n in s:
f(n)
brea... | p02684 |
N,K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
where_li = []
pre = 0
for i in range(N):
where_li.append(A[pre])
pre = A[pre]-1
aa = sorted([x for x in set(where_li) if where_li.count(x) > 1], key=where_li.index)
if(len(aa)==0):
indexes = [0,0]
else:
indexes = [i ... | N,K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
where_li = []
pre = 0
for i in range(N):
where_li.append(A[pre])
pre = A[pre]-1
len_w = len(where_li)
a = where_li.index(A[pre])
mod_s = len_w-a
if K<=len_w:
print((where_li[K-1]))
else:
if (K-len_w)%mod_s==0:
d... | p02684 |
import sys
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
alr = []
now = 1
i = 0
while not now in alr:
alr.append(now)
now = A[now-1]
i += 1
if i == K:
print(now)
sys.exit()
place = alr.index(now)
circle = len(alr) - place
rem = (K - place... | import sys
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
alr = {}
now = 1
i = 0
while not now in list(alr.keys()):
alr[now] = i
now = A[now-1]
i += 1
if i == K:
print(now)
sys.exit()
place = alr[now]
circle = len(alr) - place
rem = (K - p... | p02684 |
a = input().split(" ")
N = eval(a[0])
K = eval(a[1])
dic = {}
b = input().split(" ")
for i in range(1, N + 1):
dic[i] = eval(b[i - 1])
ans = 1
lst = []
length = 0
while K > 0:
if ans in lst:
left = lst.index(ans)
right = length
zq = right - left
K = K % zq
... | a = input().split(" ")
N = eval(a[0])
K = eval(a[1])
dic = {}
b = input().split(" ")
n_lst = [-1]
for i in range(1, N + 1):
dic[i] = eval(b[i - 1])
n_lst.append(-1)
ans = 1
lst = []
length = 0
while K > 0:
if n_lst[ans] != -1:
left = n_lst[ans]
zq = length - left
K = ... | p02684 |
def main():
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
visited = [0]
now = 0
ind = 0
my_append = visited.append
for _ in range(K):
a = A[now]-1
if a in visited:
ind = v... | def main():
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# N = 2*(10**4)
# K = 10**18
# A = [i+2 for i in range(N)]
# A[-1] = 1
# print(A)
visited = [0]
visited_set = set([0])
now = 0
... | p02684 |
from collections import deque
import sys
def main():
input = sys.stdin.readline
n,k = list(map(int,input().split()))
a = [int(x) for x in input().split()]
#ループを探す?
checked_list = [1]
que = deque([a[0]])
counter = 0
q_append = que.append
c_append = checked_list.ap... | from collections import deque
import sys
def main():
input = sys.stdin.readline
n,k = list(map(int,input().split()))
a = [int(x) for x in input().split()]
#ループを探す?
checked_dict = {1:0}
que = deque([a[0]])
counter = 0
while True:
counter += 1
x = ... | p02684 |
n,k = list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
r = [1]
k += 1
now = 1
for i in range(n):
now = a[now]
if now in r: break
r.append(now)
if len(r) == k:
print(now)
exit()
I = r.index(now)
if I != 0:
k -= I
r = r[I:]
print((r[-1] if k%len(r)==0 el... | n,k = list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
now = 1
check = [-1]*(n+1)
for i in range(k):
if check[now] == -1:
check[now] = i
now = a[now]
else:
loop = i - check[now]
afterloop = (k-(i-loop))%loop
print((check.index((i-loop) + afterloop)))
... | p02684 |
import sys
n,k = list(map(int,input().split()))
ls = list(map(int,input().split()))
def sub(k,s):
return k-s
p = [1]
i = 0
a = 0
t = 0
ans1 = 0
ans2 = 0
e = 3
while i<k-1:
if i ==0:
a = ls[i]
p.append(a)
if len(p) != len(list(set(p))):
q = [p[-1]]*(len(p))
... | n,k = list(map(int,input().split()))
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
if fi[ls[a]] == -1:
fi[ls[a]] = i + 1
aa = a
a = ls[a] - 1
else:
ind = fi[ls[a]]
t = (k - ind) % (max(fi) + 1 - ind)
t ... | p02684 |
n, k = [int(x) for x in input().strip().split()]
a = [int(x) for x in input().strip().split()]
assert len(a) == n
a = [0] + a
current = 1
way = [1]
for i in range(k):
current = a[current]
if current in way:
li = way.index(current)
loop = way[li:]
print((loop[(k - li) % len(loop)]))
break... | n, k = [int(x) for x in input().split()]
a = [int(x)-1 for x in input().split()]
assert len(a) == n
current = 0
visited = [-1] * n
visited[0] = 0
for i in range(1, k):
current = a[current]
if visited[current] == -1:
visited[current] = i
else:
pre = visited[current]
count = (k - pre) % (i -... | p02684 |
N,K = list(map(int,input().split()))
A = [0] + [int(i) for i in input().split()]
route = [1]
start = 1
cnt = 0
while True:
if cnt == K:
print((route[-1]))
exit()
elif A[start] in route:
index = route.index(A[start])
loop = len(route) - index
break
else:
route.append(A[start... | N,K = list(map(int,input().split()))
A = [0] + [int(i) for i in input().split()]
route = [1]
route_set = set()
start = 1
cnt = 0
while True:
if cnt == K:
print((route[-1]))
exit()
elif A[start] in route_set:
index = route.index(A[start])
loop = len(route) - index
break
else:
... | p02684 |
N, K = list(map(int, input().split()))
A = input().split()
A = [int(a) for a in A]
S = []
a = 1
number = 0
for _ in range(N + 1):
b = A[a - 1]
if b in S:
c = S.index(b)
break
number += 1
S.append(b)
a = b
T = S[c:number]
if K <= N:
print((S[K - 1]))
else:
z ... | N, K = list(map(int, input().split()))
A = input().split()
A = [int(a) for a in A]
S = [1]
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
b = A[a - 1]
if numbers[b-1] != -1:
c=numbers[b-1]
break
numbers[b-1]=number
number += 1
S.append(b)
a = b
... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
g = dict()
for i, v in enumerate(a):
g[i+1] = v
def find_num(target):
path = 1
target_ = g[target]
while target_ != target:
target_ = g[target_]
path += 1
return path
target = 1
visited = []... | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
g = dict()
for i, v in enumerate(a):
g[i+1] = v
def find_num(target):
path = 1
target_ = g[target]
while target_ != target:
target_ = g[target_]
path += 1
return path
target = 1
visited = di... | p02684 |
n, k = list(map(int, input().split()))
tele = list(map(int, input().split()))
loc = 1
act = [[0]*n for i in range(n)]
count = 1
log = []
log.append(loc)
while True:
newloc=tele[loc-1]
log.append(newloc)
#print(newloc)
if act[loc-1][newloc-1] == 0:
act[loc-1][newloc-1] =count
act[newloc-1][l... | n, k = list(map(int, input().split()))
tele = list(map(int, input().split()))
loc = 1
act = [-1]*n
act[0]=0
count = 1
log = []
log.append(loc)
flg = True
while True:
newloc=tele[loc-1]
if count ==k:
print(newloc)
flg = False
log.append(newloc)
#print(newloc)
if act[newloc-1] == -1:
... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
l = [1]
for i in range(n):
if a[l[i] - 1] in l:
res = l[:l.index(a[l[i] - 1])]
cyc = l[l.index(a[l[i] - 1]):]
break
else:
l += [a[l[i] - 1]]
print((cyc[(k - len(res)) % len(cyc)] if k > len(res) ... | n, k = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
visited = [-1 for _ in range(n)]
visited[0] = 0
l = [0]
for i in range(k):
nex = a[l[i]]
if visited[nex] != -1:
res = visited[nex]
cyc = i + 1 - visited[nex]
break
else:
l += [n... | p02684 |
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deep... | import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deep... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
for i in range(1, n + 1):
d[i] = a[i - 1]
did = set()
route = [1]
now = 1
for i in range(k):
now = d[now]
route.append(now)
if now in did:
break
did.add(now)
if k < 10 ** 6:#計算量的に間に合うので愚直に... | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
for i in range(1, n + 1):
d[i] = a[i - 1]
did = set([1])
route = [1]
now = 1
for i in range(k):
now = d[now]
route.append(now)
if now in did:
break
did.add(now)
if k < 10 ** 6:#計算量的に間に合うので... | p02684 |
import math
def fact(n):
ans = 1
for i in range(2, n+1):
ans*= i
return ans
def comb(n, c):
return fact(n)//(fact(n-c)*c)
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
cnt= 0
i = 1
cycle = ['1']
for c in range(n):
i = a[i-1]
if(i in cycle)... | import math
def fact(n):
ans = 1
for i in range(2, n+1):
ans*= i
return ans
def comb(n, c):
return fact(n)//(fact(n-c)*c)
'''
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt= 0
i = 1
cycle = ['1']
for c in range(n):
i = a[i-1]
if(i in cycle):
... | p02684 |
n,k= list(map(int, input().split()))
li= list(map(int, input().split()))
c=[1]
nex=1
first=0
flag=0
for i in range(k):
nex=li[nex-1]
if nex in c:
first=c.index(nex)
flag=1
break
c.append(nex)
if flag:
k-=first
k%=len(c)-first
print((c[first+k]))
else:
... | n,k= list(map(int, input().split()))
li= list(map(int, input().split()))
d = [0 for i in range(n)]
c=[1]
nex=1
first=0
flag=0
for i in range(k):
nex=li[nex-1]
if d[nex-1]:
first=c.index(nex)
flag=1
break
d[nex-1]=1
c.append(nex)
if flag:
k-=first
k%=len(c... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a = [0] + a
c = [1]
while c.count(c[-1]) == 1:
c.append(a[c[-1]])
t = c.index(c[-1])
if k <= t:
ans = c[k]
else:
ans = c[t + ((k-t) % (len(c)-1-t))]
print(ans) | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a = [0] + a
c = [1]
x = True
y = [1, 1] + [0]*(n-1)
while x:
c.append(a[c[-1]])
y[c[-1]] += 1
if y[c[-1]] == 2:
x = False
t = c.index(c[-1])
if k <= t:
ans = c[k]
else:
ans = c[t + ((k-t) % (len(c)-... | p02684 |
from collections import deque
N, K= list(map(int, input().split()))
A = list(map(int, input().split()))
x = deque([1])
point = 1
count = 0
while True:
next_point = A[point-1]
if next_point in x :
x.append(next_point)
break
x.append(next_point)
point = next_point
f = x.index(next_poi... | N, K= list(map(int, input().split()))
A = list(map(int, input().split()))
x = {1:0}
point = 1
count = 0
while True:
next_point = A[point-1]
count +=1
if next_point in x :
flag=next_point
break
x[next_point] =count
point = next_point
#print(count, x[flag])
c = count - x[flag]
f = x[f... | p02684 |
N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
test = 1
find_f = {}
for i in range(N):
find_f[i+1]=[]
find_f[1].append(0)
for i in range(K):
test = move_list[test-1]
find_f[test].append(i+1)
if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=1000... | N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
test = 1
find_f = {}
for i in range(N):
find_f[i+1]=[]
find_f[1].append(0)
for i in range(K):
test = move_list[test-1]
find_f[test].append(i+1)
if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=1000... | p02684 |
n,k=[int(x) for x in input().split()]
p=[(int(x)-1) for x in input().split()]
a=[0]
i=0
while p[i] not in a:
a.append(p[i])
i=p[i]
l=a.index(p[i])
if k<=l: print((1+a[k]))
else: print((1+a[l+(k-l)%(len(a)-l)])) | n,k=[int(x) for x in input().split()]
p=[(int(x)-1) for x in input().split()]
alp=[0 for _ in range(n)]
a=[0]
i=0
alp[0]=1
while alp[p[i]]==0:
a.append(p[i])
alp[p[i]]=1
i=p[i]
l=a.index(p[i])
if k<=l: print((1+a[k]))
else: print((1+a[l+(k-l)%(len(a)-l)])) | p02684 |
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
init = [1]
len_loop = -1
while len(init) <= k:
nxt = a[init[-1]-1]
if nxt in init:
idx_nxt = init.index(nxt)
len_init = idx_nxt
len_loop = len(init) - idx_nxt
break
else:
init.ap... | n, k = list(map(int, input().split()))
a = tuple([int(n) - 1 for n in input().split()])
visited = [0] * n
next_town = 0
order = []
while True:
if visited[next_town]:
break
order.append(next_town)
visited[next_town] = 1
next_town = a[next_town]
first_loop = len(order)
offset = o... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
for i in range(n):
a[i] -= 1
mod = -1
num = [0]
prev = 0
cnt = 0
# num.append(0)
while True:
# print(num, cnt)
if not a[prev] in num:
num.append(a[prev])
prev = a[prev]
else:
lpst = a[prev] ... | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
for i in range(n):
a[i] -= 1
used = set()
num = [0]*n
used.add(0)
now = 0
cnt = 0
while True:
cnt += 1
if not a[now] in used:
used.add(a[now])
num[a[now]] = cnt
now = a[now]
else:
l... | p02684 |
def main():
N, K = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
d = []
d_set = set()
s = 1
for _ in range(N):
if s in d_set:
if K < len(d):
return print(d[K])
idx = d.index(s)
K -= idx
... | def main():
N, K = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
d = [-1]*(N+1)
v = 1
i = 0
while d[v] == -1:
d[v] = i
v = A[v-1]
i += 1
c_length = i - d[v] # 閉路長
pre_c_length = d[v]
if K < i:
return print(d.inde... | p02684 |
N, K = list(map(int, input().split()))
A = [int(i)-1 for i in input().split()]
visited_town = []
current_town = 0
loop_flg = False
visited_town.append(current_town)
for i in range(K):
current_town = A[current_town]
if current_town in visited_town:
visited_town.append(current_town)
... | def main():
N, K = list(map(int, input().split()))
A = [int(i)-1 for i in input().split()]
visited_town = []
current_town = 0
loop_flg = False
visited_town.append(current_town)
set_visted_town = set(visited_town)
for i in range(K):
current_town = A[current_town]... | p02684 |
N, K = list(map(int, input().split()))
*A, = list(map(int, input().split()))
A = [a-1 for a in A]
# Douling(ダブリング)
i = 1
to = [[a for a in A]]
while pow(2, i) < K:
t = [to[i-1][to[i-1][j]] for j in range(N)]
to.append(t)
i += 1
i -= 1
ans = [i for i in range(N)]
while i >= 0:
if K & (1 ... | N, K = list(map(int, input().split()))
*A, = list(map(int, input().split()))
A = [a-1 for a in A]
# Douling(ダブリング)
i = 1
to = [[a for a in A]]
while pow(2, i) < K:
t = [to[i-1][to[i-1][j]] for j in range(N)]
to.append(t)
i += 1
i -= 1
ans = 0
while i >= 0:
if K & (1 << i) > 0:
... | p02684 |
from copy import copy
n, k = list(map(int, input().split()))
a = [int(i) - 1 for i in input().split()]
ans = 0
while k:
if k & 1:
ans = a[ans]
c = [a[a[i]] for i in range(n)]
a = copy(c)
k >>= 1
print((ans + 1)) | n, k = list(map(int, input().split()))
a = [int(i) - 1 for i in input().split()]
ans = 0
while k:
if k & 1:
ans = a[ans]
c = [a[a[i]] for i in range(n)]
a = c[:]
k >>= 1
print((ans + 1)) | p02684 |
n, k = list(map(int,input().split()))
a = [int(i) for i in input().split()]
s = []
ord = [-1] * (n+1)
c, l = 1, 0
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]
print((s[k] if (k < l) else s[l + (k - l) % c])) | n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
ord = [-1] * (n + 1)
s = []
v = 1
c, l = 1, 0
while ord[v] == -1:
ord[v] = len(s)
s.append(v)
v = a[v - 1]
c = len(s) - ord[v]
l = ord[v]
print((s[k] if k < l else s[l + (k - l) % c])) | p02684 |
import sys
n,k=list(map(int,input().split()))
A=[int(i) for i in input().split()]
d=dict()
for i,a in enumerate(A):
d[i+1] = a
p=1
li = [1]
for i in range(k):
#print(p,li)
p = d[p]
if p in li:
pidx = li.index(p)
cyc = i+1-pidx
print((li[(k-pidx)%cyc+li.index(p)]))
... | import sys
n,k=list(map(int,input().split()))
A=[int(i) for i in input().split()]
d=dict()
for i,a in enumerate(A):
d[i+1] = a
p=1
li = set()
li.add(1)
for i in range(k):
#print(p,li)
p = d[p]
#print(p,li)
if p in li:
cyc = i + 1
q = 1
st=0
while q !... | p02684 |
from collections import deque
n, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
loop = deque([])
visited = set()
stack = deque([1])
loop_creating = False
last = 1
while True:
if loop_creating and len(visited) == 0:
break
nex = a[last - 1]
if nex in visited:... | from collections import deque
n, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
loop = []
visited = set()
s = set([1])
loop_creating = False
last = 1
while True:
if loop_creating and len(visited) == 0:
break
if k == len(s) - 1:
print(last)
exit(0)
... | p02684 |
import sys
from io import StringIO
import unittest
def resolve():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
loop_list = []
now = 0
if k > n:
while 1:
now = a[now] - 1
if now + 1 in loop_list:
s_ind... | import sys
from io import StringIO
import unittest
def resolve():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
loop_list = []
loop_set = set()
now = 0
if k > n:
for _ in range(k):
now = a[now] - 1
if now + 1 in l... | p02684 |
import sys
input = sys.stdin.readline
def main():
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = [1]
for i in range(1,K + 1):
a = A[B[i - 1] - 1]
if a in B:
x = B.index(a)
y = i - x
z = (K - x) % y
... | import sys
input = sys.stdin.readline
def main():
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = [1]
a = 1
check = [0] * len(A)
while True:
a = A[a - 1]
if check[a - 1] == 1:
x = B.index(a)
roop = B[x:]
... | p02684 |
#####################################################################################################
##### 深さ優先探索 幅優先探索 (グラフ)
#####################################################################################################
'''
キューに頂点を入れる回数は各々高々1回のみ。
各辺を使う回数は高々一回のみ(辺を戻るような探索は枝切りされている)
結果として、計算量はO(N + M)
... | #####################################################################################################
##### 深さ優先探索 幅優先探索 (グラフ)
#####################################################################################################
'''
キューに頂点を入れる回数は各々高々1回のみ。
各辺を使う回数は高々一回のみ(辺を戻るような探索は枝切りされている)
結果として、計算量はO(N + M)
... | p02684 |
#####################################################################################################
##### 深さ優先探索 幅優先探索 (グラフ)
#####################################################################################################
'''
キューに頂点を入れる回数は各々高々1回のみ。
各辺を使う回数は高々一回のみ(辺を戻るような探索は枝切りされている)
結果として、計算量はO(N + M)
... | # https://atcoder.jp/contests/abc167/tasks/abc167_d
'''
キューに頂点を入れる回数は各々高々1回のみ。
各辺を使う回数は高々一回のみ(辺を戻るような探索は枝切りされている)
結果として、計算量はO(N + M)
'''
import sys
input = sys.stdin.readline
import marshal
class Graph:
def __init__(self, n, dictated=False, decrement=True, destroy=False, edge=[]):
sel... | p02684 |
import sys
input = sys.stdin.readline
class Doubling:
def __init__(self, A, K_max, decrement=True):
"""
:param A: リスト。i から A[i] へと移動する
:param K_max: K_max = 2**(k_max) まで参照する可能性がある
:param decrement: True = A の要素の decrement が必要
"""
N = len(A)
k_ma... | #####################################################################################################
##### ダブリング
#####################################################################################################
"""
全体の要素数がN個あって1回移動した時にどの要素に到達するのか定まっているとき、
「K個先の要素を求めるのに O(K) かかる」ような状況において
前処理:O(N logK) ... | p02684 |
import sys
input = sys.stdin.readline
class Doubling:
def __init__(self, A, K_max, decrement=True):
"""
:param A: リスト。i から A[i] へと移動する
:param K_max: K_max = 2**(k_max) まで参照する可能性がある
:param decrement: True = A の要素の decrement が必要
"""
self.k_max = K_max.bit_len... | def cycle_getter(N, start):
"""
:param N: 移動回数
:param start: 初期条件
:return front: cycleまでの要素のリスト
cycle: cycle内の要素のリスト
end: cycle後の余った部分の要素のリスト
cnt: cycle回数
"""
p = start
front, cycle, end = [], [], []
cnt = 0
visit = {p:0}
L, R = N,... | p02684 |
# Problem D - Teleporter
# input
N, K = list(map(int, input().split()))
a_nums = list(map(int, input().split()))
# initialization
visit_list = [0]*N
visit_list[0] = 1
pos = a_nums[0]
visit_count = 1
while visit_list[pos-1]==0 and visit_count<K:
visit_count += 1
visit_list[pos-1] = visit_count
... | # Problem D - Teleporter
# input
N, K = list(map(int, input().split()))
a_list = list(map(int, input().split()))
# initialization
visit_list = [0] * N
pos = a_list[0]
visit_list[pos-1] = 1
visit_count = 1
remain = K - 1
is_ok = True
ans = 0
# count
while remain>0 and is_ok:
pos = a_list[pos-1]
... | p02684 |
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
checked = [0]
next_num = a[0]-1
i = 0
while True:
if i == 0:
i += 1
elif next_num in checked:
loop = checked[checked.index(next_num):]
break
checked.append(next_num)
next_num = a[next_num]-1
dif ... | n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
checked = [0]
next_num = a[0]-1
check = [0]*n
i = 0
while True:
if i == 0:
i += 1
elif check[next_num] == 1:
loop = checked[checked.index(next_num):]
break
checked.append(next_num)
check[next_num... | p02684 |
n, k, *a = list(map(int, open(0).read().split()))
a = [0] + a
p = 1
while k:
if k % 2:
p = a[p]
a = [a[b] for b in a]
k //= 2
print(p) | n,k,*a=list(map(int,open(0).read().split()));a=[0]+a;p=1
while k:p=k%2and a[p]or p;a=[a[b]for b in a];k//=2
print(p) | p02684 |
data = input().rstrip().split(" ")
tele = input().rstrip().split(" ")
telepo = [int(i) for i in tele]
max = int(data[1])
limit = max
times = 0
pos = 1
experience = [1]
while max > 0:
pos = telepo[pos - 1]
max -= 1
times += 1
#print(pos)
if pos in experience:
length = len(expe... | data = input().rstrip().split(" ")
tele = input().rstrip().split(" ")
telepo = [int(i) for i in tele]
max = int(data[1])
limit = max
times = 0
pos = 1
experience = [1]
check = {1}
before = 0
while max > 0:
pos = telepo[pos - 1]
max -= 1
times += 1
before = len(check)
check.add(pos)
... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a = [[i - 1, j - 1] for i, j in enumerate(a)]
g = [[0 for _ in range(n)] for _ in range(n)]
x = [0]
i = 0
j = a[0][1]
while g[i][j] != 1:
x.append(j)
g[i][j] = 1
i = j
j = a[j][1]
tmp = x.index(x[-1])
if tmp >= k... | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a = [i - 1 for i in a]
g = [-1] * n
i = 0
j = 0
while g[j] == -1:
if i == k:
print((j + 1))
exit()
g[j] = i
i += 1
j = a[j]
for i in range((k - g[j]) % (i - g[j])):
j = a[j]
print((j + 1)) | p02684 |
n,k = list(map(int,input().split()))
a = [int(i) - 1 for i in input().split()]
r = []
nx = 0
for i in range(n):
r.append(nx)
if k < i:
print((r[k] + 1))
break
if a[nx] in r:
m = r.index(a[nx])
d = i - m + 1
k = k - m
k = k % d
print((r[k ... | n,k = list(map(int,input().split()))
a = [int(i) - 1 for i in input().split()]
r = []
nx = 0
sr = set([])
for i in range(n):
r.append(nx)
sr.add(nx)
if k < i:
print((r[k] + 1))
break
tnx = a[nx]
if tnx in sr:
m = r.index(a[nx])
d = i - m + 1
k =... | p02684 |
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return list(map(int, sys.stdin.readline().split()))
def LI(): return list(map(int, sys... | import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return list(map(int, sys.stdin.readline().split()))
def LI(): return list(map(int, sys... | p02684 |
N,K=list(map(int,input().split()))
T=str(bin(K))
S=len(T)-2
A = list(map(int,input().split()))
lst=[[0 for e in range(N+1)] for f in range(S)]
for i in range(1,N+1):
lst[0][i]=A[i-1]
for j in range(1,S):
for i in range(1,N+1):
lst[j][i]=lst[j-1][lst[j-1][i]]
ans=[[e for e in range(N+1)] for f in range(S+1... | N,K=list(map(int,input().split()))
T=str(bin(K))
S=len(T)-2
A = list(map(int,input().split()))
lst=[[0 for e in range(N+1)] for f in range(S)]
for i in range(1,N+1):
lst[0][i]=A[i-1]
for j in range(1,S):
for i in range(1,N+1):
lst[j][i]=lst[j-1][lst[j-1][i]]
ans=1
for k in range(S):
if int(T[k+2])==1:
... | p02684 |
N, K = list(map(int, input().split()))
A = tuple([int(n) - 1 for n in input().split()])
costs = [-1] * N
loc = 0
if K <= N:
for i in range(K):
loc = A[loc]
print((loc + 1))
else:
costs[loc] = 0
for i in range(1, K):
loc = A[loc]
if costs[loc] >= 0:
... | N, K = list(map(int, input().split()))
# A = tuple(map(lambda n: int(n) - 1, input().split()))
A = tuple(map(int, input().split()))
costs = [-1] * N
loc = 1
if K <= N:
for i in range(K):
loc = A[loc - 1]
print(loc)
else:
costs[loc - 1] = 0
for i in range(1, K):
loc = A[... | p02684 |
N,K=list(map(int,input().split()))
*A,=list(map(int,input().split()))
point=[0]*N
count_X=0
count_Y=0
i=0
while all(j<3 for j in point):
if all(j<2 for j in point):
count_X+=1
point[i]+=1
i=A[i]-1
if any(k>1 for k in point):
count_Y+=1
count_Y-=1
if K>=count_X:
Z=c... | N,K=list(map(int,input().split()))
*A,=list(map(int,input().split()))
point=[0]*N
count_X=0
count_Y=0
i=0
while point[i]!=3:
if point[i]<2:
count_X+=1
point[i]+=1
i=A[i]-1
if point[i]>1:
count_Y+=1
count_Y-=1
if K>=count_X:
Z=count_X+(K-count_X)%count_Y
else:
... | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
l = [A[0]]
a = A[0] - 1
for _ in range(K):
if A[a] not in l:
l.append(A[a])
a = A[a] - 1
else:
a = A[a]
break
n = len(l)
b = 0
for i in range(n):
if l[i] == a:
del l[0:i]
... | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
l = [A[0]]
s = {A[0]}
a = A[0] - 1
for _ in range(K):
if A[a] not in s:
l.append(A[a])
s.add(A[a])
a = A[a] - 1
else:
a = A[a]
break
n = len(l)
b = 0
for i in range(n):
if l[i... | p02684 |
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, chain
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(eval(input()))
mis = lam... | '''
・基本的には素直なシミュレーションを行う
・ループ分のシミュレーションを省略することでTLEを回避
・Kは「残りのワープ数」と考える
K回のワープ中にループが含まれる場合、
1. 素直なシミュレーションを行う。このとき、Kを減算していく。
2. ループを検出したら、Kからループ長をできるだけ多く引くことで、計算を省略する。
3. 残りのステップ数、再度シミュレーションを行う
という手順で答えを求められる。
なお、ループが含まれない場合も1のみで答えを求められる。
'''
def main():
N, K = list(map(int, input().split()))
A ... | p02684 |
from collections import deque
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
g = [i for i in range(n)]
for i in range(n):
g[i] = a[i]-1
dist =[-1]*n
dist[0] = 0
q = deque([0])
hajimari = -1
loopnum = -1
while q:
v = q.popleft()
u = g[v]
if dist[u] == -1:
... | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
dist = [-1] * n
dist[0] = 0
now = 0
while True:
next = a[now] - 1
if dist[next] == -1:
dist[next] = dist[now] + 1
else:
hajimari = next
loopnum = dist[now] + 1 - dist[hajimari]
town = 0
... | p02684 |
n,k = (int(x) for x in input().split())
a =tuple([int(x) for x in input().split()])
l = [1]
now = 1
notloop = 0
dupl = -1
for i in range(k):
now = a[now - 1]
if now in l:
dupl = now
break
else:
l.append(now)
if(dupl == -1):
print(now)
else:
notloop = l.index(dupl)
... | n,k = (int(x) for x in input().split())
a = tuple([int(x) for x in input().split()])
c = [0] + [-1]*(n)
l = [1]
now = 1
dupl = -1
for i in range(k):
now = a[now - 1]
c[now - 1] += 1
if c[now - 1] == 1:
dupl = now
break
else:
l.append(now)
if dupl == -1:
print(now)
... | p02684 |
def main():
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
from collections import deque
q = deque()
p = deque()
q.append(1)
buf = 0
flg = True
idx = 1
cnt = 0
while flg:
buf = idx
idx = a[idx-1]
cnt += 1
... | def main():
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
# k<=10**18 < 2**60
r = 60
import math
r = math.ceil(math.log2(k))
idx = 0
a = [a[i]-1 for i in range(n)]
for i in range(r):
if (k>>i)&1 == 1:
idx = a[idx]
a = ... | p02684 |
# -*- coding: utf-8 -*-
# モジュールのインポート
import math
# 標準入力の取得
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# 求解処理
to = [A]
# ダブリング表の作成
for i in range(math.ceil(math.log2(K))):
to_i = to[i]
to_next = []
for j in range(N):
to_next.append(to_i[to_i[j]-1])
... | # -*- coding: utf-8 -*-
# モジュールのインポート
import math
def get_input() -> tuple:
"""
標準入力の取得
Returns:\n
tuple: 標準入力
"""
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
return N, K, A
def main(N: int, K: int, A: list) -> None:
"""
... | p02684 |
# -*- coding: utf-8 -*-
# モジュールのインポート
import math
def get_input() -> tuple:
"""
標準入力の取得
Returns:\n
tuple: 標準入力
"""
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
return N, K, A
def main(N: int, K: int, A: list) -> None:
"""
... | # -*- coding: utf-8 -*-
# モジュールのインポート
import math
def get_input() -> tuple:
"""
標準入力の取得
Returns:\n
tuple: 標準入力
"""
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
return N, K, A
def main(N: int, K: int, A: list) -> None:
"""
... | p02684 |
import sys
from io import StringIO
import unittest
sys.setrecursionlimit(10**5*2)
def search(u, n, g, disc, k):
if disc[u] != None:
circles = n - disc[u]
v = (k - disc[u]) % circles
ans = u
if n > k:
ans = 0
for _ in range(k):
a... | def resolve():
n, k = list(map(int, input().split()))
g = list(map(int, input().split()))
disc = [-1]*(n+1)
s = []
v = 1
c = 0
while disc[v] == -1:
disc[v] = len(s)
s.append(v)
v = g[v-1]
c = len(s) - disc[v]
l = len(s)
if l > k:
... | p02684 |
import sys
from io import StringIO
import unittest
sys.setrecursionlimit(10**6)
def search(n, step, rec, seq, k):
if rec[n] != -1:
c = step - rec[n]
sim = (k - rec[n]) % c
v = n
for i in range(sim):
v = seq[v-1]
print(v)
return
rec[n] = step
if step == k:
print(n)
return
search... | def resolve():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
rec = [True]*(n+2)
now = 0
stp = [1]
while rec[now]:
rec[now] = False
now = a[now] - 1
stp.append(now+1)
before_loop = stp.index(now+1)
loop = stp[before_loop:-1]
if k <= (before_loop + 1):
print((s... | p02684 |
def resolve():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
rec = [True]*(n+2)
now = 0
stp = [1]
while rec[now]:
rec[now] = False
now = a[now] - 1
stp.append(now+1)
before_loop = stp.index(now+1)
loop = stp[before_loop:-1]
if k <= (before_loop + 1):
print((s... | def resolve():
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
rec = [True]*(n+2)
now = 0
stp = [1]
while rec[now]:
rec[now] = False
now = a[now] - 1
stp.append(now+1)
before_loop = stp.index(now+1)
loop = stp[before_loop:-1]
if k <= (before_loop):
print((stp[k... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = {i:[] for i in range(n)}
ans = 0
for i in range(k):
b[a[ans]-1].append(ans)
if len(b[a[ans]-1]) == 2:
c = [ans]
ans = a[ans]-1
while ans != c[0]:
c.append(ans)
ans = a[ans]-1... | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
s = [-1]*n
c = []
ans = 0
for i in range(k):
s[ans] = i
c.append(ans)
ans = a[ans] - 1
if s[ans] >= 0:
t = i - s[ans] + 1
ans = c[(k-i-1) % t + s[ans]]
break
print((ans+1)) | p02684 |
n, k = list(map(int, input().split()))
a = [int(_) for _ in input().split()]
a = [0] + a
rep_check = 0
now = 1
place = []
if now == a[1]:
print((1))
exit()
while now not in place:
place.append(now)
now = a[now]
rep_check += 1
if rep_check == k:
print(now)
exi... | n, k = list(map(int, input().split()))
a = [int(_) for _ in input().split()]
a = [0] + a
rep_check = 0
now = 1
place = dict()
place2 = []
if now == a[1]:
print((1))
exit()
while now not in place:
place[now] = 1
place2.append(now)
now = a[now]
rep_check += 1
if rep_check ... | p02684 |
import sys
sys.setrecursionlimit(10**7) #再帰回数の上限を変更
from collections import deque
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
L=[]
chk=[0 for _ in range(N)]
tmp=0
while(1):
if chk[tmp]==1:
break
chk[tmp]=1
L.append(tmp)
tmp=A[tmp]-1
loop_s=A[L[-1]-1]-1... | import sys
sys.setrecursionlimit(10**7) #再帰回数の上限を変更
from collections import deque
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
L=[]
chk=[0 for _ in range(N)]
tmp=0
while(1):
if chk[tmp]==1:
break
chk[tmp]=1
L.append(tmp)
tmp=A[tmp]-1
#print(L)
loop_s=A[L... | p02684 |
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
while True:
na = A[past]
if na in history:
break
history.append(na)
past = na
s = history.index(na)
e = len(history)
loop = e - s
if K <= s: # ループ前終了
print((history[K]+1))
... | N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
s = set()
while True:
na = A[past]
if na in s:
break
s.add(na)
history.append(na)
past = na
s = history.index(na)
e = len(history)
loop = e - s
if K <= s: # ループ前終了
pri... | p02684 |
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
ans=0
now=1
i=1
result=[1]
while i<=k:
now=a[now-1]
if now in result:
x=result.index(now)
length=i-x
ans=result[(k-x)%length+x]
break
result.append(now)
i+=1
if ans!=0:
print(a... | n,k=list(map(int,input().split()))
a=list([int(x)-1 for x in input().split()])
ans=-1
l=[-1]*n
now=0
i=1
result=[0]
while i<=k:
now=a[now]
if l[now]!=-1:
length=i-l[now]
d=(k-i)%length+l[now]
ans=l.index(d)
break
l[now]=i
i+=1
if ans!=-1:... | p02684 |
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
tel_list = []
next_city = 0
city_have_been_to = set([0])
way = [0]
while k > 0:
k -= 1
temp = city_have_been_to.copy()
next_city = a[next_city] - 1
city_have_been_to.add(next_city)
way.append(next_city)... | n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
tel_list = []
next_city = 0
city_have_been_to = set([0])
way = [0]
while k > 0:
k -= 1
temp = len(city_have_been_to)
next_city = a[next_city] - 1
city_have_been_to.add(next_city)
way.append(next_city)
... | p02684 |
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]
def solve():
N, K = IL(int)
... | #!/usr/bin/env python3
import sys
input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]
def solve():
N, K = IL(int)
... | p02684 |
cin = open(0).read().strip().split('\n')
n,k = list(map(int, cin[0].split(' ')))
a = list(map(int, cin[1].split(' ')))
place = 1
history = []
rule = []
while len(rule) == 0:
history.append(place)
place = a[place-1]
for i in range(1,len(history)//2+1):
aset = set(history[len(history)-i*... | cin = open(0).read().strip().split('\n')
n,k = list(map(int, cin[0].split(' ')))
a = list(map(int, cin[1].split(' ')))
history = [1]
for i in range(n):
history.append(a[history[i]-1])
if n >= k:
print((history[k]))
else:
times = [-1] * n
for i in range(n):
if times[history[i]-1]... | p02684 |
N, K = list(map(int, input().split()))
A = list([int(x)-1 for x in input().split()])
town = 0
visited = [town]
loop = None
while K > 0:
K -= 1
town = A[town]
if town in visited:
loop = visited[visited.index(town):]
break
visited.append(town)
if loop:
K %= len(loop)
... | N, K = list(map(int, input().split()))
A = list([int(x)-1 for x in input().split()])
town = 0
visited = [town]
index = 0
visited_index = {town: index}
loop = None
while K > 0:
K -= 1
town = A[town]
if town in visited_index:
loop = visited[visited_index[town]:]
break
visite... | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
loop_checker = [1]
before_loop_len = 0
loop_len = 0
now = 0
while True:
if A[now] in loop_checker:
before_loop_len = loop_checker.index(A[now])
loop_slice = loop_checker[before_loop_len:]
loop_len = len(... | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
flag = [False] * N
counted = [1]
nextT = A[0]
flag[0] = True
for x in range(N):
if flag[nextT - 1] is False:
counted.append(nextT)
flag[nextT - 1] = True
nextT = A[nextT - 1]
else:
break
b... | p02684 |
n,k = list(map(int, input().split()))
A = list(map(int, input().split()))
now = 1
f = 0
past = [0]*n
if n>k:
for i in range(k):
now = A[now-1]
print(now)
exit()
c = 0
for i in range(n):
past[c] = now
now = A[now-1]
c+=1
if now in past:
f = 1
break
loop_end = c
loop_start = past.index... | n,k = list(map(int, input().split()))
b = list(map(int, input().split()))
a=0
for i in range(n):
b[i] -= 1
if k<n:
for i in range(k):
a = b[a]
print((a+1))
exit()
c = [-1]*n
cc = 0
se = set()
while a not in se:
c[a] = cc
cc += 1
se.add(a)
a = b[a]
for i in range... | p02684 |
import sys
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
tree = [[] for _ in range(n)]
for idx, a in enumerate(A):
tree[a - 1].append(idx)
tree[idx].append(... | import sys
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
tree = [[] for _ in range(n)]
for idx, a in enumerate(A):
tree[idx].append(a - 1)
start = []
dep... | p02684 |
import sys
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
tree = [[] for _ in range(n)]
for idx, a in enumerate(A):
tree[idx].append(a - 1)
start = []
dep... | import sys
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
tree = [[] for _ in range(n)]
for idx, a in enumerate(A):
tree[idx].append(a - 1)
def dfs(pre, dist):... | p02684 |
def main():
n, k = list(map(int, input().split()))
al = list(map(int, input().split()))
city_l = [1]
city_s = set([1])
loop_city_l = []
curr_city = 1
cnt = 0
for i in range(1,k+1):
next_c = al[curr_city-1]
if next_c in city_s:
cnt = i
... | n,k = list(map(int, input().split()))
al = list(map(int, input().split()))
cnt = 0
cl = [-1]*(n+1)
cll = [1]
curr = 1
for i in range(min(n,k)):
# print(curr)
next_c = al[curr-1]
if cl[next_c] != -1:
# loop_start = (i+1)
loop_start = cl[next_c]
rem = k - (i+1)
... | p02684 |
n, k = list(map(int, input().split(" ")))
a = [-1] + list(map(int, input().split(" ")))
i = 1
head = []
used = []
roop = []
inte=False
for _ in range(k):
i = a[i]
if i in used:
used.append(i)
head = used[0: used.index(i)]
roop = used[used.index(i): -1]
inte =Tr... | import sys
n, k = list(map(int, input().split(" ")))
a = [-1] + list(map(int, input().split(" ")))
i = 1
head = []
used = {}
roop = []
index = 0
for _ in range(k):
index += 1
i = a[i]
if i in used:
head = [ k for k, v in sorted(list(used.items()), key=lambda x:x[1]) if v < used[i]]... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
history = []
index = 0
cnt = 0
loop_start_city_index = 0
for i in range(k+1):
if i == k:
print((index+1))
exit()
if index in history: # すでに訪れた街にきた
loop_start_city_index = index
break
... | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
history = []
is_visited = [False] * n
index = 0
cnt = 0
loop_start_city_index = 0
for i in range(k+1):
if i == k:
print((index+1))
exit()
if is_visited[index]: # すでに訪れた街にきた
loop_start_city_ind... | 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 = [0]+list(map(int, input().split()))
num = 1
li = [1]
flag = [True] * n
flag[0] = False
for i in range(k):
num = a[num]
if flag[num-1]:
li.append(num)
flag[num-1] = False
else:
break
d = li.index(num)
ans = (k-d)%(len(li)-... | p02684 |
import math
import queue
from collections import defaultdict
def readInt():
return int(eval(input()))
def readInts():
return list(map(int, input().split()))
def readChar():
return eval(input())
def readChars():
return input().split()
def factorization(n):
res = []
if n%2==0:
res.append(2)
for... | import math
import queue
from collections import defaultdict
def readInt():
return int(eval(input()))
def readInts():
return list(map(int, input().split()))
def readChar():
return eval(input())
def readChars():
return input().split()
def factorization(n):
res = []
if n%2==0:
res.append(2)
for... | p02684 |
# = int(input())
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]
next = 1
List = []
for i in range(K):
List.append(next)
next = A[next-1]
if len(set(List)) != len(List):
... | # = int(input())
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]
next = 1
List = [-1]*N
for i in range(K):
if List[next-1] == -1:
List[next-1] = i
next = A[next-1]
... | p02684 |
from collections import deque
n,k = list(map(int,input().split()))
tele = {}
a = list(map(int,input().split()))
for i in range(1,n+1):
tele[i] = a[i-1]
lis = deque([1])
now = 1
n = 1
while tele[now] not in lis:
lis.append(tele[now])
now = tele[now]
p = lis.index(tele[now])
q = len(lis) - p
if ... | n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
l=[1]
di={}
di[1]=1
for i in a:
b=a[l[-1]-1]
if b in di:
x=b
break
l.append(b)
di[b]=1
t1=0
for j in l:
if j==x:
break
else:
t1+=1
t2=len(l)-t1
if k<=t1:
print((l[k... | p02684 |
N,K = list(map(int, input().split()))
A = [*[int(x)-1 for x in input().split()]]
cnt = [0]*N
pre = 0
for _ in range(N*2):
cnt[pre] += 1
now = A[pre]
pre = now
one = 0
loops = 0
for c in cnt:
if c == 1: one += 1
elif c > 1: loops += 1
k = max(0, K-one) % loops + min(K, one)
pre ... | N,K = list(map(int, input().split()))
A = [*[int(x)-1 for x in input().split()]]
cnt, pre = [0]*N, 0
for _ in range(N*2):
cnt[pre] += 1
pre = A[pre]
one, loops = 0, 0
for c in cnt:
if c == 1: one += 1
elif c > 1: loops += 1
k = max(0, K-one) % loops + min(K, one)
pre = 0
for _ in ran... | p02684 |
N,K = list(map(int, input().split()))
A = [*[int(x)-1 for x in input().split()]]
pre = 0
index = [-1]*N
path_cnt = 0
for i in range(N):
if index[pre] >= 0:
break
else:
index[pre] = i
path_cnt += 1
pre = A[pre]
loops = path_cnt - index[pre]
one = path_cnt - loops
k =... | N,K = list(map(int, input().split()))
A = [*[int(x)-1 for x in input().split()]]
index = [-1]*N
pre, path_cnt = 0, 0
for i in range(N):
if index[pre] >= 0: break
else:
index[pre] = i
path_cnt += 1
pre = A[pre]
loops = path_cnt - index[pre]
one = index[pre]
k = max(0, K-one) %... | p02684 |
N,K = list(map(int, input().split()))
A = [*[int(x)-1 for x in input().split()]]
index = [-1]*N
path = []
pre = 0
while index[pre] < 0:
index[pre] = len(path)
path += [pre]
pre = A[pre]
one = index[pre]
loops = len(path) - one
k = max(0, K-one) % loops + min(K, one)
pre = 0
for _ in range(k):... | N,K = list(map(int, input().split()))
A = [*[int(x)-1 for x in input().split()]]
index = [-1]*N
path = []
pre = 0
while index[pre] < 0:
index[pre] = len(path)
path += [pre]
pre = A[pre]
one = index[pre]
loops = len(path) - one
if K < one: print((path[K]+1))
else: print((path[(K-one) % loops + on... | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
order = []
ans = A[0] - 1
start = 0
stop = 0
for i in range(K):
order.append(ans)
if len(set(order)) != i+1:
stop = i - 1
start = order.index(ans)
new = order[start:stop+1]
break
els... | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
n = 1
order = [n]
order_set = set([n])
for i in range(1, K + 1):
n = A[n - 1]
if n in order_set:
Li = order.index(n)
Ri = len(order)
idx = Li + (K - Li) % (Ri - Li)
print((order[idx]))
... | p02684 |
from itertools import count
N, K = list(map(int, input().split()))
As = [int(n) for n in input().split()]
visited = []
now = 0
num = 0
idx = 0
for i in count():
if now in visited:
idx = visited.index(now)
num = len(visited) - idx
break
visited.append(now)
now = As[no... | from itertools import count
N, K = list(map(int, input().split()))
As = [int(n) for n in input().split()]
visited = []
visited_set = set()
now = 0
num = 0
idx = 0
for i in count():
if now in visited_set:
idx = visited.index(now)
num = len(visited) - idx
break
visited_set... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
loop = 0
use = [0] * n
now = 0
use[now] = 1
x = [1]
for _ in range(n):
next = a[now] - 1
x.append(next+1)
if use[next] == 0:
use[next] = 1
now = next
else:
break
y = []
while x.count(x[0]) == 1:
... | n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
loop = 0
use = [0] * n
now = 0
use[now] = 1
x = [1]
for _ in range(n):
next = a[now] - 1
x.append(next+1)
if use[next] == 0:
use[next] = 1
now = next
else:
break
loop = x.pop(-1)
tmp = x.index(lo... | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
visited = [-1]*(N+1)
path = []
at = 1
count = 0
while True:
if visited[at] != -1:
loop = count-visited[at]
if K < count: # (K-visited[at])//loopが1以上であることを保証
print((path[K])) # 条件 K < visited[at]なら、(K-vis... | n, k = list(map(int, input().split()))
q = list(map(int, input().split()))
v = [-1]*(n+1)
p = []
a = 1
c = 0
while 1:
if v[a] != -1:
l = c-v[a]
if k < c:
print((p[k]))
else:
print((p[(k-v[a])%l+v[a]]))
break
v[a] = c
c += 1
p.append... | p02684 |
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [x-1 for x in A]
town = 0
cnt = 0
# memo = []
# visited = [False] * n
# visited[0] = -1
# start = 0
# loop = 10**8
# start_cnt = 0
# for i in range(n*2):
# town = A[town]
# memo.append(town)
# if visited[town]... | n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [x-1 for x in A]
town = 0
cnt = 0
visited = [False] * n
visited[0] = -1
start_cnt = 0
for i in range(n*2):
town = A[town]
if visited[town] != False:
start_cnt = i + 1
loop = i - visited[town]
... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
now = 1
path = [now]
for i in range(k):
now = a[now-1]
if now in set(path):
now = path[path.index(now) + (k-i-1)%(len(path) - path.index(now))]
break
path.append(now)
print(now)
| n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
alr = [0 for i in range(n)]
now = 1
path = [now]
for i in range(k):
now = a[now-1]
# if now in path:
# now = path[path.index(now) + (k-i-1)%(len(path) - path.index(now))]
# break
if alr[now-1] != 0:
... | p02684 |
N, K = list(map(int, input().split(" ")))
routs = {}
location = [0 for i in range(N + 1)]
tereporter = list(map(int, input().split()))
for i in range(N):
routs[i] = tereporter[i] - 1
present_location = 0
n = 0
for i in range(K):
n += 1
present_location = routs[present_location]
if location[present_lo... | N, K = list(map(int, input().split()))
teleporters = list(map(int, input().split()))
n = 0
present = 0
praces = [-1 for i in range(N)]
praces[0] = 0
for i in range(K):
present = teleporters[present] - 1
n += 1
if praces[present] == -1:
praces[present] = n
else:
before = praces[present]
... | p02684 |
N, K = list(map(int, input().split()))
teleporters = list(map(int, input().split()))
n = 0
present = 0
praces = [-1 for i in range(N)]
praces[0] = 0
for i in range(K):
present = teleporters[present] - 1
n += 1
if praces[present] == -1:
praces[present] = n
else:
before = praces[present]
... | from sys import exit
n, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
done = [-1 for _ in range(n)]
tmp = 0
done[0] = 0
for k in range(1, K + 1):
tmp = A[tmp]
if done[tmp] >= 0:
for i in range((K - done[tmp]) % (k - done[tmp])):
tmp = A[tmp]
... | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
visitcount = dict()
n_point = list()
x = 1
count = 0
while(x not in set(visitcount)):
visitcount[x] = count
n_point.append(x)
x = A[x-1]
count += 1
period = count - visitcount[x]
if (K < visitcount[x]):
print((... | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
visitset = set()
visitcount = dict()
n_point = dict()
x = 1
count = 0
while(x not in visitset):
visitset.add(x)
visitcount[x] = count
n_point[count] = x
x = A[x-1]
count += 1
period = count - visitcount[x]
if ... | p02684 |
n,k,*a=list(map(int,open(0).read().split()))
d={}
s=1
while k:k-=1;s=a[s-1];k%=d.get(s,2**61)-k;d[s]=k
print(s) | n,k,*a=list(map(int,open(0).read().split()))
d=[2**61]*-~n
s=1
while k:k-=1;s=a[s-1];k%=d[s]-k;d[s]=k
print(s) | p02684 |
N, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
R = []
done = set()
todo = [0]
done = set()
bp = None
while todo:
p = todo.pop()
R.append(p)
done.add(p)
np = A[p]
if np in done:
bp = np
else:
todo.append(np)
b = R.index(bp)... | N, K = list(map(int, input().split()))
A = tuple(int(x) - 1 for x in input().split())
done = {0}
rt = [0]
loop_point = 0
while True:
p = rt[-1]
np = A[p]
if np in done:
loop_point = np
break
done.add(np)
rt.append(np)
if K < len(rt):
ans = rt[K] + 1
else:
K ... | p02684 |
N, K = list(map(int, input().split()))
where_go = list(map(int, input().split()))
now = 0
PATH = []
start = 0
roop = 0
count = 0
while now not in PATH and count < K:
PATH.append(now)
now = where_go[now] - 1
count += 1
if count < K:
start = PATH.index(now)
roop = len(PATH) - start
... | N, K = list(map(int, input().split()))
A = list(map(int, input().split())) #どこに行くかのリスト
now = 0 #どこにいるか
LOG = {}
roop = 0
count = 0
while now not in LOG and count < K:
LOG.setdefault(now, count)
now = A[now] - 1
count += 1
if count < K:
roop = len(LOG) - LOG[now]
K = (K - count) % ro... | p02684 |
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
pl=1
visit=[1]
loop=0
q=True
v=True
cnt=0
br=False
while q:
cnt+=1
pl=a[pl-1]
for i in visit:
if pl==i:
loop=visit.index(pl)
br=not br
break
if br:
break
visit.a... | n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
q=True
v=True
cnt=0
while q:
cnt+=1
pl=a[pl-1]
if visit[pl-1]!=-1:
loop=visit[pl-1]
break
visit[pl-1]=cnt
if cnt==k:
print(pl)
v=not v
... | p02684 |
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
q=True
v=True
cnt=0
while q:
cnt+=1
pl=a[pl-1]
if visit[pl-1]!=-1:
loop=visit[pl-1]
break
visit[pl-1]=cnt
if cnt==k:
print(pl)
v=not v
... | n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
pl=1
visit=[-1]*n
visit[0]=0
loop=0
cnt=0
while 1:
cnt+=1
pl=a[pl-1]
if visit[pl-1]!=-1:
loop=visit[pl-1]
break
visit[pl-1]=cnt
if cnt==k:
print(pl)
exit()
loop_len=cnt-loop
q=Tr... | p02684 |
n, k = list(map(int, input().split()))
a = [0] + list(map(int,input().split()))
explored = [0] * (n+1)
t = 1
cycle = 0
while True:
if explored[t] == 2:
k %= cycle
explored[t] += 1
elif explored[t] == 1:
explored[t] += 1
cycle += 1
elif explored[t] == 0:
... | n, k = list(map(int, input().split()))
a = [0] + list(map(int,input().split()))
explored = [0] * (n+1)
t = 1
cycle = 0
while True:
if explored[t] == 2:
k %= cycle
if k == 0:
k = cycle
explored[t] += 1
elif explored[t] == 1:
explored[t] += 1
cy... | p02684 |
n, k, *A = list(map(int, open(0).read().split()))
#n, k = map(int, input().split())
#A = [int(a) for a in input().split()]
Jump = [None] * n
Jump[0] = 1
next = 0
for i in range(1,n+1):
to = A[next]
if to in Jump:
start = Jump.index(to)
end = i
dis = end - start
ro... | n, k = list(map(int, input().split()))
A = [int(a) for a in input().split()]
Jump = [None] * n
Jump[0] = 1
Rireki = [-1] * (n+1)
Rireki[0] = 0
next = 0
for i in range(1,n+1):
to = A[next]
if Rireki[to] >= 0:
start = Jump.index(to)
end = i
dis = end - start
roop =... | p02684 |
n,k = list(map(int,input().split()))
s = list(map(int,input().split()))
# print(s)
now_city = 1
s_l_city = 1
t = [1] #何回そこを通ったか
f = [0] #何回目にそこを通ったか
a=0
b = 0#サイクルありなし
f_l=0
for i in range(n-1):
t.append(0)
f.append(-1)
# print(s)
sycle = 0
for i in range(1,n+1):
now_city = s[now_city-1]
... | N, K = list(map(int, input().split()))
A = input().split()
A = [int(a) for a in A]
S = [1]
a = 1
number = 0
numbers=[-1 for _ in range(N)]
for _ in range(N + 1):
b = A[a - 1]
if numbers[b-1] != -1:
c=numbers[b-1] #1回目にループが現れた回数-1
# print("ループの最初の数字",b,"最初は"+str(c+1)+"回目","周期は",nu... | p02684 |
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# 扱いやすいので1-indexedにする
A = [0] + A
# dp[2^k][i] はiから2^k回移動したときの移動先を表す
dp = {1: A}
for k in range(1, 100):
dp[2 ** k] = [0] # 先頭はダミー(1-indexedにするため)
for i in range(1, N + 1):
# jはiから2^{i - 1}回移動した先
j = dp[2 ** (k... | N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
# 扱いやすいので1-indexedにする
A = [0] + A
# dp[k][i] はiから2^k回移動したときの移動先を表す
dp = [A]
for k in range(1, 100):
dp.append([0]) # 先頭はダミー(1-indexedにするため)
for i in range(1, N + 1):
# jはiから2^{i - 1}回移動した先
j = dp[k - 1][i]
... | p02684 |
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
ans =1
now = [1]
ed = set([1])
for i in range(n+1):
if a[now[i]-1] in ed:
break
now.append(a[now[i]-1])
ed = set(now)
t = len(now)-now.index(a[now[i]-1])
f = now.index(a[now[i]-1])
if k >= f + t:
... | import sys
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
visited = [None]*n
cur = 0
for i in range(k):
if visited[cur] is None:
visited[cur] = i
cur = a[cur]-1
else:
break
else:
print((cur+1))
sys.exit()
k -= i
k %= i-visited[cur]
... | p02684 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.