DBFS 顺序(困难版)AI (CI翻译)
This is the hard version of the problem. The difference between the versions is that in this version, the string $s$ may also contain character 1. You can hack only if you solved all versions of this problem.
这是本题的困难版本。与其他版本的区别在于,本版本中的字符串 $s$ 也可能包含字符 1。只有解决了本题的所有版本,才能进行 hack。AI (CI翻译)
You are given a rooted tree with $n$ vertices, rooted at vertex $1$. For each vertex, its children are given in a fixed order.
给定一棵有 $n$ 个顶点的有根树,根为顶点 $1$。每个顶点的子节点都按固定顺序给出。AI (CI翻译)
Each vertex except the root has a color, either $0$ or $1$. For a fixed coloring, define the following traversal.
除根节点外,每个顶点都有一种颜色:$0$ 或 $1$。对于一种固定的染色,定义如下遍历过程。AI (CI翻译)
p <- empty list
q <- deque containing only vertex 1
while q is not empty:
v <- the front element of q
if v is not in p:
append v to p
if every child of v is already in p or in q:
pop the front element from q
else:
u <- the first child of v that is neither in p nor in q
if color[u] = 0:
push u to the front of q
else:
push u to the back of q
p <- empty list
q <- deque containing only vertex 1
while q is not empty:
v <- the front element of q
if v is not in p:
append v to p
if every child of v is already in p or in q:
pop the front element from q
else:
u <- the first child of v that is neither in p nor in q
if color[u] = 0:
push u to the front of q
else:
push u to the back of qAI (CI翻译)
After the process ends, the list $p$ is called the traversal list of this coloring. It can be shown that $p$ is always a permutation of $1,2,\ldots,n$. In particular, if all colors are $0$, then $p$ is the DFS preorder of the tree; if all colors are $1$, then $p$ is the BFS order of the tree, where children are visited in the given order.
过程结束后,列表 $p$ 称为该染色对应的遍历列表。可以证明,$p$ 总是 $1,2,\ldots,n$ 的一个排列。特别地,如果所有颜色都是 $0$,那么 $p$ 就是这棵树的 DFS 先序遍历;如果所有颜色都是 $1$,那么 $p$ 就是这棵树的 BFS 遍历,其中子节点按照给定顺序访问。AI (CI翻译)
You are given a string $s$ of length $n-1$, consisting of characters 0, 1, and ?. For each vertex $i$ with $2\le i\le n$, the character $s_{i-1}$ describes the possible color of vertex $i$:
给定一个长度为 $n-1$ 的字符串 $s$,由字符 0、1 和 ? 组成。对于每个满足 $2\le i\le n$ 的顶点 $i$,字符 $s_{i-1}$ 描述了该顶点可以取的颜色:AI (CI翻译)
- if
$s_{i-1}=\texttt{0}$, then vertex$i$must have color$0$; - if
$s_{i-1}=\texttt{1}$, then vertex$i$must have color$1$; - if
$s_{i-1}=\texttt{?}$, then vertex$i$may have color$0$or$1$.
- 如果
$s_{i-1}=\texttt{0}$,那么顶点$i$的颜色必须为$0$; - 如果
$s_{i-1}=\texttt{1}$,那么顶点$i$的颜色必须为$1$; - 如果
$s_{i-1}=\texttt{?}$,那么顶点$i$的颜色可以是$0$或$1$。
Find the number of distinct traversal lists that can be obtained over all valid colorings. Since the answer may be large, output it modulo $10^9+7$.
求所有合法染色所能得到的不同遍历列表的数量。由于答案可能很大,请输出对 $10^9+7$ 取模后的结果。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows..
每个输入包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)
The first line of each test case contains an integer $n$ ($2 \le n \le 3000$) — the number of vertices in the tree.
每个测试用例的第一行包含一个整数 $n$($2 \le n \le 3000$)——树的顶点数。AI (CI翻译)
The second line contains a string $s$ of length $n-1$. In the hard version, $s$ consists of characters 0, 1, and ?. The character $s_i$ describes the possible color of vertex $i+1$.
第二行包含一个长度为 $n-1$ 的字符串 $s$。在困难版本中,$s$ 由字符 0、1 和 ? 组成。字符 $s_i$ 描述顶点 $i+1$ 可以取的颜色。AI (CI翻译)
The next $n$ lines describe the ordered lists of children. The $i$-th of these lines first contains an integer $l_i$ ($0 \le l_i \le n-1$) — the number of children of vertex $i$. Then follow $l_i$ distinct integers $a_{i,1},a_{i,2},\ldots,a_{i,l_i}$ ($1 \le a_{i,j} \le n$) — the children of vertex $i$ in their order.
接下来 $n$ 行描述各顶点的有序子节点列表。第 $i$ 行首先包含一个整数 $l_i$($0 \le l_i \le n-1$)——顶点 $i$ 的子节点数量。接下来是 $l_i$ 个互不相同的整数 $a_{i,1},a_{i,2},\ldots,a_{i,l_i}$($1 \le a_{i,j} \le n$)——顶点 $i$ 的子节点,按给定顺序排列。AI (CI翻译)
It is guaranteed that the given ordered children lists describe a rooted tree with root $1$.
保证给出的有序子节点列表构成一棵以 $1$ 为根的有根树。AI (CI翻译)
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $9 \cdot 10^6$.
保证所有测试用例的 $n^2$ 之和不超过 $9 \cdot 10^6$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output a single integer — the number of distinct traversal lists $p$ that can be generated over all valid color assignments, modulo $10^9 + 7$.
对于每个测试用例,输出一个整数——所有合法颜色分配所能生成的不同遍历列表 $p$ 的数量,对 $10^9 + 7$ 取模。AI (CI翻译)
Note
说明
AI (CI翻译)Let $c_i$ be the color of vertex $i$.
设 $c_i$ 为顶点 $i$ 的颜色。AI (CI翻译)
In the first test case, vertex $2$ must have color $1$, while vertices $3$ and $4$ are free.
在第一个测试用例中,顶点 $2$ 的颜色必须为 $1$,而顶点 $3$ 和 $4$ 的颜色可以自由选择。AI (CI翻译)
If $(c_3,c_4)=(0,0)$, the traversal list is $[1,3,4,2]$.
如果 $(c_3,c_4)=(0,0)$,遍历列表为 $[1,3,4,2]$。AI (CI翻译)
If $(c_3,c_4)=(0,1)$, the traversal list is $[1,3,2,4]$.
如果 $(c_3,c_4)=(0,1)$,遍历列表为 $[1,3,2,4]$。AI (CI翻译)
If $(c_3,c_4)=(1,0)$ or $(c_3,c_4)=(1,1)$, the traversal list is $[1,2,3,4]$.
如果 $(c_3,c_4)=(1,0)$ 或 $(c_3,c_4)=(1,1)$,遍历列表为 $[1,2,3,4]$。AI (CI翻译)
Thus there are $3$ distinct traversal lists.
因此,共有 $3$ 个不同的遍历列表。AI (CI翻译)
In the second test case, the tree is a star rooted at vertex $1$, and all five leaves have free colors. A leaf with color $0$ is visited immediately when it is considered, while a leaf with color $1$ is postponed until after all children of the root have been considered. Among all $2^5$ valid color assignments, there are $27$ distinct traversal lists.
在第二个测试用例中,这棵树是以顶点 $1$ 为根的星形树,五个叶子节点的颜色都可以自由选择。颜色为 $0$ 的叶子节点会在被考虑时立即访问,而颜色为 $1$ 的叶子节点会推迟到根节点的所有子节点都被考虑之后再访问。在 $2^5$ 种合法颜色分配中,共有 $27$ 个不同的遍历列表。AI (CI翻译)
In the third test case, the free vertices are $2,3,4,5,6,8,12$. The fixed colors are $c_7=1$, $c_9=0$, $c_{10}=1$, and $c_{11}=0$. Among all $2^7$ valid color assignments, there are $88$ distinct traversal lists.
在第三个测试用例中,颜色可以自由选择的顶点为 $2,3,4,5,6,8,12$。固定颜色为 $c_7=1$、$c_9=0$、$c_{10}=1$ 和 $c_{11}=0$。在 $2^7$ 种合法颜色分配中,共有 $88$ 个不同的遍历列表。AI (CI翻译)
样例
10
4
1??
2 2 3
0
1 4
0
6
?????
5 2 3 4 5 6
0
0
0
0
0
12
?????1?010?
1 11
2 8 3
3 9 12 6
0
1 4
0
1 10
0
1 5
0
2 2 7
0
2
1
1 2
0
5
1?0?
1 2
1 3
1 4
1 5
0
5
1111
4 2 3 4 5
0
0
0
0
3
??
2 2 3
0
0
5
1???
2 2 3
2 4 5
0
0
0
7
?1?0??
2 2 3
2 4 5
2 6 7
0
0
0
0
8
1?0??1?
3 2 3 4
2 5 6
1 7
0
0
1 8
0
0
3
27
88
1
1
1
2
6
8
14
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。