#K56222. Valid Circular Chain

    ID: 30151 Type: Default 1000ms 256MiB

Valid Circular Chain

Valid Circular Chain

You are given a circular chain puzzle. The puzzle consists of a set of clues represented by a sequence of integers. Starting from the first element (index 0), each integer in the list points to the next position in the sequence (by 1-indexing). Your task is to determine whether the given clues form a valid circular chain. In other words, starting from the first element and following the clues exactly n times (where n is the number of clues), you should return to the starting position without visiting any element more than once (except after the last step to return to the start). If the above condition holds, print (YES); otherwise, print (NO).

inputFormat

The input is read from standard input (stdin).\nThe first line contains an integer (n) ((1 \leq n \leq 10^5)), denoting the number of clues.\nThe second line contains (n) space-separated integers (a_1, a_2, \dots, a_n) (each (1 \leq a_i \leq n)), where each (a_i) represents the next position in the chain (using 1-indexing).

outputFormat

Output a single line to standard output (stdout) containing (YES) if the clues form a valid circular chain, or (NO) otherwise.## sample

4
2 3 4 1
YES