QOJ.ac

QOJ

حد الوقت: 2 s حد الذاكرة: 512 MB مجموع النقاط: 100

#3022. 红黑树

الإحصائيات

给定一棵包含 $n$ 个节点的有根树。节点编号为 $1 \dots n$。根节点为 $1$,其中有 $m$ 个节点被染成红色,其余为黑色。

你需要选择一个节点子集,使得该子集中没有任何节点是子集中其他节点的祖先。例如,如果 $A$ 是 $B$ 的父节点,$B$ 是 $C$ 的父节点,那么在你的子集中最多只能包含 $A, B, C$ 中的一个。此外,你希望所选节点中恰好有 $k$ 个是红色的。

若共有 $m$ 个红色节点,请对于所有 $k = 0 \dots m$,计算出有多少种选择子集的方法,使得子集中包含 $k$ 个红色节点,且没有任何节点是其他节点的祖先。

输入格式

输入包含单个测试用例。注意,你的程序可能会在不同的输入上运行多次。每个测试用例的第一行包含两个整数 $n$ ($1 \le n \le 2 \times 10^5$) 和 $m$ ($0 \le m \le \min(10^3, n)$),其中 $n$ 是树的节点数,$m$ 是红色节点的数量。节点编号为 $1 \dots n$。

接下来的 $n - 1$ 行,每行包含一个整数 $p$ ($1 \le p \le n$),表示该节点的父节点。节点按顺序给出,从节点 $2$ 开始,然后是节点 $3$,依此类推。节点 $1$ 被跳过,因为它是根节点。保证这些节点构成一棵以节点 $1$ 为根的单棵树,且没有环。

接下来的 $m$ 行,每行包含一个整数 $r$ ($1 \le r \le n$)。这些是红色节点的编号。保证 $r$ 的值不会重复。

输出格式

输出 $m + 1$ 行,依次对应包含 $k = 0 \dots m$ 个红色节点的满足条件的子集数量。输出结果需对 $10^9 + 7$ 取模。

样例

输入 1

4 1
1
1
1
3

输出 1

5
4

输入 2

4 4
1
1
1
1
2
3
4

输出 2

1
4
3
1
0

输入 3

14 4
1
2
1
2
3
4
5
5
13
8
10
4
4
8
3
12
13

输出 3

100
169
90
16
0

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.