Official

E - Overlap Binary Tree Editorial by evima


The problem is trivial when \(N=1\). Below, let \(3 \leq N\).

For now, let’s consider the case without the fourth condition.

[1] Restatement of the problem

Replace \(N\) with \(\frac{N+1}{2}\) and consider a rooted binary tree with \(N\) leaves.

For a rooted binary tree \(T\) that distinguishes between left and right child vertices, consider writing the interval \([L,R]\) on each vertex so that the following conditions are met:

  • The interval written on vertex \(v\) intersects with all the intervals written on the descendants of vertex \(v\).
  • For an interval \([L,R]\) written on a descendant of the left child of vertex \(v\) and an interval \([L',R']\) written on a descendant of the right child, \(R<L'\).

After writing the intervals, we can obtain the sequence of integer pairs that satisfy the conditions by labeling the intervals as \(1,2,\dots,N\) in ascending order of \(L\). Let us consider constructing \(T\) that satisfies the third condition for a sequence of integer pairs obtained in this way. The interval corresponding to the root intersects with all intervals, and this interval is unique. Since the way of dividing the remaining intervals into left and right subtrees is also unique, it can be inductively seen that there is a unique \(T\) that satisfies the conditions. In other words, the same sequence of integer pairs will not be created more than once.

Therefore, all we need to do is find the sum of the number of ways to write intervals over all ordered rooted binary trees \(T\) with \(N\) leaves.

[2] Counting the ways to write intervals

Let us use insertion DP to calculate \(dp[v]:\) the number of possible magnitude relationships between the \(L\)’s and \(R\)’s of the intervals written in the subtree rooted at vertex \(v\). The answer is \(dp[\)root\(]\).

Let \(c_{left}\) and \(c_{right}\) be the left and right child vertices of \(v\), respectively. When writing intervals on the vertices of the subtree rooted at \(v\), the number of possible magnitude relationships of the \(L\)’s and \(R\)’s other than \(L_v\) and \(R_v\) is \(dp[c_{left}] \times dp[c_{right}]\). We can find \(dp[v]\) by considering how to insert \(L_v\) and \(R_v\) to satisfy the conditions. Let \(L_{max}\) and \(R_{min}\) be respectively the maximum \(L\) and the minimum \(R\) among the intervals written on the descendants of \(v\), and then we have:

\(dp[v]=dp[c_{left}] \times dp[c_{right}] \times \) \((\)the number of \(L\) in the subtree that satisfies \(L<R_{min}+1) \times \) \((\)the number of \(R\) in the subtree that satisfies \(L_{max} < R+1)\).

Here, it can be shown inductively that the number of \(L\)’s in the subtree that satisfies \(L<R_{min}\) is equal to the number of vertices that can be reached by continuing to follow the left child from vertex \(v\) (excluding \(v\)). The same is true for the number of \(R\)’s in the subtree that satisfies \(L_{max} < R\).

Then, the number of ways to write intervals for \(T\) is \(\prod_{i=1}^N (k_i+1)!\), where \(k_i\) is the length of the path that can be traced from the \(N\) leaves toward the parent in the same direction (left or right), as shown in the figure below. For example, the number of ways to write intervals for the binary tree in the figure is \((3+1)!\times (1+1)! \times (1+1)! \times (2+1)! \times (1+1)! \times (2+1)! = 6912\).

[3] Calculating the answer by correspondence to ordered tree

For the \(N\) paths on the binary tree considered in [2], let us regard these paths as vertices and make a graph where two paths are adjacent when they share a (single) vertex. This graph will be a tree, and we can make it ordered as follows:

  • The root is the edge connecting the vertices corresponding to the leftmost and rightmost paths.
  • The order is given by the depth of the vertices where the paths intersect.

Conversely, a unique rooted binary tree can be restored for any edge-rooted ordered tree.

Furthermore, the length of each path is equal to the degree of the vertex.

Therefore, the problem can be restated as finding the sum of the products of \((d+1)!\) for the degrees \(d\) of the vertices over all (edge-rooted) order trees.

To solve this problem, we will count the labeled order trees and divide the count by \(N!\) to find the final answer. The number of ordered trees with vertex \(i\) having a degree of \(d_i\) is \(2(N-1)!\), so the answer is

\[[x^{2N-2}] \frac{2}{N} \left(\sum_{d=1}^{N} (d+1)! x^d\right)^N.\]

 

Proof for the number of ordered trees An ordered tree can be created by attaching holes numbered from \(1\) to \(d_i-1\) and a special hole to each vertex and following the steps below:

  • Repeat the following \(N-2\) times: choose one hole and a special hole and connect them with an edge.
  • For the two vertices with their special holes remaining, place one to the left and the other to the right, span an edge between them, and make this edge the root.

The number of holes that can be chosen in the \(i\)-th \((1\leq i \leq N-2)\) step is \(N-2-i\), and of the remaining \(N+1-i\) special holes, there is only one that cannot be chosen, so there are \((N-2)! \times (N-1)!\) possible ways to perform the \(N-2\) steps. The last step can be performed in two ways, depending on which vertex goes to the left.

Considering the order in which non-root edges are added, the same ordered tree is generated \((N-2)!\) times, so the number of different ordered trees is \(\frac{2 \times (N-2)! \times (N-1)!}{(N-2)!}=2 \times (N-1)!\).

[4] Considering the fourth condition

Based on the above discussion, we consider the problem with the fourth condition.

First, the intervals satisfying \(L_i+1=R_i\) can only be written on leaves. Fix the leaves where those intervals are written. Then, in calculating the number of ways to write intervals by decomposing them into paths described in [2], either of the following conditions is added depending on the type of the interval written on a leaf:

  • None of \(L_v\) and \(R_v\) should be inserted between the \(L\) and \(R\) of the leaf.
  • Eventually, some \(L_v\) or \(R_v\) must be inserted between the \(L\) and \(R\) of the leaf.

The number of ways to insert a leaf to satisfy the first condition is \(k!\) instead of \((k+1)!\). Similarly, the number of ways to insert a leaf to satisfy the second condition is \((k+1)!-k!\).

Therefore, the answer to the problem is:

\[[x^{2N-2}] \frac{2}{N} \ _{N}C_K\left(\sum_{i=1}^{N} d!x^d\right)^K\left(\sum_{d=1}^{N} ((d+1)!-d!)x^d\right)^{N-K}.\]

This can be calculated in \(O(N\log^2N)\) time using convolution and binary exponentiation (or in \(O(N\log N)\) time using fps_pow).

(Above is a modification of a translation by GPT-4.)

posted:
last update: