Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 99999999
int N,B[21]={};
int solve(int num){
int mx=-INF,mn=INF;
for(int i=2;i<=N;i++){
if(B[i]==num){
int tmp=solve(i);
mx=max(tmp,mx);
mn=min(tmp,mn);
}
}
if(abs(mx)==INF) mx=0;
if(abs(mn)==INF) mn=0;
return mx+mn+1;
}
signed main() {
cin>>N;
for(int i=2;i<=N;i++) cin>>B[i];
cout<<solve(1)<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment