记录编号 549718 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 3052.[NOIP 2018PJ]对称二叉树 最终得分 100
用户昵称 夜莺 是否通过 通过
代码语言 C++ 运行时间 1.624 s
提交时间 2020-02-21 13:57:42 内存使用 5.05 MiB
显示代码纯文本
#include<cstdio>
#define N 1000010
using namespace std;
int n,v[N],l[N],r[N],son[N],ans=0;
inline int read(){
	int x=0,f=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		f=(c=='-')?1:f;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-'0';
		c=getchar();
	}
	return f?-x:x;
}
bool pd(int x,int y){
	if (x==-1&&y==-1)return 1;
	if (x==-1||y==-1)return 0;
	if (v[x]!=v[y])return 0;
	return pd(l[x],r[y])&pd(r[x],l[y]);
}
void dfs(int x){
	son[x]=1;
	if (l[x]!=-1)dfs(l[x]),son[x]+=son[l[x]];
	if (r[x]!=-1)dfs(r[x]),son[x]+=son[r[x]];
	if (son[x]>ans&&pd(l[x],r[x]))ans=son[x];
}

int main(){
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++)
		v[i]=read();
	for(int i=1;i<=n;i++){
		l[i]=read();
		r[i]=read();
	}
	dfs(1);
	printf("%d",ans);
	return 0;
}