Created
May 10, 2020 23:42
-
-
Save monhime/08571eb19b785faaca43d5da1209cc5b to your computer and use it in GitHub Desktop.
ABC167 D問題 解答
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def input(): return sys.stdin.readline().rstrip() | |
def main(): | |
n,k=map(int,input().split()) | |
A=list(map(int,input().split())) | |
if A[0]==1: | |
print(1) | |
sys.exit() | |
cur_a=1 | |
cunt=0 | |
visited=[False]*n | |
visited[0]=True | |
route=[1] | |
for _ in range(k): | |
next_a=A[cur_a-1] | |
if visited[next_a-1]: | |
loop=len(route)-route.index(next_a) | |
cunt-=loop-1 | |
break | |
cur_a=next_a | |
cunt+=1 | |
route.append(cur_a) | |
visited[cur_a-1]=True | |
else: | |
print(next_a) | |
sys.exit() | |
#print(route) | |
#rint(cunt,loop) | |
k-=cunt | |
k=k%loop | |
#print(k) | |
print(route[cunt+k]) | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment