Cow pedigrees (△)

Source: Internet
Author: User

Thought of DP

However, I did not think of the multiplication principle.

Set the height of a binary tree to K, where the number of nodes is n

The number of possible Binary Trees

F [k] [N] = 2 * summation (F [k-1] [r] * smaller [k-1] [n-1-R]) + summation (F [k-1] [r] * f [k-1] [n-1-R])

R ranges from 1 to n.

The first indicates that the Left subtree height is K-1, the right subtree height is smaller than the K-1, multiplied by 2, indicates the situation of the left and right subtree swap.

The second indicates that the height of the left and right subtree is K-1

Note the calculation of smaller,Smaller [k] [N]It indicates the possible type of child number with a smaller height than K and N number of nodes.

Usaco analysis:


This is a DP problem. the properties of a tree that we are interested in are depth and number of nodes, so we'll make a table: table [I] [J] contains the number of trees with depth I and number of nodes J. given the constraints of the task, J must be odd. how do you construct a tree? From smaller trees, of course. A tree of depth I and j nodes will be constructed from two smaller trees and one more node.

With I and j already chosen, we chose K, which is the number of nodes in the left subtree. then the number of nodes in the right subtree is known, j-k-1. for depth, at least one subtree has to have depth I-1 so that the new made tree wowould have depth I. there are three possibilities: the left subtree can have depth I-1 and the depth of the right subtree can be smaller, the right subtree can have depth I-1 and the depth of the Left subtree can be smaller, or they can both have depth I-1.

The truth is that once we are constructing trees of depth I, we use smaller trees, but we only care if those are of depth I-1 or smaller. so, let another array, smalltrees [I-2] [J] contain number of trees of any depth smaller than I-1, not just I-2. now, knowing all this, We Contruct our tree from three possible ways:

table[i][j] += smalltrees[i-2][k]*table[i-1][j-1-k];                  // left subtree smaller than i-1, right is i-1table[i][j] += table[i-1][k]*smalltrees[i-2][j-1-k];                  // left subtree is i-1, right smallertable[i][j] += table[i-1][k]*table[i-1][j-1-k];                  // both i-1 

In addition, if the number of nodes in the left subtree is smaller then the number of nodes in the left subtree, we can count the tree twice, as different tree can be constructed by swapping left and right subtree.

Total running time is O (K * n ^ 2), with very favorable constant factor.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.