Problem: The Kingdom of Takahashi has N towns, numbered 1 through N.  There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.  Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.

Submission attempts (in order):

def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()


tmps=[1] 
loops=[] 
count=0 

for i in range(2*(10**5)+1):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
	elif A[count] in loops:
		break
	count=A[count]-1

tmps=tmps[:-(len(loops))]
print(tmps)
print(loops)

if k<=len(tmps):
	print(tmps[k-1])
else:
	ans=(k-(len(tmps)))%len(loops)
	print(loops[ans])


def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()


judge=[True]*n
tmps=[1] 
count=0 

for i in range(2*(10**5)+1):
	if judge[count]==True:
		judge[count]=False
		count=A[count]-1
		tmps.append(count+1)
	else:
		break

loop_st=tmps[-1]
loops=tmps[loop_st:-1]


if k<loop_st:
	print(tmps[k])
else:
	k=k-loop_st
	k%=len(loops)
	print(loops[k])


def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()


tmps=[1] 
loops=[] 
st=0 が始まる地点
count=0 

for i in range(10**5):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
		st=i
	else:
		break
	count=A[count]-1


print(tmps)
print(loops)
print(st)

if k<=st:
	print(tmps[k-1])
else:
	ans=(k-(st-1))%len(loops)
	print(ans)
	print(loops[ans])

n,k=input2()
A=input_array()


tmps=[1] 
loops=[] 
st=0 が始まる地点
count=0 

for i in range(10**5):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
		st=i
	else:
		break
	count=A[count]-1


if k<=st:
	print(tmps[k-1])
else:
	ans=(k-(st-1))%len(loops)
	print(loops[ans])


def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()


tmps=[1] 
loops=[] 
st=0 が始まる地点
count=0 

for i in range(2*(10**5)+1):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
		st=i
	elif A[count] in loops:
		break
	count=A[count]-1



if k<=st:
	print(tmps[k-1])
else:
	ans=(k-(st-1))%len(loops)
	print(loops[ans])


def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()


tmps=[1] 
loops=[] 
st=0 が始まる地点
count=0 

for i in range(10**5):
	if A[count] not in tmps:
		tmps.append(A[count])
	elif (A[count] in tmps) and (A[count] not in loops):
		loops.append(A[count])
		st=i
	else:
		break
	count=A[count]-1




if k<st:
	print(tmps[k])
else:
	ans=(k-(st-1))%len(loops)
	print(loops[ans])


def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))
  
n,k=input2()
A=input_array()


judge=[True]*n
tmps=[1] 
count=0 

for i in range(2*(10**5)+1):
	if judge[count]==True:
		judge[count]=False
		count=A[count]-1
		tmps.append(count+1)
	else:
		break

loop_st=tmps.index(tmps[-1])
loops=tmps[loop_st:-1]


if k<loop_st:
	print(tmps[k])
else:
	k=k-loop_st
	k%=len(loops)
	print(loops[k])

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Accepted