CF2164F2.Chain Prefix Rank (Hard Version)

省选/NOI-

通过率:0%

时间限制:5.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The difference between the versions is that in this version, n≤5⋅105n \le 5 \cdot 10^5. You can hack only if you solved all versions of this problem.

Given a tree TT with nn vertices rooted at 11 and a sequence aa of length nn, count the number of permutations∗^{\text{∗}} pp of length nn that satisfy the following condition:

  • For all 1≤u≤n1 \le u \le n, there are exactly aua_u vertices vv such that vv is an ancestor of uu in TT and pv<pup_v \lt p_u.

Output the answer modulo 998 244 353998\,244\,353. Input data is selected in such way, such that it's guaranteed that at least one valid permutation exists.

∗^{\text{∗}}A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

这是该问题的困难版本。两个版本的区别在于,在本版本中,n≤5⋅105n \le 5 \cdot 10^5。仅当您已解决该问题的所有版本时,才可进行 Hack。

给定一棵以 11 为根、含 nn 个顶点的树 TT,以及一个长度为 nn 的序列 aa,请计算满足以下条件的排列∗^{\text{∗}} pp(长度为 nn)的个数:

  • 对所有 1≤u≤n1 \le u \le n,恰好存在 aua_u 个顶点 vv,使得 vv 是 uu 在树 TT 中的祖先,且 pv<pup_v \lt p_u。

输出答案对 998 244 353998\,244\,353 取模的结果。输入数据经过选取,保证至少存在一个合法的排列。

∗^{\text{∗}} 长度为 nn 的排列是指由 11 到 nn 中 nn 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,而 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中包含了 44)。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤50001 \le t \le 5000). The description of the test cases follows.

The first line of each test case contains an integer nn (2≤n≤5⋅1052 \le n \le 5 \cdot 10^5) — the number of vertices.

The second line of each test case contains n−1n-1 integers fa2,fa3,…,fanfa_2,fa_3,\ldots,fa_n (1≤fai<i1 \le fa_i \lt i)— faifa_i is the parent of ii on TT.

The third line of each test case contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (0≤ai<n0 \le a_i \lt n).

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5. Input data is selected in such way, such that it's guaranteed that at least one valid permutation exists.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤50001 \le t \le 5000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤5⋅1052 \le n \le 5 \cdot 10^5)—— 表示顶点数量。

每个测试用例的第二行包含 n−1n-1 个整数 fa2,fa3,…,fanfa_2,fa_3,\ldots,fa_n(1≤fai<i1 \le fa_i \lt i)—— 其中 faifa_i 表示树 TT 中顶点 ii 的父节点。

每个测试用例的第三行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(0≤ai<n0 \le a_i \lt n)。

保证所有测试用例的 nn 之和不超过 5⋅1055 \cdot 10^5。输入数据经过选取,以确保至少存在一个合法的排列。

输出格式

For each test case, output one integer — the number of permutations modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数——排列数对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#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

说明/提示

Visualizer link

In the first test case, the only permutation which satisfies the condition is [1,2,3,4,5][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][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][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][1,2,3,4,5]。

在第二个测试用例中,满足条件的排列有:[4,5,1,2,3][4,5,1,2,3]、[4,5,1,3,2][4,5,1,3,2]、[4,5,2,1,3][4,5,2,1,3]、[4,5,2,3,1][4,5,2,3,1]、[4,5,3,1,2][4,5,3,1,2]、[4,5,3,2,1][4,5,3,2,1]。

在第三个测试用例中,满足条件的排列有:[3,1,6,2,5,7,8,4][3,1,6,2,5,7,8,4]、[3,1,6,2,5,8,7,4][3,1,6,2,5,8,7,4]、[3,2,6,1,5,7,8,4][3,2,6,1,5,7,8,4]、[3,2,6,1,5,8,7,4][3,2,6,1,5,8,7,4]。

输入解题思路,AI测评打分。不知道怎么写?

首页