Osmanthus Tree
Time limit0.5sMemory limit1024 MB
Count trees on n+m vertices that keep the LCAs of the given tree T on vertices 1 to n and keep each pairwise LCA label within max(i, j) + k, modulo 1e9+7.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Tree, Combinatorics
- Solved
- No attempts yet
Problem
Eight years ago, Little B saw an osmanthus tree, which is a tree with vertices, where the parent vertex of any non-root vertex in has a smaller label than its own. Given an integer , a rooted tree with vertices is prosperous if and only if the following conditions are met:
- For any with , the lowest common ancestor of vertices and in and has the same label.
- For any with , the label of the lowest common ancestor of vertices and in does not exceed .
All vertices are labeled starting from , and the root has label . does not need to satisfy the condition that the parent vertex of any non-root vertex has a smaller label than its own.
Little B wants to know how many trees with vertices are prosperous. Two trees are different if there is a vertex whose parent differs between the two trees. Output the count modulo .
Input
The input contains multiple test data sets.
The first line contains two integers and , where is the test case number and is the number of test data sets. If , this is a sample test.
Each test data set starts with a line containing three integers , , and . The next line contains integers , where is the label of the parent vertex of vertex in .
Output
For each test data set, output one line containing the number of prosperous trees modulo .
Constraints
For all test data, it is guaranteed that: