Solve ABC167-D in Python

Introduction

Solve D that could not be solved yesterday

ABC167-DTeleporter

** Thoughts ** Since $ K $ is big, it seems that it can not be solved unless it is looped somewhere → Let's detect the loop. And it will be classified according to the size of $ K $. All you need to implement is a list to determine if you are visiting and a list to record the order of visits. The former list prevents infinite loops and the latter list facilitates later implementations.

n, k = map(int,input().split())
a = list(map(int,input().split()))

s = [True] * n #List to determine if you are visiting
c = [1] #List that records the order of visits
now = 0 #Current location
while s[now]:
    s[now] = False #Record the points you visited
    now = a[now] - 1
    c.append(now+1)

start_cycle = c.index(c[-1]) #Point to enter the loop
loop = c[start_cycle:-1] #Loop list
cycle = len(loop)

if k < start_cycle: #K unable to reach the loop
    print(c[k])
else:
    k -= start_cycle
    k %= cycle
    print(loop[k])

Summary

If I was calm, I was disappointed because I could solve it during the contest. see you.

Recommended Posts

Solve ABC167-D in Python
Solve ABC159-D in Python
Solve ABC146-C in Python
Solve ABC098-C in Python
Solve ABC169 in Python
Solve ABC160-E in Python
Solve ABC176 E in Python
Solve Wooldridge exercises in Python
Solve ABC175 D in Python
[Python] ABC175D
Solve optimization problems in Python
Solve Atcoder ABC169 A-D in Python
Solve ABC036 A ~ C in Python
Solve ABC037 A ~ C in Python
Solve ordinary differential equations in Python
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
[Python] DP ABC184D
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
[Python] UnionFind ABC177D
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
ABC 157 D --Solve Friend Suggestions in Python!
I wanted to solve ABC159 in Python