Symmetrical binary solution to a problem Luogu P5018

Foreword

date: 2019-11-15 CSP1 day before

Last year, as a still immature players to participate OIer NOIP2018, and feel like experiencing an era of great change, mind feeling great.

It is ignorant, confused mood to answer in the examination room. Know today to open this topic, I found myself once how Shui, this question did not even take the full points.

(Later recalled barely even search all, it makes sense can be considered)

topic

 

The right to have a little bit of the root of the tree if the following conditions are met Xuan Xuan were called symmetric binary tree:

 

  1. Binary tree;
  2. These left and right subtrees of all tree nodes exchange, the same tree structure of the new and original tree corresponding positions and points of equal power.

 

The numbers in the figure is the node weights, the outer nodes  id  represents the node number.

Now we are given a binary tree, I hope you find it a sub-tree, the tree is symmetrical sub binary tree, and a maximum number of nodes. Please tree output sub-tree nodes.

Note: Only the roots of the trees are symmetrical binary tree. In this problem convention, the node  T  a sub-root of the "subtree" means: node T  binary tree and all of its successor nodes.

 

Input Format

 

The first line of a positive integer  n- , represents a given number of nodes in the tree, a predetermined node number  . 1 ~ n, where node  . 1 1 is a root.

The second row  of n positive integers, separated by a space, the  i-th positive integers  V i  representative node i  weights.

Next  n lines of two positive integers  L i , R & lt i , respectively, node  i  is the number of children left. If the left / right child does not exist,  - 1 represents. Separated by a space between the two numbers.

 

Output Format

 

Total output file row contains an integer indicating the maximum tree given two symmetrical fork tree nodes.

analysis

1. How to build a binary tree:

(That goes without saying?) Recursive DFS

  Each node parameter set 1 l, r represents the number of its left child node;

  Each node 2 is provided on its size parameter indicates the number of child nodes (including itself);

  Each node apparatus 3 is represented in parameter val weights

Wherein, the input can be done in 1,3, 2 can be done (after recursive processing statistics) by recursively

2. How to judge a sub-tree is not "symmetrical binary tree":

  If the two sons 1 nodes do not exist, the current is symmetrical binary tree;

  2 if and only one son node exists, the current is not symmetrical binary tree;

  3 If the node exists but two sons weights do not match, the current is not symmetrical binary tree;

  4 If the two sons node exists and whether the right values ​​match, the further examination of the left and right son of the left son of the right son of the son of symmetry, if his son left his son's left and right and the right son of the son of symmetry, are symmetrical if the current is symmetrical binary tree, otherwise it is not. (Recursion)

3. How to deal with the answer:

When the tree subtree is "symmetrical binary tree" when we update the answer to its maximum value and size of the current node (homeopathy)

detail

1. Each node must take size value of 1

2. Note that certain leaf node is "symmetrical binary"

3. Be sure not to use scanf (The reason I do not know, just because I submitted with scanf is Wrong Answer is Memory Limited Errow, then changed cin on the AC)

4. The initial value assigned must take the answer is 1 (there is always a binary tree leaf nodes)

Code

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int n,res=1;//赋值
struct node//节点结构体
{
    int l,r,size,val;
}a[1000005];
inline bool judge(int lef,int rig)//判断一当前节点为根节点的子树是否为“对称二叉树”
{
    if(lef==-1 and rig==-1)//叶子节点
        return 1;
    if(lef==-1 or rig==-1)//独生子女
        return 0;
    if(a[lef].val^a[rig].val)//左右孩子权值不相等
        return 0;
    return judge(a[lef].l,a[rig].r)&judge(a[lef].r,a[rig].l);//递归检查
}
inline void dfs(int k)
{
    if(a[k].l^-1)//向左建树
    {
        dfs(a[k].l);
        a[k].size+=a[a[k].l].size;
    }
    if(a[k].r^-1)//向右建树
    {
        dfs(a[k].r);
        a[k].size+=a[a[k].r].size;
    }
    if(judge(a[k].l,a[k].r))
        res=max(res,a[k].size);//更新答案
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(register int i=1;i<=n;i++)
        cin>>a[i].val,a[i].size=1;
    for(register int i=1;i<=n;i++)
        cin>>a[i].l>>a[i].r;
    dfs(1);//建树兼更新答案
    cout<<res<<endl;
    return 0;
}

 

to sum up

Exam on the last day, I wish all OIer RP ++, AK audience!

Guess you like

Origin www.cnblogs.com/chengyurui/p/11865463.html