Problem p02684 (python)

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

MAX_N=2*10**5
b=[1]
visited=[-1]*(n+10)
for i in range(1,2*MAX_N+1):
    nxt=a[b[-1]]
    b.append(nxt)
        
if k<=n:
    print(b[k])
    exit()
# print(b)  
for i,bi in enumerate(b):
    if visited[bi]==-1:
        visited[bi]=i
    else:
        m=i-visited[bi]
        l=visited[bi]
        break
k=k%m

while k<n:
    k+=m
print(b[k])

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=[q-1 for q in LI()]

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

if k<=2*10**5:
    for i in range(k):
        nxt=a[now]
        now=nxt
    print(now)
    exit()

for i in range(k):
    nxt=a[now]
    if visited[nxt]==-1:
        visited[nxt]=i
    else:
        m=i-visited[nxt]
        break
for i in range(k%m+m):
    nxt=a[now]
    now=nxt
print(now)
exit()

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=[q-1 for q in LI()]

visited=[-1]*n
now=0
visited[now]=0

if k<=2*10**5:
    for i in range(k):
        nxt=a[now]
        now=nxt
    print(now)
    exit()

for i in range(k):
    nxt=a[now]
    now=nxt
    if visited[nxt]==-1:
        visited[nxt]=i
    else:
        m=i-visited[nxt]
        break
now=0
for i in range(k%m+m):
    nxt=a[now]
    now=nxt
print(now)
exit()

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=LI()
b=[1]
visited=[-1]*n
visited[0]=0
# print(visited)

for i in range(k):
    # print(visited)
    nxt=a[b[-1]-1]
    b.append(nxt)
    if visited[nxt-1]==-1:
        visited[nxt-1]=i+1
    else:
        mod=(visited[nxt-1],i+1)
        break
    
# print(visited,b,mod)

ml,mr=mod
shuuki=mr-ml
k=k%shuuki+shuuki
# print(k)

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

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=LI()
b=[]
visited=[-1]*n


now=1
for i in range(k):
    nxt=a[now-1]
    b.append(nxt)
    if visited[nxt-1]==-1:
        visited[nxt-1]=i+1
    else:
        mod=(visited[nxt-1],i+1)
        break
    now=nxt
    # visited[now]
    # print(i,now)
# print(now,mod)

ml,mr=mod
k=(k-ml)%(mr-ml)
if k==0:
    k+=(mr-ml)
k+=ml
# print(k)

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

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=LI()
b=[]
visited=[-1]*n

now=1
for i in range(k):
    nxt=a[now-1]
    b.append(nxt)
    if visited[nxt-1]==-1:
        visited[nxt-1]=i+1
    else:
        mod=(visited[nxt-1],i+1)
        break
    now=nxt

ml,mr=mod
k=(k-ml)%(mr-ml)+ml
print(k)

print(b[k-1])

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
# from collections import Counter
# from functools import *

def LI():
    return list(map(int,input().split()))

n,k=LI()
a=LI()
b=[]
visited=[-1]*n


now=1
for i in range(k):
    nxt=a[now-1]
    b.append(nxt)
    if visited[nxt-1]==-1:
        visited[nxt-1]=i
    else:
        mod=(visited[nxt-1],i)
        break
    now=nxt
    # visited[now]
    # print(i,now)
# print(now,mod)

ml,mr=mod
k=(k-ml)%(mr-ml)+ml
# print(k)

# now=1
# for i in range(k):
#     now=a[now-1]
# print(now)
print(b[k-1])