@[Fang_Junyi](/user/215713)
看不懂你在干什么。
```cpp
#include <bits/stdc++.h>
#define count sfhis
using namespace std;
const int MAX_N=1000005;
int n;
int val[MAX_N];
int lson[MAX_N],rson[MAX_N];
int siz[MAX_N];
int ans=0;
int count(int rt)
{
if(rt==-1)return 0;
else return siz[rt]=1+count(lson[rt])+count(rson[rt]);
}
bool check(int u,int v)
{
if(u==-1 && v==-1)return true;
if(u==-1 || v==-1)return false;
if(siz[u]!=siz[v] || val[u]!=val[v])return false;
return check(lson[u],rson[v]) && check(rson[u],lson[v]);
}
void dfs(int rt)
{
if(ans>=siz[rt])return;
if(check(rt,rt))ans=siz[rt];
dfs(lson[rt]);
dfs(rson[rt]);
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>val[i];
for(int i=1;i<=n;i++)cin>>lson[i]>>rson[i];
count(1);
dfs(1);
cout<<ans<<endl;
return 0;
}
```
by Smile_Cindy @ 2020-02-08 14:06:16