搞忘Markdown了
```
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<set>
#include<map>
#include<string>
#include<vector>
#define N 1000005
#define ls(x) tree[x].s[0]
#define rs(x) tree[x].s[1]
#define sz(x) tree[x].sz
#define w(x) tree[x].w
using namespace std;
struct Node{
int s[3],w,sz;
}tree[N];
int n,ans=1;
void dfs1(int x,int fa){
tree[x].sz=1;
if(ls(x)!=-1){
dfs1(ls(x),x);
sz(x)+=sz(ls(x));
}
if(rs(x)!=-1){
dfs1(rs(x),x);
sz(x)+=sz(rs(x));
}
}
bool dfs2(int x,int y){
if(sz(x)!=sz(y))return 0;
if(w(x)!=w(y))return 0;
if((rs(y)==-1)!=(ls(x)==-1))return 0;
if((rs(x)==-1)!=(ls(y)==-1))return 0;
if(ls(x)!=-1&&!dfs2(ls(x),rs(y)))return 0;
if(rs(x)!=-1&&!dfs2(rs(x),ls(y)))return 0;
return 1;
}
inline int read(){
int f=1,w=0;
char ch=getchar();
while(ch>'9'||ch<'0'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
w*=10,w+=ch-'0';
ch=getchar();
}
return f*w;
}
int main(){
// freopen("tree.in","r",stdin);
// freopen("tree.out","w",stdout);
n=read();
for(int i=1;i<=n;i++)
w(i)=read();
for(int i=1;i<=n;i++)
ls(i)=read(),rs(i)=read();
dfs1(1,0);
for(int i=1;i<=n;i++){
if(sz(i)<ans)continue;
if(ls(i)!=-1&&rs(i)!=-1&&dfs2(ls(i),rs(i)))
ans=max(ans,sz(i));
}
printf("%d",ans);
return 0;
}
```
by jiangby @ 2018-11-11 22:53:04
这道题 要不是我没时间做
by resftlmuttmotw @ 2018-11-11 23:03:40
@[卖报纸就找我](/space/show?uid=75982) 为什么可以dfs不爆栈啊...
根结点的两侧都是一条长5e5(-1)的链
感觉这样栈空间必爆啊?
by Kirisame_Marisa_ @ 2018-11-12 11:19:59
希望更丰富的展现?使用Markdown
by Zimo @ 2018-11-12 13:53:35
如果爆栈就凉凉了
by 立花瀧 @ 2018-11-12 22:39:18
担心啊
by 立花瀧 @ 2018-11-12 22:39:25