QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓

#10826. 树上游戏

Statistics

Alice 发现了一个新游戏,并准备和 Bob 一起玩。

给定一棵以节点 1 为根的树,包含 $n$ 个节点。每个节点 $u$ 都有一个关联值 $a_u$,其中 $a_1, a_2, \dots, a_n$ 是 $n$ 的一个排列(从 1 到 $n$ 的每个整数恰好出现一次)。我们定义节点 $u$ 上的“重排”操作如下:

  • 移除节点 $u$ 及其所有直接相连的子节点上的值,然后重新分配这些值,使得 $a_1, a_2, \dots, a_n$ 仍然是 $n$ 的一个排列。

操作完成后,我们称节点 $u$ 为“已重排”。Alice 仅当一个节点的所有直接子节点都已重排时,才能对该节点进行重排。此外,每个节点最多只能被重排一次。如果 Alice 能使得对于每个节点 $u$ 都有 $a_u = u$,她就赢得了这个游戏。

Alice 想要挑战这个游戏,并要求 Bob 画出一棵树并为每个节点分配初始值。现在 Bob 已经画好了一棵树并分配了部分值,但在他完成所有值的分配之前,他发现有些分配方式会导致 Alice 永远无法获胜,这会让 Alice 感到失望。为了避免这种情况,Bob 想知道有多少种分配方式使得 Alice 有机会获胜。由于答案可能很大,请输出答案对 998244353 取模的结果。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 10000$),表示测试用例的数量。 每个测试用例的第一行包含一个整数 $n$ ($2 \le n \le 200000$),表示节点数。 每个测试用例的第二行包含 $n - 1$ 个整数 $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$),其中 $p_i$ 表示节点 $i$ 的父节点。 每个测试用例的第三行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$),表示每个节点的初始值。$a_i = 0$ 表示节点 $i$ 尚未被分配值。保证所有非零的 $a_i$ 互不相同。

保证所有测试用例的 $n$ 之和不超过 200000。

输出格式

对于每个测试用例,输出一行一个整数,表示可能的分配方案数。由于答案可能很大,请输出答案对 998244353 取模的结果。

样例

样例输入 1

2
7
1 2 2 3 2 4
0 0 0 2 0 0 0
7
1 2 3 3 3 1
0 0 0 0 0 0 0

样例输出 1

12
288

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.