蒟蒻求助

P5018 [NOIP2018 普及组] 对称二叉树

92太强辣%%%%
by clockwhite @ 2018-11-11 09:41:31


```c++ #include<iostream> #include<cstdio> using namespace std; int n,ans,f[1000001],l[1000001],r[1000001],s; bool check(int x,int y){ if(f[x]!=f[y])return false; if(r[x]!=-1&&l[y]!=-1){ if(!check(r[x],l[y]))return false; }else if(!(r[x]==-1&&l[y]==-1))return false; if(l[x]!=-1&&r[y]!=-1){ if(!check(l[x],r[y]))return false; }else if(!(l[x]==-1&&r[y]==-1))return false; return true; } void find(int x){ s++; if(l[x]!=-1)find(l[x]); if(r[x]!=-1)find(r[x]); } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&f[i]); for(int i=1;i<=n;i++)scanf("%d %d",&l[i],&r[i]); for(int i=1;i<=n;i++){ if(check(i,i)){ s=0; find(i); ans=max(ans,s); } } printf("%d",ans); } ```
by suyiheng @ 2018-11-11 09:44:02


emmmm?这不是$n^2$的吗?
by longlongzhu123 @ 2018-11-11 09:44:16


100分
by suyiheng @ 2018-11-11 09:44:55


?为什么
by longlongzhu123 @ 2018-11-11 09:45:37


还有,为什么哈希做不出来?
by longlongzhu123 @ 2018-11-11 09:47:31


你这个代码输入 1 1 -1 -1 会输出0 但它的答案是1
by suyiheng @ 2018-11-11 09:50:37


废龙你看看你这一句
by suyiheng @ 2018-11-11 09:53:12


```c++ if((x==-1&&y!=1)||(x!=-1&&y==1))return false; ```
by suyiheng @ 2018-11-11 09:53:36


不应该是
by suyiheng @ 2018-11-11 09:53:56


| 下一页