题面
https://www.luogu.org/problem/P5018
题解
#include<cstdio> #include<iostream> using namespace std; struct node { int val,lson,rson; } a[1050000]; int siz[1050000]; int n; void dfs(int x){ siz[x]=1; if (a[x].lson!=-1) dfs(a[x].lson),siz[x]+=siz[a[x].lson]; if (a[x].rson!=-1) dfs(a[x].rson),siz[x]+=siz[a[x].rson]; } bool can(int u,int v){ if (u==-1 && v==-1) return true; if (u!=-1 && v!=-1) { if (a[u].val==a[v].val && can(a[u].lson,a[v].rson) && can(a[u].rson,a[v].lson)) return true; else return false; } return false; } int main(){ int i; scanf("%d",&n); for (i=1;i<=n;i++) scanf("%d",&a[i].val); for (i=1;i<=n;i++) scanf("%d %d",&a[i].lson,&a[i].rson); dfs(1); int ans=0; for (i=1;i<=n;i++) if (can(a[i].lson,a[i].rson)) ans=max(ans,siz[i]); printf("%d ",ans); }