kolbka_

Untitled

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