RT,求调
#include <bits/stdc++.h>
using namespace std ;
const int maxn = 1e6 + 5 ;
int n ;
int point[maxn] ;
int tree[maxn][2] ; // 左0右1
int ans ;
int sum[maxn] ; // sum[i]表示以i为根的节点总数
bool check(int rootx,int rooty) {
// cout << rootx << " " << rooty << endl ;
// system("pause") ;
if(rootx==-1&&rooty==-1) {
ans = max(ans,1) ;
return 1 ;
}
if(rootx!=-1&&rooty!=-1&&check(tree[rootx][1],tree[rooty][0])&&(check(tree[rootx][0],tree[rooty][1]))&&(point[rootx]==point[rooty])) {
ans = max(ans,sum[rootx]+sum[rooty]+1) ;
return 1 ;
}
if((rootx!=-1)&&check(tree[rootx][0],tree[rootx][1])) {
ans = max(ans,sum[rootx]) ;
}
if((rooty!=-1)&&check(tree[rooty][0],tree[rooty][1])) {
ans = max(ans,sum[rooty]) ;
}
return 0 ;
}
void init(int root) {
if(tree[root][0]==-1&&tree[root][1]==-1) {
sum[root] = 1 ;
return ;
}
if(tree[root][0]!=-1)
init(tree[root][0]) ;
if(tree[root][1]!=-1)
init(tree[root][1]) ;
sum[root] = sum[tree[root][0]] + sum[tree[root][1]] + 1 ;
return ;
}
int main() {
scanf("%d",&n) ;
for ( int i = 1 ; i <= n ; ++ i ) {
scanf("%d",point+i) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
scanf("%d%d",&tree[i][0],&tree[i][1]) ;
}
init(1) ;
// for ( int i = 1 ; i <= n ; ++ i ) {
// cout << sum[i] << " " ;
// }
if(check(tree[1][0],tree[1][1])) ans = sum[1] ;
cout << ans ;
}