#P8935. Tree Pruning with Trunk Constraint
Tree Pruning with Trunk Constraint
Tree Pruning with Trunk Constraint
You are given a rooted tree with (n) nodes (numbered from 1 to (n)) with the root being node (1). You have to perform a pruning process on the tree. In each pruning operation, you choose a node (u) (which has not yet been removed) and remove the entire subtree of (u) (i.e. all nodes in the subtree of (u), including (u) itself). The process stops if and only if you choose node (1) (i.e. when you prune (1) the process terminates).
There is one more twist. You are given a special node (x). It is known that the unique simple path from (1) to (x) (i.e. the trunk) needs special handling. In the pruning process you must perform an operation on (x) exactly at the (k)-th pruning – that is, in the (k)-th operation you must choose (x) explicitly. In other words, you are not allowed to remove (x) indirectly (by pruning one of its ancestors on the trunk) in any of the first (k) operations. Note that pruning node (1) is allowed only when (x) has already been pruned in the (k)-th step.
Two pruning sequences are considered different if they have different lengths, or there exists an index (i) such that the node chosen in the (i)-th operation is different. Your task is to count the number of different valid pruning sequences modulo (10^9+7).
A valid pruning sequence must satisfy the following conditions:
- Initially, all (n) nodes are present and node (1) is always present.
- In each operation you may choose any node that is still present.
- When you choose a node (u), you remove every node in the subtree of (u) (with respect to the original tree).
- The operations continue until you choose node (1) (which removes all remaining nodes immediately).
- Moreover, if the unique trunk from (1) to (x) is (1 = v_1,v_2,\dots,v_{d}=x) then none of the nodes (v_1,v_2,\dots,v_{d-1}) (i.e. all trunk nodes except (x)) can be chosen in any operation before the (k)-th operation. And the (k)-th operation must choose (x) (note that (1) is never allowed before the final operation).
It is guaranteed that the input tree is rooted at (1) and that the trunk from (1) to (x) is well–defined.
For example, consider a tree with (n=2) and edge (1-2) (with (x=2) and (k=1)). The only valid sequence is first to choose (2) (directly pruning (x)) and then choose (1); hence the answer is 1.
inputFormat
The input begins with a line containing three integers (n), (x) and (k) ( (2 \le n \le 20,\ 1 \le x \le n,\ 1 \le k \le n)).
Then follow (n-1) lines. For (i=2,3,\dots,n) the (i)-th node’s parent is given as an integer. It is guaranteed that the given edges form a tree rooted at (1).
(Note: The constraint on (n) is small so that a brute force state search using bitmask DP is feasible.)
outputFormat
Output a single integer – the number of valid pruning sequences modulo (10^9+7).
sample
2 2 1
1
1
</p>