Untitled
unknown
python
2 years ago
767 B
5
Indexable
def solve(arr, a, b): set2 = set() map = {} t = a c = 1 while True: if t in map: break if t == -1: break map[t] = c c += 1 t = arr[t] t = b j = 0 minimum = float('inf') ans = -1 while True: if t in set2: break if t == -1: break if map[t] == 0: j += 1 set2.add(t) t = arr[t] continue summation = map[t] + j if summation < minimum: minimum = summation ans = t set2.add(t) t = arr[t] return ans n = int(input()) arr = list(map(int, input().split())) a, b = map(int, input().split()) print(solve(arr, a, b))
Editor is loading...