Advertisement
kolbka_

Untitled

Dec 16th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #define _GLIBCXX_DEBUG 0
  5. #include <iostream>
  6. #include <cassert>
  7. #include <algorithm>
  8. #include <vector>
  9. #include <unordered_map>
  10. #include "optimization.h"
  11. #include <map>
  12.  
  13. #define all(a) a.begin, a.end()
  14. using namespace std;
  15. vector<vector<int>> children;
  16. int dfs(int root, int count){
  17. if (children[root].empty()){
  18. return count;
  19. }
  20. int temp = 0;
  21. for (auto x : children[root]){
  22. temp = max(dfs(x, count + 1), count);
  23. }
  24. count = max(temp, count);
  25. return count;
  26. }
  27. int main() {
  28. int n;
  29. cin >> n;
  30. children.resize(n);
  31. int root = -1;
  32. for (int i = 0; i < n; i++){
  33. int temp;
  34. cin >> temp;
  35. if (temp == -1){
  36. root = i;
  37. continue;
  38. }
  39. children[temp].push_back(i);
  40. }
  41. cout << dfs(root, 1);
  42. }
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement