当前位置:网站首页>Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
2022-07-03 14:27:00 【q779】
Luogu P5018 [NOIP2018 Popularization group ] Symmetric binary tree Answer key
Topic link :P5018 [NOIP2018 Popularization group ] Symmetric binary tree
The question :
If a rooted tree with some weight meets the following conditions , Is called symmetric binary tree by Xuanxuan :
- Binary tree ;
- Swap the left and right subtrees of all nodes of this tree , The structure of the corresponding position of the new tree and the original tree is the same and the point weight is equal .
The number in the node in the figure below is the weight , Out of node i d id id Indicates the node number .
Now give a binary tree , I hope you find a subtree of it , The subtree is a symmetric binary tree , And the number of nodes most . Please output the number of nodes of this subtree .
Be careful : A tree with only its roots is also a symmetric binary tree . It is agreed in this question , To the node T T T A tree that is the root of a child tree “ Son Trees ” refer to : node T T T And all its descendant nodes .
【 Data scale and agreement 】
common 25 25 25 A test point .
v i ≤ 1000 v_i ≤ 1000 vi≤1000.
Test point 1 ∼ 3 , n ≤ 10 1 \sim 3, n ≤ 10 1∼3,n≤10, Ensure that all nodes of the left subtree of the root node have no right children , Right of root node All nodes of the subtree have no left children .
Test point 4 ∼ 8 , n ≤ 10 4 \sim 8, n ≤ 10 4∼8,n≤10.
Test point 9 ∼ 12 , n ≤ 1 0 5 9 \sim 12, n ≤ 10^5 9∼12,n≤105, Ensure that the input is a “ Full binary tree ” .
Test point 13 ∼ 16 , n ≤ 1 0 5 13 \sim 16, n ≤ 10^5 13∼16,n≤105, Ensure that the input is a “ Perfect binary tree ”.
Test point 17 ∼ 20 , n ≤ 1 0 5 17 \sim 20, n ≤ 10^5 17∼20,n≤105, Ensure that the point weight of the input tree is 1 1 1.
Test point 21 ∼ 25 , n ≤ 1 0 6 21 \sim 25, n ≤ 10^6 21∼25,n≤106.
For every point of direct violence dfs Its left and right subtrees can be judged
Pay attention to the direction of symmetry and don't write it wrong
Time complexity O ( n log n ) O(n \log n) O(nlogn)
Why won't the chain get stuck , Because the intermediate judgment is equivalent to pruning off
Only the complete binary tree will arrive O ( n log n ) O(n \log n) O(nlogn)
Code :
#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;
}
Reprint please explain the source
边栏推荐
- Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
- Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
- SSH访问控制,多次失败登录即封掉IP,防止暴力破解
- Exercise 6-6 use a function to output an integer in reverse order
- 7-11 calculation of residential water charges by sections
- Exercise 8-2 calculate the sum and difference of two numbers
- Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
- Exercise 9-3 plane vector addition
- Sendmail can't send mail and it's too slow to send. Solve it
- Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
猜你喜欢

论文分享:Generating Playful Palettes from Images

必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿

Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million

Leetcode (4) -- find the median of two positively ordered arrays

Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them

Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion

关于回溯问题中的排列问题的思考(LeetCode46题与47题)

Exercise 8-8 moving letters

使用并行可微模拟加速策略学习

Leetcode(4)——寻找两个正序数组的中位数
随机推荐
fpga阻塞赋值和非阻塞赋值
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Eight sorts
Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
Exercise 10-1 judge the three digits that meet the conditions
Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions
Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
Strategy, tactics (and OKR)
Exercise 10-8 recursive implementation of sequential output of integers
虽然不一定最优秀,但一定是最努力的!
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
玖逸云黑免费无加密版本源码
洛谷P3065 [USACO12DEC]First! G 题解
Thread.sleep和TimeUnit.SECONDS.sleep的区别
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
Mongodb index
Exercise 8-8 moving letters
