A conveyor is modeled as a singly linked list of n nodes indexed 0..n-1 from the head. Normally the last node points to nothing, but a maintenance splice may connect the tail's next back to an earlier node, creating an endless loop. You are told the 0-indexed node the tail connects to, or -1 if the tail connects to nothing.
Decide whether following next pointers from the head ever revisits a node (a cycle).
Input format
Line 1: an integer n, the number of nodes.
Line 2: n space-separated integers, the node values from head to tail (empty line when n is 0).
Line 3: an integer target. If target == -1 the tail points to nothing; otherwise 0 <= target <= n-1 and the tail's next is set to the node at index target.
Output format
Print YES if the list contains a cycle, otherwise print NO.
Constraints
- 0 <= n <= 100000
- -1000000000 <= each value <= 1000000000
target == -1, or0 <= target <= n-1- When
n == 0,targetis-1.