Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GLIBCXX_DEBUG 0
- #include <iostream>
- #include <cassert>
- #include <algorithm>
- #include <vector>
- #include <unordered_map>
- #include "optimization.h"
- #include <map>
- #define all(a) a.begin, a.end()
- using namespace std;
- vector<vector<int>> children;
- int dfs(int root, int count){
- if (children[root].empty()){
- return count;
- }
- int temp = 0;
- for (auto x : children[root]){
- temp = max(dfs(x, count + 1), count);
- }
- count = max(temp, count);
- return count;
- }
- int main() {
- int n;
- cin >> n;
- children.resize(n);
- int root = -1;
- for (int i = 0; i < n; i++){
- int temp;
- cin >> temp;
- if (temp == -1){
- root = i;
- continue;
- }
- children[temp].push_back(i);
- }
- cout << dfs(root, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement