Contest1082 - 20210817

2021-08-17 12:00:00
2021-08-31 16:00:00
Finished Public Server Current Time:2024-05-17 14:15:22

Contest Information

2018复赛


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
ll l,r,val;
}bt[1000010];
bool same(ll now1,ll now2){//判断是否对称
if(now1==-1 && now2==-1) return true;
if(now1==-1 || now2==-1) return false;//小技巧
if(bt[now1].val!=bt[now2].val) return false;
return same(bt[now1].l,bt[now2].r)&&same(bt[now1].r,bt[now2].l);//递归的判断左右子树相等
}
int count(ll now){//递归的对左右子树计数
return now==-1?0:count(bt[now].l)+count(bt[now].r)+1;
}
int main(){
int n,ans=0;
cin>>n;
for(int i=1;i<=n;i++) cin>>bt[i].val;
for(int i=1;i<=n;i++) cin>>bt[i].l>>bt[i].r;
for(int i=1;i<=n;i++)/*枚举每棵子树*/ 
if(same(bt[i].l,bt[i].r)) ans=max(ans,count(i));
printf("%d",ans);
return 0;
}



Problem ID Title AC Submit Num