luogu#P5018. [NOIP 2018 普及组] 对称二叉树

    ID: 4036 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>树形数据结构2018NOIP 普及组哈希 hashing

[NOIP 2018 普及组] 对称二叉树

Background

NOIP 2018 Junior Group T4.

Problem Description

A rooted tree with node weights is called a symmetric binary tree by Xuanxuan if it satisfies the following conditions:

  1. It is a binary tree.
  2. If we swap the left and right subtrees of every node in the tree, then in the new tree and the original tree, the structures at corresponding positions are the same and the node weights are equal.

In the figure below, the number inside each node is the weight, and the idid outside each node indicates the node index.

Now you are given a binary tree. You need to find a subtree that is a symmetric binary tree and has the maximum number of nodes. Output the number of nodes in that subtree.

Note: A tree with only the root is also a symmetric binary tree. In this problem, a “subtree” rooted at node TT means the binary tree formed by node TT and all of its descendants.

Input Format

The first line contains a positive integer nn, representing the number of nodes in the given tree. The node indices are 1n1 \sim n, and node 11 is the root.

The second line contains nn positive integers separated by spaces. The ii-th positive integer viv_i represents the weight of node ii.

The next nn lines each contain two integers li,ril_i, r_i, representing the indices of the left and right children of node ii, respectively. If the left/right child does not exist, it is represented by 1-1. The two numbers are separated by a space.

Output Format

Output one line containing an integer, representing the maximum number of nodes among all symmetric binary subtrees of the given tree.

2 
1 3 
2 -1 
-1 -1 

1
10 
2 2 5 5 5 5 4 4 2 3 
9 10 
-1 -1 
-1 -1 
-1 -1 
-1 -1 
-1 2 
3 4 
5 6 
-1 -1 
7 8
3

Hint

Sample 1 Explanation


The largest symmetric binary subtree is the subtree rooted at node 22, and the number of nodes is 11.

Sample 2 Explanation

The largest symmetric binary subtree is the subtree rooted at node 77, and the number of nodes is 33.

Constraints

There are 2525 test points in total.

vi1000v_i \le 1000.

  • Test points 131 \sim 3, n10n \le 10. It is guaranteed that all nodes in the left subtree of the root have no right child, and all nodes in the right subtree of the root have no left child.
  • Test points 484 \sim 8, n10n \le 10.
  • Test points 9129 \sim 12, n105n \le 10^5. It is guaranteed that the input is a “full binary tree”.
  • Test points 131613 \sim 16, n105n \le 10^5. It is guaranteed that the input is a “complete binary tree”.
  • Test points 172017 \sim 20, n105n \le 10^5. It is guaranteed that all node weights in the input tree are 11.
  • Test points 212521 \sim 25, n106n \le 10^6.

In this problem, the following definitions apply:

Level: The level of nodes starts from the root. The root is on level 1, and the children of the root are on level 2. The level of any node in the tree equals the level of its parent node plus 11.

Depth of a tree: The maximum level of nodes in the tree is called the depth of the tree.

Full binary tree: Suppose the depth of a binary tree is hh, and the binary tree has 2h12^h - 1 nodes. Then it is a full binary tree.

Complete binary tree: Suppose the depth of a binary tree is hh. Except for level hh, the number of nodes on all other levels reaches the maximum. All nodes on level hh are continuously packed to the far left. Then it is a complete binary tree.

Translated by ChatGPT 5