fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int N,count=0,X;
  5. scanf("%d",&N);
  6. int A[N+1];
  7. for(int i=2;i<N+1;i++){
  8. scanf("%d",&A[i]);
  9. }
  10. scanf("%d",&X);
  11.  
  12. while(A[X] > 0){
  13. X = A[X];
  14. count++;
  15. }
  16. printf("%d",count);
  17. return 0;
  18. }
  19.  
Success #stdin #stdout 0s 5284KB
stdin
9
1 1 2 2 3 3 4 8
9
stdout
4