```cpp
#include<bits/stdc++.h>
using namespace std;
int ans;
int cnt;
struct node{
int val, l, r;
}; node tree[1000005];
bool flag;
void dfs(int x){
cnt++;
if(tree[tree[x].l].val == 0 and tree[tree[x].r].val == 0){
// ans = max(ans, cnt);
return ;
}
if(tree[tree[x].l].val != tree[tree[x].r].val){
flag = false;
return ;
}
dfs(tree[x].l);
dfs(tree[x].r);
}
int main(){
int n;
cin>>n;
for(int i = 1; i <= n; i++){
cin>>tree[i].val;
}
for(int i = 1; i <= n; i++){
cin>>tree[i].l>>tree[i].r;
if(tree[i].l == -1) tree[i].l = 0;
if(tree[i].r == -1) tree[i].r = 0;
}
for(int i = 1; i <= n; i++){
cnt = 0;
flag = true;
dfs(i);
if(flag) ans = max(ans, cnt);
// cout<<cnt<<endl;
// ans = max(cnt, ans);
}
cout<<ans<<endl;
return 0;
}
```
by WRuperD @ 2021-08-28 08:54:30
@[God_Panda](/user/247388) 左孩子和右孩子的权值可以不一样
by Sharing666 @ 2021-08-28 09:07:22
比如题目描述中的第一个例子
by Sharing666 @ 2021-08-28 09:08:35
@[Sharing666](/user/394991) thx
by WRuperD @ 2021-08-28 10:02:15