#P9479. [NOI2023] 桂花树

[NOI2023] 桂花树

Problem Description

The osmanthus tree that Little B saw eight years ago was a tree TT with nn nodes. It is guaranteed that, for every non-root node in TT, the index of its parent is smaller than its own index. Given an integer kk, a rooted tree TT^{\prime} with (n+m)(n+m) nodes is called prosperous if and only if all the following conditions are satisfied:

  1. For any pair (i,j)(i,j) with 1i,jn1 \le i,j \le n, in both tree TT and tree TT^{\prime}, the index of the lowest common ancestor of nodes ii and jj is the same.
  2. For any pair (i,j)(i,j) with 1i,jn+m1 \le i,j \le n + m, in tree TT^{\prime}, the index of the lowest common ancestor of nodes ii and jj is at most max(i,j)+k\max(i,j)+k.

Note that all trees in this problem are indexed starting from 11, and the root has index 11. TT^{\prime} does not need to satisfy that the parent index of a non-root node is smaller than its own.

Little B wants to know how many prosperous trees with (n+m)(n+m) nodes there are. Two trees are considered different if and only if there exists some node whose parent is different in the two trees. You only need to output the number of valid trees modulo (109+7)(10^9+7).

Input Format

This problem has multiple test cases.

The first line contains two integers c,tc,t, representing the test point ID and the number of test cases. c=0c=0 means this test point is the sample.

Then each test case is given as follows:

The first line contains three integers n,m,kn,m,k.

The second line contains n1n-1 integers f2,f3,,fnf_2,f_3,\dots,f_n, where fif_i is the index of the parent of node ii in TT.

Output Format

For each test case, output one integer per line, the number of prosperous trees modulo (109+7)(10^9+7).

0 3
1 2 1

2 2 1
1
2 2 0
1
3
16
15
见附件中的 tree/tree2.in。
见附件中的 tree/tree2.ans。
见附件中的 tree/tree3.in。
见附件中的 tree/tree3.ans。
见附件中的 tree/tree4.in。
见附件中的 tree/tree4.ans。

Hint

[Sample Explanation #1]

For the first test case in the sample, there are three valid trees. The sequence of parents of each node, {f2,f3}\{f_2,f_3\}, is respectively {1,1}\{1,1\}, {3,1}\{3,1\}, and {1,2}\{1,2\}. Note that the second line of this test case is an empty line.

For the second and third test cases in the sample, there are 1616 trees that satisfy the first condition, and among them only the tree with parent sequence {4,4,1}\{4,4,1\} does not satisfy the second condition in the third test case.

[Sample Explanation #2]

This sample group satisfies n100n \le 100. In the five test cases, mm is at most 0,1,1,2,20, 1, 1, 2, 2, respectively.

[Sample Explanation #3]

This sample group satisfies k=0k = 0. In the five test cases, the first two test cases satisfy n=1n = 1, and the first, third, and fourth test cases satisfy n,m100n, m \le 100.

[Sample Explanation #4]

In this sample group, the first two test cases satisfy n=1n = 1, and the first, third, and fourth test cases satisfy n,m100n, m \le 100.

[Constraints]

For all testdata, it is guaranteed that 1t151 \le t \le 15, 1n3×1041 \le n \le 3 \times 10^4, 0m30000 \le m \le 3000, 0k100 \le k \le 10, and 1fii11 \le f_i \le i - 1.

Test Point ID nn \le mm \le kk \le
1,21,2 44 1010
33 3×1043\times 10^4 00
44 10210^2 11
55 3×1043 \times 10^4
66 10210^2 22
77 3×1043\times 10^4
8,98,9 11 10210^2 00
1010 3,0003,000
1111 10210^2 1010
1212 3,0003,000
13,1413,14 10210^2 00
15,1615,16 3×1043\times 10^4 3,0003,000
17,1817,18 10210^2 1010
19,2019,20 3×1043\times 10^4 3,0003,000

Translated by ChatGPT 5