QOJ.ac

QOJ

Límite de tiempo: 3.0 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#17789. 落月摇情满江树

Estadísticas

一棵河边的树可以描述为包含 $x$ 个节点的形式,节点编号为 $1 \sim x$:

  • 恰好有一个根节点,编号为 1。
  • 对于每个节点 $i$ ($i \neq 1$),它恰好有一个父节点,编号为 $f$,满足 $f < i$。

小 B 记不住“树”这样抽象的概念,于是他发明了两种将树转化为排列的方法:

Algorithm 1 Transfer Tree to Permutation
Require: sons of each node son, interger type equals to 1 or 2.
Ensure: queue q.
1: function DFS(u)
2:     q.push(u)
3:     if type = 1 then
4:         for each v in son[u] in increasing order do
5:             DFS(v)
6:         end for
7:     else
8:         for each v in son[u] in decreasing order do
9:             DFS(v)
10:            end for
11:    end if
12: end function
13: DFS(1)
  • 方法 1,$type = 1$:从根节点开始,先访问父节点,再访问子节点,且子节点按其编号升序进行访问。
  • 方法 2,$type = 2$:从根节点开始,先访问父节点,再访问子节点,且子节点按其编号降序进行访问。

可以证明,在访问完所有节点后,$q$ 中的元素构成一个 $1 \sim x$ 的排列。

如果一个排列可以由某棵树 $t_1$ 通过方法 1 得到,同时也可以由某棵树 $t_2$ 通过方法 2 得到,则称该排列是有歧义的(ambiguous)。注意,这两棵树可能是相同的,即 $t_1$ 可以等于 $t_2$。

猫猫(bugcat)只记得一个长度为 $n + m$ 的排列 $a$ 中 $1 \sim n$ 的位置,并希望你求出在所有可能的排列中,有多少个是有歧义的。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 10^5$),表示测试用例的数量。

对于每个测试用例:

第一行包含两个整数 $n$ 和 $m$ ($0 \le m \le 80$,$1 \le (n + m) \le 10^5$)。

第二行包含 $n + m$ 个整数 $a_{1 \sim n+m}$ ($0 \le a_i \le n$)。如果 $a_i = 0$,表示猫猫不记得位置 $i$ 处的数值。保证 $1$ 到 $n$ 中的每个数字在 $a$ 中恰好出现一次。

同时保证 $\sum (n + m) \le 3 \times 10^5$。

输出格式

对于每个测试用例,输出一个整数,表示有歧义的排列数量模 $10^9 + 7$ 的结果。

样例

输入样例 1

3
3 0
1 2 3
4 1
1 2 3 0 4
3 5
1 2 3 0 0 0 0 0

输出样例 1

1
1
109

说明

对于第二个样例,唯一可能的排列是 1, 2, 3, 5, 4。它可以由第一棵树通过方法 1 得到,也可以由第二棵树通过方法 2 得到,因此它是有歧义的。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.