ZhenlongDai/ACPR huggingface dataset:
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n , k = map(int,input().split())
S = list(map(int,input().split()))
l = [1]
for i in range(2*n):
    l.append(S[l[i]-1])

L = len(l)
def f(s, e):
    a = s
    b = s

    while True:
        a += 2
        b += 1
        if a >= e:
            return None
        if l[a] == l[b]:
            break

    a = 0
    while True:
        a += 1
        b += 1
        if b >= e:
            return None
        if l[a] == l[b]:
            break

    return a, b

c, d = f(0, L)
loop = l[c:d]

m = d - c 
A = c + 1
B = d + 1
if k <=  n:
    print(l[k])
else:
   K = (k - A-1) % m
   print(loop[K-1])

Failing test case input:
6 2190427306125562200
6 4 3 1 3 2

Expected output:
1


Actual output:
6


Fixed submission (verdict: Accepted):
n , k = map(int,input().split())
S = list(map(int,input().split()))
l = [1]
for i in range(2*n):
    l.append(S[l[i]-1])

L = len(l)
def f(s, e):
    a = s
    b = s

    while True:
        a += 2
        b += 1
        if a >= e:
            return None
        if l[a] == l[b]:
            break

    a = 0
    while True:
        a += 1
        b += 1
        if b >= e:
            return None
        if l[a] == l[b]:
            break

    return a, b

c, d = f(0, L)
loop = l[c:d]

m = d - c 
A = c + 1
B = d + 1
if k <=  n:
    print(l[k])
else:
   K = (k - c) % m
   print(loop[K])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1]=l[i]
visited=set()
l=[]
i=1
while True:
    l.append(i)
    if i not in visited:
        visited.add(i)
    else:
        break
    i=d[i]
first=l.index(l[-1])
if k<first:
    print(l[k])
else:
    k=(len(l)-first)%((len(l)-1)-first)
    k+=first
    print(l[k])

Failing test case input:
6 242746494311961546
2 1 1 5 2 2

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
from collections import defaultdict
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1]=l[i]
visited=set()
l=[]
i=1
while True:
    l.append(i)
    if i not in visited:
        visited.add(i)
    else:
        break
    i=d[i]
first=l.index(l[-1])
if k<first:
    print(l[k])
else:
    k=(k-first)%((len(l)-1)-first)
    k+=first
    print(l[k])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())
a = tuple(map(int, input().split()))

i = 0
aa = 0
visited = {}
for i in range(k):
    aa = a[aa] - 1
    if aa in visited:
        s, e = visited[aa], i
        loop = e - s
        k = s + ((k - s) % loop)
        exit(0)
    visited[aa] = i

print(aa + 1)


Failing test case input:
4 9
3 2 4 1

Expected output:
1


Actual output:

Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
a = tuple(map(int, input().split()))

i = 0
aa = 0
visited = {}
for i in range(k):
    aa = a[aa] - 1
    if aa in visited:
        s, e = visited[aa], i
        loop = e - s
        k = s + (((k-1) - s) % loop)
        print(list(visited.keys())[k] + 1)
        exit(0)
    visited[aa] = i

print(aa+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = [1]
if k > 5*n:
    for i in range(k % n):
        ans.append(a[ans[i] - 1])
    print(a[-1])
else:
    for i in range(k):
        ans.append(a[ans[i] - 1])
    print(a[-2])

Failing test case input:
6 727202214173249351
6 5 1 5 4 2

Expected output:
5


Actual output:
2


Fixed submission (verdict: Accepted):

n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = [1]
c = 1
for i in range(n):
    ans.append(a[ans[i] - 1])
b = ans.index(ans[n])
if k < b:
    for l in range(k):
        c = a[c - 1]
else:
    for l in range((k-b)%(n-b)+b):
        c = a[c - 1]
print(c)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, k = map(int, input().split())
tel = list(map(int, input().split()))
log = [1]
end = True
while(end):
    log.append(tel[log[-1] - 1])
    for i in range(len(log) - 1):
        if(log[i] == log[-1]):
            star = i
            mod = len(log) - star - 1
            end = False
print(log)
print(mod)
if(k < len(log)):
    print(log[k])
else:
    print(log[(k - star) % mod + star])

Failing test case input:
6 727202214173249351
6 5 1 5 1 2

Expected output:
5


Actual output:
[1, 6, 2, 5, 1]
4
5


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
tel = list(map(int, input().split()))
log = [1]
record = [0] * n
while(True):
    log.append(tel[log[-1] - 1])
    if(record[log[-1] - 1] == 1):
        break
    record[log[-1] - 1] += 1
for i in range(len(log) - 1):
    if(log[i] == log[-1]):
        star = i
        mod = len(log) - star - 1
        break
if(k < len(log)):
    print(log[k])
else:
    print(log[(k - star) % mod + star])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(K):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(now_town)
    sys.exit()

roop = len(queue) - (queue[start] - 1)
before_len = len(queue) - roop

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])


Failing test case input:
6 165759462200935729
4 5 2 5 3 2

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
import sys
import time

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

queue = {}

start = -1
count = 1

now_town = 1

for i in range(K):
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue[town_list[now_town - 1]] = count
    now_town = town_list[now_town - 1]
    count += 1

if start == -1:
    print(now_town)
    sys.exit()

roop = len(queue) - (queue[start] - 1)
before_len = queue[start]

print(list(queue.keys())[(K - before_len) % roop + before_len - 1])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
now = 1
used = [-228] * (n + 191)
used[1] = -1
cn = -292
for _ in range(k):
    now = A[now]
    if used[now] != -228:
        cn = _ + 1 - used[now]
        break
    used[now] = _
if cn == -292:
    print(now)
    exit(0)
for i in range(k % cn):
    now = A[now]
print(now)


Failing test case input:
6 727202214173249351
6 5 4 5 3 2

Expected output:
4


Actual output:
5


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
now = 1
used = [-228] * (n + 191)
used[1] = -1
cn = -292
ahah = 1
for _ in range(k):
    now = A[now]
    if used[now] != -228:
        cn = _ + 1 - used[now] - 1
        break
    used[now] = _
    ahah += 1
if cn == -292:
    print(now)
    exit(0)
for i in range((k - ahah) % cn):
    now = A[now]
print(now)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int,input().split())
A = list(map(int,input().split()))

check = [0] * N 
check[0] = 1
li = [] 
li.append(1)
i, j = 1, 1 

while True:
  k = A[i-1] 
  if check[k-1] == 1:
    
    n = li.index(k) 
    break
  check[k-1] = 1
  li.append(k)
  i = k
  j += 1

if K <= n-1: print(li[K])
else:
  if j > n :
    print(li[((K - (n-1)) % (j-n)) + n - 1])
  elif j == n:
    print(li[(K % j) + n])

Failing test case input:
6 727202214173249351
3 5 3 8 3 2

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):
N, K = map(int,input().split())
A = list(map(int,input().split()))

check = [0] * (N+1) 
li = [] 
i, j = 1, 1 

while True:
  k = A[i-1] 
  if check[k] == 1:
    
    n = li.index(k)+1 
    break
  check[k] = 1
  li.append(k)
  i = k
  j += 1

if K < n: print(li[K-1])
else: print(li[(K-n) % (j-n) + n - 1])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int,input().split())
A = list(map(int,input().split()))

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



Failing test case input:
4 0
3 0 4 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
N, K = map(int,input().split())
A = list(map(int,input().split()))

count = 0
i = 1
position = 1
position_list = [1]
position_history = [0] * N
start_loop = 0
def last():
  k = (K - start_loop) % (count - start_loop)
  print(position_list[start_loop+k])

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

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

check = [0]*N
i = 0
c = 0
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-check.index(i))%len(X)])  
else: 
    print(i+1)

Failing test case input:
4 0
3 0 4 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

check = [0]*N
i = 0
c = 1
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-check.index(i))%len(X)])  
else: 
    print(i+1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(lambda x: x - 1, map(int, input().split())))
print(A)
current = 0
memo = {
    0: 0
}
for i in range(1, K + 1):
    current = A[current]
    if current in memo:
        
        length = i - memo[current]
        tmp = (K - memo[current]) % length
        
        for j in range(tmp):
            current = A[current]
        break
    else:
        memo[current] = i
print(current + 1)

Failing test case input:
6 2190427306125562200
6 4 3 2 3 3

Expected output:
3


Actual output:
[5, 3, 2, 1, 2, 2]
3


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(lambda x: x - 1, map(int, input().split())))
current = 0
memo = {
    0: 0
}
for i in range(1, K + 1):
    current = A[current]
    if current in memo:
        
        length = i - memo[current]
        tmp = (K - memo[current]) % length
        for j in range(tmp):
            current = A[current]
        break
    else:
        memo[current] = i
print(current + 1)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, k = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]

t = 0
l = []
c = 0
d = {}
while True:
    d[t+1] = c
    c += 1
    l.append(t+1)
    t = a[t] - 1
    
    
    if t + 1 in d:
        s = l[d[t+1]:]
        q = t + 1
        break




if q == 1:
    print(s[k % len(s)])
else:
    if k <= q:
        print(l[k])
    else:
        print(s[(k-q) % len(s)])

Failing test case input:
6 237500909755776657
6 4 1 5 6 2

Expected output:
6


Actual output:
5


Fixed submission (verdict: Accepted):
n, k = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]

t = 0
l = []
c = 0
d = {}
while True:
    d[t+1] = c
    c += 1
    l.append(t+1)
    t = a[t] - 1
    
    
    if t + 1 in d:
        s = l[d[t+1]:]
        q = d[t+1]
        break





if k <= q:
    print(l[k])
else:
    print(s[(k-q) % len(s)])
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    pos=set()
    pos.add(0)
    i=0
    traversal=[0]
    start=0
    nodes=[1]
    while(start<K):
        position=A[i]-1
        if position in pos:
            break
        traversal.append(position)
        pos.add(position)
        start+=1
        nodes.append(A[i])
        i=position
    if start==K:
        print(A[traversal[-1]])
    else:
        ind=traversal.index(position)
        interval=len(traversal)-ind
        if interval%2==0:
            print(nodes[ind+((K-start+3)%interval)])
        else:
            print(nodes[ind+((K-start+2)%interval)])
if __name__=='__main__':
    main()

Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    pos=set()
    pos.add(0)
    i=0
    traversal=[0]
    start=0
    nodes=[1]
    while(start<K):
        position=A[i]-1
        if position in pos:
            break
        traversal.append(position)
        pos.add(position)
        start+=1
        nodes.append(A[i])
        i=position
    if start==K:
        print(nodes[-1])
    else:
        ind=traversal.index(position)
        interval=len(traversal)-ind
        print(nodes[ind+((K-ind)%interval)])
if __name__=='__main__':
    main()

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
count_arr = [0] * n
count = 0

now = 0
loop_length = -1
while True:
    if count == k:
        print(a[now] + 1)
        exit()
    if count_arr[now] != 0:
        loop_length = count - count_arr[now] + 1
        break
    count += 1
    count_arr[now] = count
    now = a[now]


rest = (k - count) % loop_length


if rest == 0:
    print(now + 1)
    exit()

while rest > 0:
    rest -= 1
    if rest == 0:
        break
    now = a[now]
print(a[now] + 1)


Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):


n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
count_arr = [0] * n
count = 0

now = 0
loop_length = -1
while True:
    if count == k:
        print(now + 1)
        exit()
    if count_arr[now] != 0:
        loop_length = count - count_arr[now] + 1
        break
    count += 1
    count_arr[now] = count
    now = a[now]


rest = (k - count) % loop_length


if rest == 0:
    print(now + 1)
    exit()

while rest > 0:
    rest -= 1
    if rest == 0:
        break
    now = a[now]
print(a[now] + 1)
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n,k=map(int,input().split())
al=[0]+list(map(int,input().split()))
 

visited=[-1]*(1+n)
cl=[0]*(1+n)
visited[1]=0
c=0
now=1
t=0
 
start=0
 
for i in range(1,n+1):
    if visited[al[now]]==-1: 
        visited[al[now]]=0
        c+=1
        cl[al[now]]=c
        now=al[now]
    else:  
        start=cl[al[now]]
        t=i-start
        break

if start>=k:
    print(cl.index(k))
else:

    print(cl.index(start+(k-start)%t))

Failing test case input:
6 242746494311961546
3 1 1 5 2 2

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
al=[0]+list(map(int,input().split()))

visited=[-1]*(n+1)
visited[1]=0

lstart=0
t=0

now=1
while lstart==0:
    if visited[al[now]]<0:
        visited[al[now]]=visited[now]+1
        now=al[now]
        
    else:
        t=visited[now]-visited[al[now]]+1
        lstart=al[now]

if visited[lstart]>=k:
    print(visited.index(k))
            
else:
    print(visited.index(visited[lstart]+(k-visited[lstart])%t))

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
from collections import defaultdict

n, k = map(int, input().split())
*a, = map(int, input().split())
a = [a[i] - 1 for i in range(n)]
ans = 0

if k <= n:
    for i in range(k):
        ans = a[i]

else:
    
    start_period = 1
    period_i = -1
    d = defaultdict(int)
    d[0] = 1
    i = 0
    count = 1
    while(1):
        if a[i] in d.keys():
            period_i = count
            start_period = d[a[i]]
            break

        d[a[i]] = count + 1
        i = a[i]
        count += 1
        
    
    d_r = {i:k for k, i in d.items()}
    ans = d_r[start_period + ((k - (start_period - 1)) % (period_i - start_period + 1))] + 1
    
    
print(ans)

Failing test case input:
4 0
3 1 4 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
from collections import defaultdict

n, k = map(int, input().split())
*a, = map(int, input().split())
a = [a[i] - 1 for i in range(n)]
ans = 0

if k <= n:
    i = 0
    while(k > 0):
        ans = a[i]
        i = a[i]
        k -= 1
        
    ans += 1

else:
    
    start_period = 1
    period_i = -1
    d = defaultdict(int)
    d[0] = 1
    i = 0
    count = 1
    while(1):
        
        if a[i] in d.keys():
            period_i = count
            start_period = d[a[i]]
            break

        d[a[i]] = count + 1
        i = a[i]
        count += 1
        
    
    d_r = {i:k for k, i in d.items()}
    
    
    
    
    
    
    
    
    ans = d_r[start_period + ((k - (start_period - 1)) % (period_i - start_period + 1))] + 1
    
    
print(ans)
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))
tele = [0] * N
for i in range(N):
    tele[i] = A[i] - 1
now = 0
cnt = 0
exp = [False] * N
period = 0
loop = False
start = -1
while cnt < K:
    exp[now] = True
    now = tele[now]
    cnt += 1
    if loop:
        period += 1
        if now == start:
            loop = False
            cnt += K - ((K - cnt) % period)
    if exp[now] and (not loop):
        loop = True
        start = now
    
print(now+1)

Failing test case input:
6 786427263574235267
2 1 1 5 2 2

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))
tele = [0] * N
for i in range(N):
    tele[i] = A[i] - 1
now = 0
cnt = 0
exp = [False] * N
period = 0
loop = False
start = -1
while cnt < K:
    exp[now] = True
    now = tele[now]
    cnt += 1
    if loop:
        period += 1
        if now == start:
            loop = False
            cnt = K - ((K - cnt) % period)
    if exp[now] and (not loop):
        loop = True
        start = now
    
print(now+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N,K=map(int,input().split())
A=list(map(int,input().split()))
ischecked=[0]*N
move=[]
move1=[]
now=0
count=0
count1=0
while 1:
    move.append(now+1)
    nex=A[now]-1
    ischecked[now]=1
    if ischecked[nex]==1:
        roop=nex+1
        break
    now=nex
if K>len(move):
    for i in range(len(move))[::-1]:
        move1.append(move[i])
        if move[i]==roop:
            b=len(move)-i
            break
    n=K-len(move)
    n=n%len(move1)
    print(move1[len(move1)-1-n])
else:
    print(move[K-1])

Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
4


Fixed submission (verdict: Accepted):
N,K=map(int,input().split())
A=list(map(int,input().split()))
ischecked=[0]*N
move=[]
move1=[]
now=0
count=0
count1=0
while 1:
    move.append(now+1)
    nex=A[now]-1
    ischecked[now]=1
    if ischecked[nex]==1:
        roop=nex+1
        break
    now=nex
for i in range(len(move))[::-1]:
    move1.append(move[i])
    if move[i]==roop:
        b=len(move)-i
        break
if K>=len(move):
    n=K-len(move)
    n=n%len(move1)
    print(move1[len(move1)-1-n])
else:
    print(move[K])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):




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

    visited = [0] * (N+1)
    seq = []

    step = 0
    current = 1

    n1 = n2 = 0
    while(step<len(A)):
        
        seq.append(current)
        step += 1
        visited[current] = step
        next_pos = A[current-1]
        if visited[next_pos]>0:
            n1 = visited[next_pos]
            n2 = step - n1 + 1
            break
        current = next_pos

    
    
    
    if n1+n2<=K:
        print(seq[n1-1+((K-(n1-1))%n2)])
    else:
        print(seq[K-1])

    return


if __name__ == '__main__':
    main()


Failing test case input:
4 0
3 0 4 1

Expected output:
1


Actual output:
4


Fixed submission (verdict: Accepted):




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

    visited = [0] * (N+1)
    seq = []

    step = 0
    current = 1

    n1 = n2 = 0
    while(step<len(A)):
        
        seq.append(current)
        step += 1
        visited[current] = step
        next_pos = A[current-1]
        if visited[next_pos]>0:
            n1 = visited[next_pos]
            n2 = step - n1 + 1
            seq.append(next_pos)
            break
        current = next_pos

    
    
    
    if n1+n2<=K:
        print(seq[n1-1+((K-(n1-1))%n2)])
    else:
        print(seq[K])

    return


if __name__ == '__main__':
    main()

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int,input().split())
A = list(map(int,input().split()))

check = [0] * N 
check[0] = 1
li = [] 
li.append(1)
i, j = 1, 1 

while True:
  k = A[i-1] 
  if check[k-1] == 1:
    
    n = li.index(k) 
    break
  check[k-1] = 1
  li.append(k)
  i = k
  j += 1

if K <= n-1: print(li[K])
else:
  if j-n > 0:
    print(li[((K - (n-1)) % (j-n)) + n - 1])
  else:
    print(li[((K - (n-1)) % (j)) + n - 1])

Failing test case input:
6 727202214173249351
6 5 1 5 2 2

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):
N, K = map(int,input().split())
A = list(map(int,input().split()))

check = [0] * (N+1) 
li = [] 
i, j = 1, 1 

while True:
  k = A[i-1] 
  if check[k] == 1:
    
    n = li.index(k)+1 
    break
  check[k] = 1
  li.append(k)
  i = k
  j += 1

if K < n: print(li[K-1])
else: print(li[(K-n) % (j-n) + n - 1])
Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N , K = map(int,input().split())
B = {}
C = {}
D = {0}
A = list(map(int,input().split()))
place = 0
num = 0
C[0] = 0
while True:
    place = int(A[place]) - 1
    num += 1   
    if place in D:
        Y = int(num) - int(C[place])
        break
    elif num == K:
        break
    else:
        B[num] = place
        C[place] = num
        D.add(place)
try:
    X = (K - num) % Y
    print(B[C[place] + X] + 1)
except:
    print(int(A[place]-1))

   

Failing test case input:
6 1110123996242283897
6 5 1 5 1 3

Expected output:
1


Actual output:
5


Fixed submission (verdict: Accepted):
N , K = map(int,input().split())
B = {}
C = {}
D = {0}
A = list(map(int,input().split()))
place = 0
num = 0
C[0] = 0
while True:
    place = int(A[place]) - 1
    num += 1   
    if place in D:
        Y = int(num) - int(C[place])
        break
    elif num == K:
        break
    else:
        B[num] = place
        C[place] = num
        D.add(place)
try:
    X = (K - num) % Y
    print(B[C[place] + X] + 1)
except:
    print(place+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))

visited = [-1] * N
visited[0] = 0
nxt = 1
cnt = 1
for i in range(N):
    nxt = A[nxt-1]
    if visited[nxt-1] == -1:
        visited[nxt-1] = cnt
        cnt += 1
    else:
        cycl = i + 1 - visited[nxt-1]
        m = visited[nxt-1]
        break
print(visited, cycl)

k = (K - m) % cycl + m

for i in range(N):
    if visited[i] == k:
        print(i+1)
        break


Failing test case input:
6 357997123416626975
4 4 0 1 0 2

Expected output:
4


Actual output:
[0, -1, -1, 1, -1, -1] 2
4


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))

visited = [-1] * N
visited[0] = 0
nxt = 1
cycl = N
m = 0
for i in range(N):
    nxt = A[nxt-1]
    if visited[nxt-1] == -1:
        visited[nxt-1] = i+1
    else:
        cycl = i + 1 - visited[nxt-1]
        m = visited[nxt-1]
        break
if K < m: k = K
else:
    k = (K - m) % cycl + m

for i in range(N):
    if visited[i] == k:
        print(i+1)
        break
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k = 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 = len(checked) - len(loop)
if k <= dif:
    print(checked[k-1]+1)
else:
    k = k-dif
    print(loop[k%len(loop)]+1)

Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
4


Fixed submission (verdict: Accepted):
n,k = 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] = 1
    next_num = a[next_num]-1
dif = len(checked) - len(loop)
if k <= dif:
    print(checked[k]+1)
else:
    k = k-dif
    print(loop[k%len(loop)]+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

check = [0]*N
i = 0
c = 0
check = []
Flag = False
while c<=K:
    if i in check:
        flag = True
        break
    else:
        check.append(i)
        i = A[i]

X = check[check.index(i):]
print(1+X[(K-c)%len(X)] if flag==True else print(A[i]+1))

Failing test case input:
6 727202214173249351
4 5 2 3 3 3

Expected output:
3


Actual output:
5


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

check = [0]*N
i = 0
c = 1
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-check.index(i))%len(X)])  
else: 
    print(i+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N,K=[int(x) for x in input().rstrip().split()]
A=[int(x) for x in input().rstrip().split()]

stack=[]
done=[0 for i in range(N)]
done[0]=0
now=1
cnt=0

for i in range(2*(10**5)):
  done[now-1]+=1
  if done[now-1]==2:
    cnt+=1

  if done[now-1]==3:
    break
  
  stack.append(now)
  now=A[now-1]


ans=K%cnt
print(stack[ans])

Failing test case input:
6 727202214173249351
6 0 3 4 3 5

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):
N,K=[int(x) for x in input().rstrip().split()]
A=[int(x) for x in input().rstrip().split()]

stack=[]
done=[0 for i in range(2*(10**5))]
done[0]=0
now=0
dou=[]

while(done[now]<2):
  
  if done[now]==0:
    stack.append(now)
  else:
    dou.append(now)
  done[now]+=1
  now=A[now]-1

if K<len(stack):
  print(stack[K]+1)
else:
  print(dou[(K-len(stack)-len(dou))%len(dou)]+1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

check = [0]*N
i = 0
c = 0
check = []
Flag = False
while c<=K:
    if i in check:
        flag = True
        break
    else:
        check.append(i)
        i = A[i]

X = check[check.index(i):]
print(1+X[(K-c)%len(X)] if flag==True else print(i+1))

Failing test case input:
6 727202214173249351
4 5 2 3 3 3

Expected output:
3


Actual output:
5


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [i-1 for i in A]

check = [0]*N
i = 0
c = 1
check = []
visited = [0]*N
flag = False
while c<=K:
    if visited[i] == 1:
        flag = True
        break
    else:
        check.append(i)
        visited[i] = 1
        i = A[i]
        c += 1
if flag==True:
    X = check[check.index(i):]
    print(1+X[(K-check.index(i))%len(X)])  
else: 
    print(i+1)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N,K = map(int,input().split())
A = list(map(int,input().split()))

cnt = 0
x = 1
for i in range(N):
    y = A[x-1]
    cnt += 1
    if y == 1:
        break
    x = y

if N < K:
    s = K % cnt
else:
    s = K
x = 1
for i in range(s):
    y = A[x-1]
    x = y
print(x)

Failing test case input:
6 355145681216602327
4 5 1 5 2 3

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
N,K = map(int,input().split())
A = list(map(int,input().split()))

cnt = 0
dic = {1:0}
x = 1
for i in range(N):
    y = A[x-1]
    cnt += 1
    if y in dic:
        x = y        
        break
    else:
        dic[y] = cnt
        x = y

    if cnt == K:
        print(y)
        quit()
       
loop = (K-dic[x]) % (cnt - dic[x])

for i in range(loop):
    x = A[x-1]
print(x)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(lambda x: x-1, map(int, input().split())))
T = [False]*N

initcount = 0
count = 0
cur = 0
for _ in range(N):
    count += 1
    T[cur] = True
    cur = A[cur]
    if T[cur]:
        break
initcur = 0
for _ in range(count+1):
    initcount += 1
    initcur = A[initcur]
    if initcur == cur:
        break
if count != initcount:
    count -= initcount

if K <= initcount:
    cur = 0
    for _ in range(K):
        cur = A[cur]
    print(cur+1)
    print("aa")
else:
    K -= initcount
    count = K%count
    for _ in range(count):
        cur = A[cur]
    print(cur+1)



Failing test case input:
4 0
3 1 4 1

Expected output:
1


Actual output:
1
aa


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(lambda x: x-1, map(int, input().split())))
T = [False]*N

initcount = 0
count = 0
cur = 0
for _ in range(N):
    count += 1
    T[cur] = True
    cur = A[cur]
    if T[cur]:
        break
initcur = 0
for _ in range(count+1):
    initcount += 1
    initcur = A[initcur]
    if initcur == cur:
        break
if count != initcount:
    count -= initcount

if K <= initcount:
    cur = 0
    for _ in range(K):
        cur = A[cur]
    print(cur+1)
else:
    K -= initcount
    count = K%count
    for _ in range(count):
        cur = A[cur]
    print(cur+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))

t_zero = [-1] * N

t = 0
a = 1

while t_zero[a-1] == -1:
  if t == K:
    break
  t_zero[a-1] = t
  t += 1
  a = A[a-1]
  
else:
  k = (K - t_zero[a-1]) % (t - t_zero[a-1])
  a = A[t_zero[a-1] + k]
print(a)

Failing test case input:
6 2190427306125562200
6 4 3 2 3 2

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))

t_zero = [-1] * N

t = 0
a = 1

while t_zero[a-1] == -1:
  if t == K:
    break
  t_zero[a-1] = t
  t += 1
  a = A[a-1]
  
else:
  k = (K - t_zero[a-1]) % (t - t_zero[a-1])
  a = a = t_zero.index(t_zero[a-1] + k) + 1
print(a)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int,input().split())
a = list(map(int,input().split()))


visited = [0]*n
visited[0]=1
root=[0]

next=a[0]-1
same=0
cnt=0
while cnt<k:
    if visited[next]==1:

        same=next

        root.append(next)

        index = root.index(same)

        break
    else:
        visited[next]=1
        root.append(next)

    next = a[next]-1
    cnt+=1






if same!=next:
    print(next)
    exit(0)

roops=len(root) - index -1 

if roops!=0:
    moves = index + ( (k-index)%roops )
else:
    moves= index

now=0

for i in range(moves):
    now=a[now]-1

print(now+1)




Failing test case input:
4 0
3 0 4 1

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
a = list(map(int,input().split()))


visited = [0]*n
visited[0]=1
root=[0]

next=a[0]-1
same=0
cnt=0
while cnt<k:
    if visited[next]==1:

        same=next

        root.append(next)

        index = root.index(same)

        break
    else:
        visited[next]=1
        root.append(next)

    next = a[next]-1
    cnt+=1




if cnt==k:
    print(root[-1]+1)
    exit(0)

roops=len(root) - index -1 

if roops!=0:
    moves = index + ( (k-index)%roops )
else:
    moves= index

now=0

for i in range(moves):
    now=a[now]-1

print(now+1)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
a = [0] +  list(map(int,input().split()))

vlist = [1]
vcnt = [0]*(n+1)

v = 1
for i in range(1,n+1):
    v = a[v]
    vlist.append(v)
    if i == k:
        print(vlist[-1])
        exit()
    if vcnt[v] == 0 and v != 1:
        vcnt[v] += i
    else:
        syuuki = i - vcnt[v]
        hajime = i - syuuki
        m_start = (hajime + 1)%syuuki
        break

print(vlist[k%syuuki])



Failing test case input:
6 2190427306125562200
6 6 3 1 3 2

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
alist = list(map(lambda x:int(x) - 1,input().split()))
count = [0]*n
count[0] += 1
now = 0
order = [0]
for i in range(k):
    now = alist[now]
    count[now] += 1
    if count[now] == 2:
        start = order.index(now)
        loop = len(order[start:])
        syoki = len(order[:start])
        break
    order.append(now)
else:
    print(now+1)
    exit()
k -= syoki
k %= loop
print(order[k+syoki] + 1)
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys
n,k=map(int,input().split())
alist=list(map(int,input().split()))

vislist=[1]
visset={1}
loopflag=True
endflag=False
donek=0
ima=1

while loopflag:
    ima=alist[ima-1]
    if ima in visset:
        loopflag=False
        if ima == alist[ima-1]:
            endflag=True
    else:
        vislist.append(ima)
        visset.add(ima)

        donek+=1

        if donek == k:
            loopflag=False
            endflag=True

if endflag:
    print(ima)
    sys.exit()

loopstartindex=vislist.index(ima)
looplength=len(visset)-loopstartindex
print(vislist[loopstartindex+(k+1-loopstartindex)%looplength-1])



Failing test case input:
6 916742600949256759
6 5 0 4 2 2

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):
import sys
n,k=map(int,input().split())
alist=list(map(int,input().split()))

vislist=[1]
visset={1}
loopflag=True
endflag=False
donek=0
ima=1

while loopflag:
    ima=alist[ima-1]
    if ima in visset:
        loopflag=False
        if ima == alist[ima-1]:
            endflag=True
    else:
        vislist.append(ima)
        visset.add(ima)

        donek+=1

        if donek == k:
            loopflag=False
            endflag=True

if endflag:
    print(ima)
    sys.exit()

loopstartindex=vislist.index(ima)
looplength=len(visset)-loopstartindex
if (k+1-loopstartindex)%looplength == 0:
    print(vislist[-1])
else:
    print(vislist[loopstartindex+(k+1-loopstartindex)%looplength-1])

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
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):
                print(d[K])
            idx = d.index(s)
            K -= idx
            d = d[idx:]
            break
        d.append(s)
        d_set.add(s)
        s = A[s-1]

    
    K %= len(d)
    print(d[K])


if __name__ == '__main__':
    main()


Failing test case input:
4 0
3 1 4 1

Expected output:
1


Actual output:
1
1


Fixed submission (verdict: Accepted):
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
            d = d[idx:]
            break
        d.append(s)
        d_set.add(s)
        s = A[s-1]

    
    K %= len(d)
    print(d[K])


if __name__ == '__main__':
    main()
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
for i in range(n):
  temp=a[temp-1]
  result.append(temp)
temp=result.index(result[len(result)-1])
c=len(result)-temp-1
if k<n:
  print(result[k+1])
else:
  print(result[temp+(k-temp)%c])

Failing test case input:
4 0
3 2 1 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
for i in range(n):
  temp=a[temp-1]
  result.append(temp)
temp=result.index(result[len(result)-1])
c=len(result)-temp-1
if k<n:
  print(result[k])
else:
  print(result[temp+(k-temp)%c])

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

N,K = 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
    print(visit)
print(move)
print(loop)
if len(move)>K:
    print(move[K]+1)
else:
    print(loop[(K-(len(move)-len(loop)))%len(loop)]+1)


Failing test case input:
6 727202214173249351
4 5 2 5 3 2

Expected output:
5


Actual output:
[1, 0, 0, 0, 0, 0]
[1, 0, 0, 1, 0, 0]
[1, 0, 0, 1, 1, 0]
[1, 0, 1, 1, 1, 0]
[1, 1, 1, 1, 1, 0]
[1, 1, 1, 1, 2, 0]
[1, 1, 2, 1, 2, 0]
[1, 2, 2, 1, 2, 0]
[0, 3, 4, 2, 1]
[4, 2, 1]
5


Fixed submission (verdict: Accepted):

N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
loop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        loop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(loop[(K-(len(move)-len(loop)))%len(loop)]+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys
a,b=map(int,input().split())
b=b+1
l=[0]*(a)
List=list(map(int,input().split()))
i=0
j=1
while l[i]==0 and j<b:
  l[i]+=j
  j+=1
  i=List[i]-1
if j==b:
  print(List[i])
  sys.exit()
roop=j-l[i]
c=(b-j)%roop
for y in range(len(l)):
  if l[y] ==c+l[i]:
    print(y+1)
    sys.exit()

Failing test case input:
4 0
3 2 4 1

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
import sys
a,b=map(int,input().split())
b=b+1
l=[0]*(a)
List=list(map(int,input().split()))
i=0
j=1
while l[i]==0 and j<b:
  l[i]+=j
  j+=1
  i=List[i]-1
if j==b:
  print(i+1)
  sys.exit()
roop=j-l[i]
c=(b-j)%roop
for y in range(len(l)):
  if l[y]==c+l[i]:
    print(y+1)
    sys.exit()
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys
N,K = map(int,input().split())
As = list(map(int,input().split()))
towns = [0]*N
route = [0]
pre = 0
i = 0
for _ in range(N):
    i = As[i]-1
    if towns[i]>0:
        pre = route.index(i)
        route = route[pre:]
        break
    route.append(i)
    towns[i] = 1
else:
    print(i+1)
    sys.exit()
mod = len(route)
print(route[(K-pre)%mod]+1)

Failing test case input:
6 80635290846982474
6 4 1 5 3 2

Expected output:
5


Actual output:
1


Fixed submission (verdict: Accepted):
N,K = map(int,input().split())
As = list(map(int,input().split()))
towns = [0]*N
route = [0]
pre = 0
i = 0
towns[0] = 1
for _ in range(K):
    i = As[i]-1
    if towns[i]>0:
        pre = route.index(i)
        route = route[pre:]
        break
    route.append(i)
    towns[i] = 1
mod = len(route)
print(route[(K-pre)%mod]+1)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N ,K = map(int, input().split())
A = list(map(int, input().split()))
B = [0]
C = {} 
C[0] = 0
cnt = 0
jdg = 0
q = 0
while jdg == 0:
    i = A[q]-1
    q = i
    if q in C:
        l = C[q]
        
        break
    cnt += 1
    B.append(i)
    C[i] = cnt
r = cnt+1





H = B[l:r]
print(H) 
M = (K-l)%(r-l)

if K >= r:
	print(H[M]+1)
else:
  print(B[K]+1)

Failing test case input:
5 242746494311961546
3 1 1 5 2 2

Expected output:
1


Actual output:
[0, 2]
1


Fixed submission (verdict: Accepted):
N ,K = map(int, input().split())
A = list(map(int, input().split()))
B = [0]
C = {} 
C[0] = 0
cnt = 0
jdg = 0
q = 0
while jdg == 0:
    i = A[q]-1
    q = i
    if q in C:
        l = C[q]
        
        break
    cnt += 1
    B.append(i)
    C[i] = cnt
r = cnt+1





H = B[l:r]

M = (K-l)%(r-l)

if K >= r:
	print(H[M]+1)
else:
  print(B[K]+1)

Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,k=map(int,input().split())
A = list(map(int,input().split()))

C=[0]*n
C[0]=1
c=0
loopc=0
i=0
while i<n :
    newi = A[i]-1
    C[newi]+=1
    if C[newi]>=3 :
        rs=newi
        break
    elif C[newi]==2 :
        loopc+=1
    i=newi
    c+=1

mod = (k-(c-loopc))%(loopc+1)


i=rs
while mod>0 :
    newi = A[i]-1
    i=newi
    mod-=1

print( i+1 )


Failing test case input:
6 727202214173249351
4 5 0 5 1 1

Expected output:
5


Actual output:
4


Fixed submission (verdict: Accepted):
n,k=map(int,input().split())
A = list(map(int,input().split()))

C=[0]*n
C[0]=1
c=0
loopc=0
i=0
io=0
while c<k :
    newi = A[i]-1
    C[newi]+=1
    if C[newi]>=3 :
        rs=newi
        break
    elif C[newi]==2 :
        loopc+=1
    io=i
    i=newi
    c+=1

if (k-c)<0 or loopc==0 :
    print(i+1)
    exit()
mod = (k-c)%(loopc)




while mod>0 :
    newi = A[i]-1
    i=newi
    mod-=1

print( i+1 )
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, k = map(int, input().split())

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

city = 1
route = []
cnt = [0] * n
cnt[0] += 1
route.append(1)


while True:
    city = roads[city-1] 
    cnt[city-1] += 1
    if cnt[city-1] == 2:
        st = route.index(city)
        length = len(route) - st
        break
    route.append(city)

print(cnt)
print(route)
print(st)
print(length)

if k < len(route):
    print(route[k])

elif length >= 1:
    amari = (k+1-st) % length
    if amari  == 0:
        print(route[-1])
    else:
        print(route[amari+st-1])

Failing test case input:
4 5
3 2 4 1

Expected output:
4

Actual output:
[2, 0, 1, 1]
[1, 3, 4]
0
3
4


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())

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

city = 1
route = []
cnt = [0] * n
cnt[0] += 1
route.append(1)


while True:
    city = roads[city-1] 
    cnt[city-1] += 1
    if cnt[city-1] == 2:
        st = route.index(city)
        length = len(route) - st
        break
    route.append(city)


if k < len(route):
    print(route[k])

else:
    amari = (k+1-st) % length
    if amari  == 0:
        print(route[-1])
    else:
        print(route[amari+st-1])

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [val - 1 for val in A]

route = [0]
seen = {0}
here = 0
for _ in range(K):
    here = A[here]

    if here in seen:
        start = route.index(here)
        period = len(route) - start
        
        r = (K - start + 1) % period
        if r == 0:
            idx = period + start - 1
        else:
            idx = r + start - 1
        print(route[idx] + 1)
        break
    seen.add(here)
    route.append(here)
else:
    print(route[K])
    






Failing test case input:
4 0
3 1 4 1

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [val - 1 for val in A]

route = [0]
seen = set()
here = 0
for _ in range(N + 100):
    here = A[here]
    route.append(here)

if len(route) >= K:
    print(route[K] + 1)
else:
    for i in range(len(route)):
        if route[i] in seen:
            start = route.index(route[i])
            period = i - start
            
            r = (K - start + 1) % period
            if r == 0:
                idx = period + start - 1
            else:
                idx = r + start - 1
            print(route[idx] + 1)
            break
        seen.add(route[i])
Problem: p02684
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, K = map(int, input().split())
A = [x-1 for x in list(map(int, input().split()))]

current = 0
d = dict()
d[current] = d.get(current, 0) + 1
pattern = [0]
cnt = K

while True:
    current = A[current]
    if d.get(current) == 2:
        break
    else:
        if d.get(current) == None:
            d[current] = d.get(current, 0) + 1
        else:
            d[current] = d.get(current) + 1
        pattern.append(current)
        cnt -= 1
        if cnt == 0:
            break

if cnt == 0:
    print(pattern[-1]+1)
else:
    loop = [key for key, value in d.items() if value == 2]
    res = len(pattern) % len(loop)
    ans = (K - res) % len(loop)

    print(pattern[res+ans]+1)

Failing test case input:
6 116449238586591883
6 2 1 4 3 2

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n, K = map(int, input().split())
A = [x-1 for x in list(map(int, input().split()))]

current = 0
d = dict()
d[current] = d.get(current, 0) + 1
pattern = [0]
cnt = K

while True:
    current = A[current]
    if d.get(current) == 2:
        break
    else:
        if d.get(current) == None:
            d[current] = d.get(current, 0) + 1
        else:
            d[current] = d.get(current) + 1
        pattern.append(current)
        cnt -= 1
        if cnt == 0:
            break

if cnt == 0:
    print(pattern[-1]+1)
else:
    loop = len([key for key, value in d.items() if value == 2])
    not_loop = len([key for key, value in d.items() if value != 2])
    pattern = pattern[not_loop:]
    ans_position = (K - not_loop) % loop

    print(pattern[ans_position]+1)

Problem: p02684
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
path.append(1)
x = a[0]
path.append(x)
for i in range(n-2):
    x = a[x-1]
    path.append(x)

c = Counter(path)
roop_st = 0
for key, v in c.items():
    if v >= 2:
        roop_st = key
roop_st_idx = 0
roop_fin_idx = n-1
if roop_st != 0:
    roop_st_idx = path.index(roop_st)
    roop_fin_idx = path.index(roop_st, roop_st_idx+1)

k_mod = (k - roop_st_idx) % (roop_fin_idx - roop_st_idx)
print(path[roop_st_idx+k_mod])


Failing test case input:
6 237500909755776657
6 4 1 5 3 2

Expected output:
4


Actual output:
2


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []
x = 1
path.append(x)
for i in range(n):
    x = a[x-1]
    path.append(x)

count = [0]*n
roop_st = 0
roop_len = 0
for town in path:
    count[town-1] += 1
    if count[town-1] == 2:
        roop_st = town
        break
roop_st_idx = path.index(roop_st)
roop_fin_idx = path.index(roop_st, roop_st_idx+1)

if k >= roop_st_idx:
    k_mod = (k - roop_st_idx) % (roop_fin_idx - roop_st_idx)
    print(path[roop_st_idx + k_mod])
else:
    print(path[k])