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):
import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1

now = 0
if rep_start > K:
    for i in range(K+1):
        now = As[now]
    print(now+1)
    sys.exit()

# now = p

    # now = As[now]


K -= rep_start
K %= move
now = p


# print('now',now)

for i in range(K):
    print(now+1)
    now = As[now]

print(now+1)

import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

now = 0
# move = 1
nows = [0]
p = 0

# print('-----output-----')
while True:
    # move += 1
    now = As[now]
    if now in nows:
        p = now
        rep_start = len(nows)
        nows = nows[::-1]
        move = nows.index(now)+1
        break
    nows.append(now)

print('実行速度計測')

"""
now = 0
if rep_start > K:
    for i in range(K):
        now = As[now]
        print(now+1)
        sys.exit()

now = 0
for i in range(rep_start):
    now = As[now]


K -= rep_start
K %= move
now = p

for i in range(K):
    now = As[now]

print(now+1)
"""

import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1

now = 0
if rep_start > K:
    print(roots[K]+1)
    
        # now = As[now]
        # print(now+1)
        # sys.exit()

# now = p

    # now = As[now]


K -= rep_start
K %= move
now = p


# print('now',now)

for i in range(K):
    print(now+1)
    now = As[now]

print(now+1)

import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1

now = 0
# move = 1
nows = [0]
p = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        break
    visited[now] = 1

print('実行速度計測')

"""
now = 0
if rep_start > K:
    for i in range(K):
        now = As[now]
        print(now+1)
        sys.exit()

now = 0
for i in range(rep_start):
    now = As[now]


K -= rep_start
K %= move
now = p

for i in range(K):
    now = As[now]

print(now+1)
"""

import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1

now = 0
if rep_start > K:
    for i in range(K+1):
        now = As[now]
        print(now+1)
        sys.exit()

# now = p

    # now = As[now]


K -= rep_start
K %= move
now = p


# print('now',now)

for i in range(K):
    print(now+1)
    now = As[now]

print(now+1)

import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1

now = 0
if rep_start > K:
    for i in range(K):
        now = As[now]
    print(now+1)
    sys.exit()

# now = p

    # now = As[now]


K -= rep_start
K %= move
now = p


# print('now',now)

for i in range(K):
    print(now+1)
    now = As[now]

print(now+1)

import sys

# print('input >>')
N, K = map(int,(input().split()))
As = list(map(lambda x: int(x)-1,(input().split())))

visited = [0] * N
visited[0] = 1
roots = [0]
now = 0

# print('-----output-----')
while True:
    now = As[now]
    if visited[now] == 1:
        p = now
        rep_start = roots.index(now)
        move = len(roots) - rep_start
        break
    roots.append(now)
    visited[now] = 1

now = 0
if rep_start > K:
    for i in range(K):
        now = As[now]
    print(now+1)
    sys.exit()

# now = p

    # now = As[now]


K -= rep_start
K %= move
now = p


# print('now',now)

for i in range(K):
    now = As[now]

print(now+1)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted