#P3816. [FJOI2017] 树的平均路长问题
[FJOI2017] 树的平均路长问题
Description
The internal path length of a tree is defined as:
where ranges over the internal nodes of , and is the length of the path from the root to node .
A red-black tree is a special binary search tree in which each node is colored red or black. If we regard null pointers in a binary search tree as pointing to an empty node, we call these empty nodes the external nodes of the BST. We stipulate that all external nodes have height . All nodes other than external nodes are internal nodes.
A red-black tree is a colored binary search tree that satisfies the following red-black properties:
- Every node is either red or black.
- Every external node is black.
- The children of any red node are black.
- Every path from any node to its descendant external nodes contains the same number of black nodes.
For any node in a red-black tree, the number of black nodes on any path from (excluding ) down to an external node is called the black height of node , denoted . The black height of a red-black tree is the black height of its root.
Given a positive integer , design an algorithm to compute, among all red-black trees with nodes, the maximum possible internal path length.
Input Format
Multiple test cases. Each line contains a single positive integer (), which is the number of nodes in the red-black tree. The input is terminated by a line containing a single .
Output Format
For each given , output the maximum internal path length on a separate line. Do not output any extra content.
1
2
3
0
1
3
5
0
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号