CF2164F1.Chain Prefix Rank (Easy Version)
省选/NOI-
通过率:0%
时间限制:5.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the easy version of the problem. The difference between the versions is that in this version, n≤5000. You can hack only if you solved all versions of this problem.
Given a tree T with n vertices rooted at 1 and a sequence a of length n, count the number of permutations∗ p of length n that satisfy the following condition:
- For all 1≤u≤n, there are exactly au vertices v such that v is an ancestor of u in T and pv<pu.
Output the answer modulo 998244353. Input data is selected in such a way that at least one valid permutation exists.
∗A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
这是该问题的简单版本。两个版本的区别在于,在此版本中,n≤5000。仅当您解决了该问题的所有版本时,才可进行 Hack。
给定一棵以 1 为根、含 n 个顶点的树 T 和一个长度为 n 的序列 a,请计算满足以下条件的排列∗ p(长度为 n)的个数:
- 对所有 1≤u≤n,恰好存在 au 个顶点 v,使得 v 是 u 在树 T 中的祖先,且 pv<pu。
输出答案对 998244353 取模的结果。输入数据保证至少存在一个合法的排列。
∗ 长度为 n 的排列是指由 1 到 n 中 n 个互不相同的整数按任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列,但 [1,2,2] 不是排列(数字 2 在数组中出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤2500). The description of the test cases follows.
The first line of each test case contains an integer n (2≤n≤5000) — the number of vertices.
The second line of each test case contains n−1 integers fa2,fa3,…,fan (1≤fai<i)— fai is the parent of i on T.
The third line of each test case contains n integers a1,a2,…,an (0≤ai<n).
It is guaranteed that the sum of n over all test cases does not exceed 5000. Input data is selected in such a way that at least one valid permutation exists.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤2500)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤5000)—— 表示顶点数量。
每个测试用例的第二行包含 n−1 个整数 fa2,fa3,…,fan(1≤fai<i)—— 其中 fai 表示树 T 中顶点 i 的父节点。
每个测试用例的第三行包含 n 个整数 a1,a2,…,an(0≤ai<n)。
保证所有测试用例的 n 之和不超过 5000。输入数据经过选取,确保至少存在一个合法的排列。
输出格式
For each test case, output one integer — the number of permutations modulo 998244353.
对于每个测试用例,输出一个整数——排列数对 998244353 取模的结果。
输入输出样例
输入#1
3 5 1 2 3 4 0 1 2 3 4 5 1 2 1 1 0 1 0 0 0 8 1 1 3 3 4 5 7 0 0 1 0 1 3 3 1
输出#1
1 6 4
说明/提示
In the first test case, the only permutation which satisfies the condition is [1,2,3,4,5].
In the second test case, permutations which satisfy the condition are [4,5,1,2,3],[4,5,1,3,2],[4,5,2,1,3],[4,5,2,3,1],[4,5,3,1,2],[4,5,3,2,1].
In the third test case, permutations which satisfy the condition are [3,1,6,2,5,7,8,4],[3,1,6,2,5,8,7,4],[3,2,6,1,5,7,8,4],[3,2,6,1,5,8,7,4].
在第一个测试用例中,唯一满足条件的排列是 [1,2,3,4,5]。
在第二个测试用例中,满足条件的排列有:[4,5,1,2,3],[4,5,1,3,2],[4,5,2,1,3],[4,5,2,3,1],[4,5,3,1,2],[4,5,3,2,1]。
在第三个测试用例中,满足条件的排列有:[3,1,6,2,5,7,8,4],[3,1,6,2,5,8,7,4],[3,2,6,1,5,7,8,4],[3,2,6,1,5,8,7,4]。
输入解题思路,AI测评打分。不知道怎么写?