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

Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值

Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions

7-7 12-24 hour system

玖逸云黑免费无加密版本源码

Programming language: the essence of type system

JS first summary

Solution to failure or slow downloading of electron when electron uses electron builder to package

Scroll detection, so that the content in the lower right corner is not displayed at the top of the page, but is displayed as the mouse slides

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

Exercise 10-2 recursive factorial sum
随机推荐
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
Strategy, tactics (and OKR)
Toast UI editor (editor allows you to edit your markup document using text or WYSIWYG, with syntax highlighting, scrolling synchronization, real-time preview and chart functions.)
GRPC的四种数据流以及案例
npm install卡住与node-npy的各种奇怪报错
使用并行可微模拟加速策略学习
Understanding of closures
Article content typesetting and code highlighting
etcd集群权限管理和账号密码使用
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
concat和concat_ws()区别及group_concat()和repeat()函数的使用
Statistical capital consonants
How Facebook moves instagram from AWS to its own server
战略、战术(和 OKR)
7-24 reduction of the simplest fraction (rolling Division)
常见问题之PHP——ldap_add(): Add: Undefined attribute type in
7-17 crawling worms (break exercise)
Interface for querying IP home
6-9 statistics of single digits (15 points)
7-2 and then what time (15 minutes)
