这题数据换行有毒吧

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

@[_自动AC机_](/space/show?uid=84991) 但不重选的话样例我是读不完的
by Episode9 @ 2018-11-15 22:10:11


@[GREED_VI](/space/show?uid=37409) 你的电脑怎么那么神奇······
by _自动AC机_ @ 2018-11-15 22:11:27


@[GREED_VI](/space/show?uid=37409) Win8?
by _自动AC机_ @ 2018-11-15 22:11:46


@[_自动AC机_](/space/show?uid=84991) win10
by Episode9 @ 2018-11-15 22:12:58


@[_自动AC机_](/space/show?uid=84991) 放评测机也一样
by Episode9 @ 2018-11-15 22:13:13


![](https://cdn.luogu.com.cn/upload/pic/43595.png)
by Episode9 @ 2018-11-15 22:14:24


@[GREED_VI](/space/show?uid=37409) 那你的电脑是真的神奇了,我也是Win10,不知道为何你的电脑这么秀
by _自动AC机_ @ 2018-11-15 22:17:59


代码搁这了,求救啊啊啊啊 ```cpp #include<bits/stdc++.h> #define MAXN 1000005 using namespace std; struct node{int l_son,r_son;}; node tree[MAXN]; int n,val[MAXN],ans=0; inline int dfs(int l,int r) { if ((tree[l].l_son!=-1&&tree[l].r_son!=-1)&&(tree[r].l_son!=-1&&tree[r].r_son!=-1)&&val[tree[l].l_son]==val[tree[r].r_son]&&val[tree[l].r_son]==val[tree[r].l_son]) return dfs(tree[l].l_son,tree[r].r_son)+1+dfs(tree[l].r_son,tree[r].l_son); if (tree[l].l_son!=-1&&tree[r].r_son!=-1&&tree[l].r_son==-1&&tree[r].l_son==-1&&val[tree[l].l_son]==val[tree[r].r_son]) return dfs(tree[l].l_son,tree[r].r_son)+1; if (tree[l].r_son!=-1&&tree[r].l_son!=-1&&tree[l].l_son==-1&&tree[r].r_son==-1&&val[tree[l].r_son]==val[tree[r].l_son]) return dfs(tree[l].r_son,tree[r].l_son)+1; bool flag=1; if (!(tree[tree[l].l_son].l_son!=-1&&tree[tree[r].r_son].r_son!=-1)) flag=0; if (!(tree[tree[l].r_son].l_son!=-1&&tree[tree[r].l_son].r_son!=-1)) flag=0; if (!(tree[tree[l].r_son].r_son!=-1&&tree[tree[r].l_son].l_son!=-1)) flag=0; if (!(tree[tree[l].r_son].l_son!=-1&&tree[tree[r].l_son].r_son!=-1)) flag=0; if (flag==0) return -9999999; } int main() { cin>>n; for (int i=1;i<=n;i++) cin>>val[i]; for (int i=1;i<=n;i++) cin>>tree[i].l_son>>tree[i].r_son; for (int i=1;i<=n;i++) { int sum=dfs(i,i); if (sum>ans) ans=sum; } cout<<ans*2+1<<endl; return 0; } ```
by Episode9 @ 2018-11-15 22:21:20


以上程序不保证正确性,只是想询问输入
by Episode9 @ 2018-11-16 16:00:19


```cpp #include<bits/stdc++.h> #define MAXN 1000005 using namespace std; struct node{int l_son,r_son;}; node tree[MAXN]; int n,val[MAXN],ans=0; inline int dfs(int l,int r) { if (val[l]!=val[r]) return -9999999; if ((tree[l].l_son!=-1&&tree[l].r_son!=-1)&&(tree[r].l_son!=-1&&tree[r].r_son!=-1)) return dfs(tree[l].l_son,tree[r].r_son)+1+dfs(tree[l].r_son,tree[r].l_son); if (tree[l].l_son!=-1&&tree[r].r_son!=-1&&tree[l].r_son==-1&&tree[r].l_son==-1) return dfs(tree[l].l_son,tree[r].r_son)+1; if (tree[l].r_son!=-1&&tree[r].l_son!=-1&&tree[l].l_son==-1&&tree[r].r_son==-1) return dfs(tree[l].r_son,tree[r].l_son)+1; bool flag=1; if (!((tree[tree[l].l_son].l_son!=-1&&tree[tree[r].r_son].r_son!=-1)||(tree[tree[l].l_son].l_son==-1&&tree[tree[r].r_son].r_son==-1))) flag=0; if (!((tree[tree[l].r_son].l_son!=-1&&tree[tree[r].l_son].r_son!=-1)||(tree[tree[l].r_son].l_son==-1&&tree[tree[r].l_son].r_son==-1))) flag=0; if (!((tree[tree[l].r_son].r_son!=-1&&tree[tree[r].l_son].l_son!=-1)||(tree[tree[l].r_son].r_son==-1&&tree[tree[r].l_son].l_son==-1))) flag=0; if (!((tree[tree[l].r_son].l_son!=-1&&tree[tree[r].l_son].r_son!=-1)||(tree[tree[l].r_son].l_son==-1&&tree[tree[r].l_son].r_son==-1))) flag=0; if (flag==0) return -9999999; } int main() { cin>>n; for (int i=1;i<=n;i++) cin>>val[i]; for (int i=1;i<=n;i++) cin>>tree[i].l_son>>tree[i].r_son; for (int i=1;i<=n;i++) { int sum=dfs(i,i); if (sum>ans) ans=sum; } cout<<ans*2+1<<endl; return 0; } ``` 这是对的代码,手测
by Episode9 @ 2018-11-16 20:53:34


上一页 | 下一页