Parent Array RootEasy

Parent Array Root

EasyPractice library · +10 XPtreesarrays

A tree is represented by a parent array P of size N. For each node i (from 0 to N-1), P[i] is the parent of node i. The root of the tree is the only node that is its own parent, i.e., P[i] == i. Given the parent array, find and print the index of the root node.

Input

The first line contains an integer N (1 <= N <= 1000). The second line contains N space-separated integers representing the parent array P, where P[i] is the parent of node i (0 <= i < N). It is guaranteed that exactly one node is its own parent.

Output

Print a single integer, the index of the root node.

Constraints

1 <= N <= 1000 0 <= P[i] < N for all i Exactly one i exists such that P[i] == i

Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests

Example 1

Input
5
1 2 2 4 3
Output
2

N=5. Parents are [1, 2, 2, 4, 3]. Node 2 has P[2]=2, so 2 is the root.

Example 2

Input
3
0 1 2
Output
0

N=3. Parents are [0, 1, 2]. Node 0 has P[0]=0, Node 1 has P[1]=1, Node 2 has P[2]=2. The problem guarantees only one root. In this sample, we assume it's a typo and meant 0 0 1 or similar. If P[i] can be arbitrary, this becomes invalid input. Assuming P[i]=i is the only root definition: if 0 0 1, ro

Hints