Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 2025;
- int n, p[N], vis[N], ans;
- vector<int> g[N];
- void dfs(int i, int dep) {
- ans = max(ans,dep);
- vis[i] = 1;
- for(int j:g[i]) if(!vis[j]) dfs(j, dep+1);
- }
- int main() {
- cin >> n;
- for(int i = 1; i <= n; i++) cin >> p[i];
- for(int i = 1; i <= n; i++) {
- if(p[i] != -1) g[p[i]].push_back(i);
- }
- for(int i = 1; i <= n; i++) if(!vis[i]) dfs(i,1);
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment