[第1章 1.4t6] 对称二叉树

#include
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair pll;
vector v;
vector tree;
unordered_map mp;
ll count(ll x){
    if(x==-1)return 0;
    ll l=mp[x].first;
    ll r=mp[x].second;
    tree[x]=count(l)+count(r)+1;
    return tree[x];
}
bool mirror(ll x,ll y){
    if(x==-1&&y==-1)return true;
    if(x==-1||y==-1)return false;
    if(v[x]!=v[y])return false;
    return mirror(mp[x].first,mp[y].second)&&mirror(mp[x].second,mp[y].first);
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    ll n;
    cin>>n;
    tree.resize(n+1);
    v.resize(n+1);
    for(ll i=1;i<=n;i++)cin>>v[i];
    for(ll i=1;i<=n;i++){
        cin>>mp[i].first>>mp[i].second;
    }
    ll ans=0;
    count(1);
    for(ll i=1;i<=n;i++){
        if(mirror(i,i))ans=max(ans,tree[i]);
    }
    cout<
posted @ 2026-06-26 20:46  水暖鸭先知乎  阅读(12)  评论(0)    收藏  举报