Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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<int64_t>> children;
- int64_t dfs(int64_t root){
- if (children[root].empty()){
- return 1;
- }
- int64_t temp = 0;
- for (auto x : children[root]) {
- temp = max(dfs(x) + 1, temp);
- }
- return temp;
- }
- int main() {
- int64_t n = readInt();
- // cin >> n;
- children.resize(n);
- int64_t root = -1;
- for (int64_t i = 0; i < n; i++){
- int64_t temp = readInt();
- // cin >> temp;
- if (temp == -1){
- root = i;
- continue;
- }
- children[temp].push_back(i);
- }
- writeInt(dfs(root));
- }
Advertisement
Add Comment
Please, Sign In to add comment