当前位置:网站首页>洛谷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;
}
转载请说明出处
边栏推荐
猜你喜欢

一文了解微分段应用场景与实现机制

Accelerating strategy learning using parallel differentiable simulation

Exercise 6-2 using functions to sum special A-string sequences

7-15 calculation of PI

GRPC的四种数据流以及案例

剑指 Offer 28. 对称的二叉树

Exercise 10-8 recursive implementation of sequential output of integers

Redis: redis data structure and key operation commands

Redis:Redis的数据结构、key的操作命令

Leetcode(4)——寻找两个正序数组的中位数
随机推荐
7-28 monkeys choose King (Joseph problem)
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
Redis: commandes d'action pour les données de type chaîne
JVM object lifecycle
7-14 sum integer segments (C language)
7-9 find a small ball with a balance
7-16 find the set of integers that meet the given conditions
LNMP环境mail函数不能发送邮件解决
6-9 statistics of single digits (15 points)
Why is this error reported when modifying records in the database
Generate directories from web content
Sendmail can't send mail and it's too slow to send. Solve it
Solve the problem of dormitory router campus network sharing login
Why don't I have a rookie medal
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
JS Part III
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Print. JS -- web page file printing
How to bold text in AI
Leetcode(4)——尋找兩個正序數組的中比特數
