Created
October 2, 2019 21:29
-
-
Save sofhiasouza/fb0dfc6f95d1e275023656370ef672ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int maxn = 2e3+10; //valor maximo de n | |
int n; | |
vector < int > grafo[maxn]; | |
int dfs(int u) | |
{ | |
int r = 1; | |
for(int i = 0 ; i < grafo[u].size() ; i++) //chamo pros filhos de u | |
{ | |
int v = grafo[u][i]; | |
r = max(r, 1+dfs(v)); //pego a maior altura entre os meus filhos | |
} | |
return r;//retorno a maior altura da subarvore de u | |
} | |
int main() | |
{ | |
cin >> n; | |
vector < int > aux; //vetor onde guardarei as raizes das arvores (os vertices que nao possuem pai) | |
for(int i = 1 ; i <= n ; i++) | |
{ | |
int x; | |
cin >> x; //pai de i | |
if(x == -1) aux.push_back(i); //se nao possui pai, coloco no vetor aux | |
else grafo[x].push_back(i); //se possui pai, me conecto ao meu pai | |
} | |
int resp = 0; | |
for(int i = 0 ; i < aux.size() ; i++) | |
{ | |
int u = aux[i]; | |
resp = max(resp, dfs(u)); //salvo em resp a maior altura entre a que eu ja tinha e a que calculp agora, de u | |
} | |
cout << resp << "\n"; //imprimo a resposta | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment