Parent Array Root
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
Example 1
5 1 2 2 4 3
2
N=5. Parents are [1, 2, 2, 4, 3]. Node 2 has P[2]=2, so 2 is the root.
Example 2
3 0 1 2
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
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 1 2 2 4 3
3 0 1 2