#CF2237I2. DBFS 序(困难版) / I2. DBFS Order (Hard Version)
DBFS 序(困难版) / I2. DBFS Order (Hard Version)
I2. DBFS Order (Hard Version)
Source: Codeforces 2237I2
Contest: Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
Time limit: 2 seconds
Memory limit: 1024 megabytes
Statement
This is the hard version of the problem. The difference between the versions is that in this version, the string may also contain character 1. You can hack only if you solved all versions of this problem.
You are given a rooted tree with vertices, rooted at vertex . For each vertex, its children are given in a fixed order.
Each vertex except the root has a color, either or . For a fixed coloring, define the following traversal.
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
After the process ends, the list is called the traversal list of this coloring. It can be shown that is always a permutation of . In particular, if all colors are , then is the DFS preorder of the tree; if all colors are , then is the BFS order of the tree, where children are visited in the given order.
You are given a string of length , consisting of characters 0, 1, and ?. For each vertex with , the character describes the possible color of vertex :
- if , then vertex must have color ;
- if , then vertex must have color ;
- if , then vertex may have color or .
Find the number of distinct traversal lists that can be obtained over all valid colorings. Since the answer may be large, output it modulo .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows..
The first line of each test case contains an integer () — the number of vertices in the tree.
The second line contains a string of length . In the hard version, consists of characters 0, 1, and ?. The character describes the possible color of vertex .
The next lines describe the ordered lists of children. The -th of these lines first contains an integer () — the number of children of vertex . Then follow distinct integers () — the children of vertex in their order.
It is guaranteed that the given ordered children lists describe a rooted tree with root .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the number of distinct traversal lists that can be generated over all valid color assignments, modulo .
Note
Let be the color of vertex .
In the first test case, vertex must have color , while vertices and are free.
If , the traversal list is .
If , the traversal list is .
If or , the traversal list is .
Thus there are distinct traversal lists.
In the second test case, the tree is a star rooted at vertex , and all five leaves have free colors. A leaf with color is visited immediately when it is considered, while a leaf with color is postponed until after all children of the root have been considered. Among all valid color assignments, there are distinct traversal lists.
In the third test case, the free vertices are . The fixed colors are , , , and . Among all valid color assignments, there are distinct traversal lists.
Examples
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