#include<bits/stdc++.h>
using namespace std;
int n,t;
struct tree{
int l,r,v;
}a[1000001];
int ans;
bool pd(int x, int y) {
if (x==-1&&y==-1)
return 1;
if (x==-1||y==-1||a[x].v!=a[y].v)
return 0;
return pd(a[x].l, a[y].r)&&pd(a[x].r, a[y].l);
}
int dp(int x){
if(x==-1)return 0;
else return dp(a[x].l)+dp(a[x].r)+1;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].v;
}
for(int i=1;i<=n;i++){
cin>>a[i].l>>a[i].r;
}
for(int i=1;i<=n;i++){
if(pd(a[i].l,a[i].r))ans=max(ans,dp(i));
}
cout<<ans;
return 0;
}
看题解看到的方法,因为我递归太烂了,直接暴力。
先对每个节点为根的子树进行对称判断,若是对称树,就进行节点数计算,最后取最大即可
Couldn’t be happier with the ease of getting B29.bet from this site. Fast download and great performance. Get yours now: tải b29.bet
A Beth Nacional tá bombando! Sempre vejo gente falando bem e experimentei pra ver se era tudo isso mesmo. E ó, aprovei! Dá uma olhada lá: beth nacional
Want the best battles? You can find them at dagadonc1 org. Come on over! đá gà đòn c1. I know you’ll have fun!