#include #include #include class TreeHeight { int n; int root; std::vector parent; public: void read() { std::cin >> n; parent.resize(n); for (int i = 0; i < n; i++) { std::cin >> parent[i]; if (parent[i] < 0) root = i; } /* std::cout << root << std::endl; */ } int compute_height() { std::vector heights(n); for (int vertex = 0; vertex < n; vertex++) { if (heights[vertex] != 0) continue; int height = 0; for (int i = vertex; i != -1; i = parent[i]) { height++; if (height > heights[i]) heights[i] = height; else break; } } return heights[root]; } }; int main() { std::ios_base::sync_with_stdio(0); TreeHeight tree; tree.read(); std::cout << tree.compute_height() << std::endl; }