D - Reverse Brackets 解説 by evima
First, replace \(S\) with (
+ \(S\) + )
. It is easy to check that this does not change the final answer.
We now construct a one-to-one correspondence between \(S\) and a rooted tree with ordered children as follows.
Because \(S\) is a valid parenthesis sequence, we can form \(\frac{N}{2}\) pairs of matching opening and closing parentheses. Each such pair corresponds to one vertex. The pair consisting of the leftmost (
and the rightmost )
in \(S\) is taken to be the root vertex.
For each of the other vertices, its parent is the matching pair that strictly contains that pair and has the smallest span among such pairs. Moreover, if two pairs have the same parent, we determine the left–right order of their children according to their order in \(S\) (because of the nature of parenthesis sequences, pairs do not intersect, and for pairs with the same parent, a left–right relationship can be defined).
We can also convert a rooted tree back into a parenthesis sequence whose ends form a pair of parentheses. (If \(T\) is a tree whose root has children \(u_1, u_2, \ldots, u_k\) in that order, and \(t_i\) is the parenthesis sequence obtained from the subtree rooted at \(u_i\), then \(T\) corresponds to (
\(+ t_1 + t_2 + ... + t_k +\) )
.)
Under this correspondence, the operation can be rephrased as follows:
- Choose a vertex \(v\). Suppose \(v\)’s children are \(u_1, u_2, \ldots, u_k\) in that order. Choose a contiguous subsequence of these children, and reverse it. Then, for every vertex that has one of those children as an ancestor, reverse the order of the children of that vertex. (Here, “reverse” means taking a list \((a_1, a_2, \dots, a_n)\) to \((a_n, a_{n-1}, \dots, a_1)\); this differs from “reverse” as defined in the problem statement.)
By combining these operations, we can perform the following simpler operation:
- Choose a vertex \(v\). Suppose \(v\)’s children are \(u_1, u_2, \ldots, u_k\) in that order. Choose a contiguous subsequence of these children and reverse it.
Moreover, even when using this new operation, it is still possible to perform the original operation. Therefore, from now on, we count the number of trees that can be produced by using this new operation.
Using this operation, we can reorder the children of any vertex \(v\) in any way we like. However, we must take into account the possibility of duplicate subtrees among those children. In other words, the problem can be rephrased as follows:
For each vertex \(v\), let \(t_v\) be the subtree rooted at \(v\). If we distinguish \(t_1, t_2, \ldots, t_n\) by whether or not they are isomorphic, determine the frequency distribution of these subtrees.Here, two rooted trees \(A\) and \(B\) are said to be isomorphic if they satisfy one of the following:
- \(A\) and \(B\) both consist of exactly one vertex.
- \(A\) and \(B\) have the same number of children, and there is a way to pair the children such that for every pair \((a, b)\), the subtree rooted at \(a\) is isomorphic to the subtree rooted at \(b\).
We can check isomorphism starting from smaller subtrees. Concretely, assign an integer label to each subtree. Assign label \(1\) to any tree consisting of a single vertex. Then, consider a tree \(T\); let \(u_1, u_2, \dots, u_k\) be its children. View \(T\) as the multiset of labels of the subtrees rooted at those \(u_i\). If that multiset has already appeared, then \(T\) receives the same label as the tree that had that multiset. If it has not appeared yet, then assign \(T\) the smallest positive integer label that has not been used so far.
Implementing this procedure directly achieves an \(O(N \log N)\) solution. An \(O(N^2)\) approach, such as maintaining canonicalized parenthesis strings instead of integers to represent trees, or explicitly computing a hash for each of the \(N\) subtrees, would also be acceptable.
投稿日時:
最終更新: