ac

芝士不拉丝  •  1年前


include <bits/stdc++.h>

using namespace std; const int N = 1000000+5; typedef long long ll; ll n; ll l[N]={0},r[N]={0}; ll a[N]; ll ans; bool check(ll x,ll y) {

if (x==0&&y==0)
    return true;
if (a[x]!=a[y]) 
    return false; 
return ((check(l[x],r[y])&&check(r[x],l[y]))); 

} void dfs(ll rt,ll&h,ll&sz) {

if (rt<=0) {
    h=0,sz=0; return; 
} 
ll h1,h2,sz1,sz2; 
dfs(l[rt],h1,sz1);
dfs(r[rt],h2,sz2); 
if (h1==h2&&sz1==sz2) 
    if (check(l[rt],r[rt]))
        ans=max(ans,sz1+sz2+1);
h=max(h1,h2)+1; 
sz=sz1+sz2+1; 

} int main() {

scanf("%lld",&n);
for (int i=1;i<=n;i++) 
    scanf("%lld",&a[i]);
for (int i=1;i<=n;i++) {
    scanf("%lld%lld",&l[i],&r[i]); 
    if (l[i]==-1) l[i]++;
    if (r[i]==-1) r[i]++;
} 
a[0]=-1; ans=1; 
ll h,sz; 
dfs(1,h,sz);
printf("%lld",ans);

}


评论:

请先登录,才能进行评论