当前位置:网站首页>洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
2022-07-03 14:15:00 【q779】
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
题目链接:P5018 [NOIP2018 普及组] 对称二叉树
题意:
一棵有点权的有根树如果满足以下条件,则被轩轩称为对称二叉树:
- 二叉树;
- 将这棵树所有节点的左右子树交换,新树和原树对应位置的结构相同且点权相等。
下图中节点内的数字为权值,节点外的 i d id id 表示节点编号。
现在给出一棵二叉树,希望你找出它的一棵子树,该子树为对称二叉树,且节点数 最多。请输出这棵子树的节点数。
注意:只有树根的树也是对称二叉树。本题中约定,以节点 T T T 为子树根的一棵“子 树”指的是:节点 T T T 和它的全部后代节点构成的二叉树。
【数据规模与约定】
共 25 25 25 个测试点。
v i ≤ 1000 v_i ≤ 1000 vi≤1000。
测试点 1 ∼ 3 , n ≤ 10 1 \sim 3, n ≤ 10 1∼3,n≤10,保证根结点的左子树的所有节点都没有右孩子,根结点的右 子树的所有节点都没有左孩子。
测试点 4 ∼ 8 , n ≤ 10 4 \sim 8, n ≤ 10 4∼8,n≤10。
测试点 9 ∼ 12 , n ≤ 1 0 5 9 \sim 12, n ≤ 10^5 9∼12,n≤105,保证输入是一棵“满二叉树” 。
测试点 13 ∼ 16 , n ≤ 1 0 5 13 \sim 16, n ≤ 10^5 13∼16,n≤105,保证输入是一棵“完全二叉树”。
测试点 17 ∼ 20 , n ≤ 1 0 5 17 \sim 20, n ≤ 10^5 17∼20,n≤105,保证输入的树的点权均为 1 1 1。
测试点 21 ∼ 25 , n ≤ 1 0 6 21 \sim 25, n ≤ 10^6 21∼25,n≤106。
对于每个点直接暴力dfs它的左右子树判断即可
注意对称的方向不要写错了
时间复杂度 O ( n log n ) O(n \log n) O(nlogn)
为什么链不会被卡呢,因为中间判断的时候已经相当于剪枝掉了
只有完全二叉树才会到 O ( n log n ) O(n \log n) O(nlogn)
代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(1e6+15)
int n,ch[N][2],val[N],sz[N];
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
void dfs(int u)
{
sz[u]=1;
if(ls(u)!=-1)
{
dfs(ls(u));
sz[u]+=sz[ls(u)];
}
if(rs(u)!=-1)
{
dfs(rs(u));
sz[u]+=sz[rs(u)];
}
}
bool solve(int u,int v)
{
if(u==-1&&v==-1)return 1;
if(u==-1||v==-1)return 0;
return val[u]==val[v]&&solve(ls(u),rs(v))&&solve(rs(u),ls(v));
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n;
for(int i=1; i<=n; i++)
cin >> val[i];
for(int i=1; i<=n; i++)
cin >> ls(i) >> rs(i);
dfs(1);
int ans=0;
for(int i=1; i<=n; i++)
if(solve(ls(i),rs(i)))
ans=max(ans,sz[i]);
cout << ans << '\n';
return 0;
}
转载请说明出处
边栏推荐
- 7-22 tortoise and rabbit race (result oriented)
- MongoDB数据库入门的常用命令
- Exercise 8-7 string sorting
- 7-6 mixed type data format input
- 如何查询淘宝天猫的宝贝类目
- Redis: commandes d'action pour les données de type chaîne
- Message subscription and publishing
- Exercise 6-6 use a function to output an integer in reverse order
- Leetcode (4) -- find the median of two positively ordered arrays
- Statistical capital consonants
猜你喜欢
GRPC的四种数据流以及案例
7-9 find a small ball with a balance
Exercise 10-3 recursive implementation of exponential functions
Exercise 8-8 moving letters
Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
Jiuyi cloud black free encryption free version source code
TS code automatically generates JS
编程语言:类型系统的本质
使用并行可微模拟加速策略学习
Exercise 9-3 plane vector addition
随机推荐
7-19 check denomination (solve binary linear equation)
String sort
Recent learning summary
7-23 currency conversion (using array conversion)
7-15 calculation of PI
Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
FPGA test method takes mentor tool as an example
6-9 statistics of single digits (15 points)
Ultra simple mobile map development
适用于XP的DDK
Stop asking yourself if you are suitable for software testing
Canvas utility library fabric JS user manual
JVM垃圾回收机
GRPC的四种数据流以及案例
Exercise 9-1 time conversion
Statistical capital consonants
Find specified characters
常见问题之PHP——ldap_add(): Add: Undefined attribute type in
7-11 calculation of residential water charges by sections
7-20 print 99 formula table (format output)