$P5018 对称二叉树$
2024-08-25 06:51:41
一直忘记给这个题写题解了。
这题挺水的吧。
挺后悔当时没写出来。
#ifdef Dubug
#endif
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
inline LL In() { LL res(0),f(1); register char c ;
while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : 0 ;
while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ;
return res * f ;
}
int n ;
const int N = 1e6 + 5 ;
int a[N] ;
int Left[N] , Right[N] ;
int sum = 1 ;
inline bool dfs(int x,int y) {//搜索能否有对称。
if(x == -1 and y == -1) return 1 ;
if(x == -1 or y == -1 or a[x] != a[y]) return 0 ;
sum += 2 ;
return (dfs(Left[x],Right[y]) and dfs(Left[y],Right[x])) ;
}
signed main() {
n = In() ;
for(register int i=1;i<=n;i++) a[i] = In() ;
for(register int i=1;i<=n;i++) Left[i] = In() , Right[i] = In() ;
int ans = -0x7f ;
for(register int i=1;i<=n;i++) {//每个枚举一遍。n个节点。
sum = 1 ;//初始化
if(dfs(Left[i],Right[i])) ans = max(ans,sum) ;//如果有对称 那么更新ans
}
cout << ans << endl ;
return 0 ;
}
最新文章
- python文件操作
- windows系统激活-使用微软官方公布的kms client setup key安装或安装后使用slmgr导入
- PowerDesigner导出建表sql脚本
- 九幽2015年Q3 WP市场份额细分报告
- Hibernate设置派生属性(formula)
- Linux磁盘分区实战案例
- 网页在Safari快速滚动和回弹的原理: -webkit-overflow-scrolling : touch;的实现
- SQL SERVER大话存储结构(4)_复合索引与包含索引
- slf4j-logback 日志以json格式导入ELK
- 内存管理单元--MMU
- ajax跨域例子
- Java-接口(interface)
- 函数防抖 debounce
- Python中【__all__】的用法
- Qsys 设计流程---Qsys System Design Tutorial
- mysql中参数--init-file的作用是什么呢?
- 迷你MVVM框架 avalonjs 1.2.4发布
- Oracle 表空间、段、区和块简述
- QT学习笔记2:QT中常用函数
- POJ 3057 Evacuation 二分+最大流