fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include<vector>
  5.  
  6. int n = 0;
  7. int r = 0;
  8.  
  9. int max(int a, int b){
  10. return a > b ? a : b;
  11. }
  12. int Hight(int r, int *mas){
  13. int hight = 1;
  14. vector<int> children;
  15. for(int i = 0; i < n; ++i){
  16. if(mas[i] == r){
  17. children.push_back(i);
  18. }
  19. }
  20.  
  21. for(int i =0; i < children.size(); ++i){
  22. hight = max(hight, Hight(children[i], mas) + 1);
  23. }
  24. return hight;
  25. }
  26. int main(){
  27. cin>>n;
  28.  
  29. int *mas = new int[n];
  30. for(int i = 0; i < n; ++i){
  31. cin>>mas[i];
  32. if(mas[i] == -1)
  33. r = i;
  34. }
  35.  
  36. cout<<Hight(r, mas);
  37.  
  38.  
  39.  
  40. }
Success #stdin #stdout 0s 4332KB
stdin
5
4
-1
4
1
1
stdout
3