40分求救
查看原帖
40分求救
338147
01bit楼主2020/10/27 21:25
#include<cstdio>
#include<algorithm>
using namespace std;
int n,v[1000001],l[1000001],r[1000001];
bool issf[1000001]={};
int sftree(int root){
	if(root==-1)return 0;
	if(l[root]==-1&&r[root]==-1){issf[root]=true;return 1;}
	int sfl=sftree(l[root]),sfr=sftree(r[root]);
	if(v[l[root]]==v[r[root]]&&issf[l[root]]&&issf[r[root]]){issf[root]=true;return sfl+sfr+1;}
	return sfl>sfr?sfl:sfr;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",v+i);
	for(int i=1;i<=n;i++)scanf("%d%d",l+i,r+i);
	printf("%d",sftree(1));
	return 0;
}
2020/10/27 21:25
加载中...