#include
#include
#include
using namespace std;
const int maxn=5005;
vectorgraph[maxn];
int arr[maxn];
int parent[maxn];
int dp[maxn];
int dfs(int pos){
if(dp[pos]!=-1){
return dp[pos];
}
int r1=arr[pos];
for(int i=0; i>n>>arr[0];
int a, b;
for(int i=1; i>arr[i]>>b;
a=i;
parent[i]=b;
graph[b].push_back(i);
}
memset(dp, -1, sizeof dp);
cout<