-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvolution.cpp
37 lines (35 loc) · 890 Bytes
/
Evolution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
#include<iostream>
using namespace std;
void dfs(vector<vector<int>> &v, int height[], bool visited[], int i){
visited[i]=true;
for(int j=0; j<v[i].size(); j++){
dfs(v, height, visited, v[i][j]);
height[i] = max(height[i], 1+ height[ v[i][j] ]);
}
}
int main(){
int n, i, x;
cin>>n;
vector<int> c;
vector<vector<int>> v(n, c);
int height[n];
bool visited[n];
for(i=0; i<n; i++) {
cin>>x;
height[i]=1;
visited[i]=false;
if(x==-1) continue;
v[--x].push_back(i);
}
for(i=0; i<n; i++){
if(!visited[i]){
dfs(v, height, visited, i);
}
}
int res=0;
for(i=0; i<n; i++){
res = max(res, height[i]);
}
cout<<res<<endl;
}