#P16175. [ICPC 2014 NAIPC] Cheats

[ICPC 2014 NAIPC] Cheats

题目描述

Cosmo is busy playing the little-known latest installment in the Legend of Zelda series of video games, Skyward Wind Mask of Twilight Time. In this game, the player must complete all nn objectives as the young adventurer Link. However, some objectives must be done before others! Each objective ii, i=2..ni = 2..n, has a prerequisite, PiP_i, which must be completed before ii. Of course, the first objective (always number 1) has no prerequisite. There are no cycles of dependencies which would cause an objective to indirectly depend on itself.

Like all games, however, this game has hidden cheats. There is one cheat for each objective i=2..ni = 2..n which allows it to be completed before its prerequisite! However, things still can't be done too much out of order. If objective ii's cheat is used, then instead of objective ii being completed after it's prerequisite PiP_i, it just has to be completed after it's prerequisite's prerequisite, PPiP_{P_i} (unless Pi=1P_i = 1, in which case it can be completed at any point, since objective 1 has no prerequisite). Furthermore, using multiple cheats too close to each other can lead to unpredictable effects, so each objective can be involved in at most one use of a cheat. In other words, if you use a cheat on objective ii so that you can complete it before its prerequisite PiP_i, then you can't use a cheat on PiP_i, nor on any other objective that has ii as a prerequisite.

Cosmo would like to complete the game while exploiting at most kk cheats. In how many different orders can he complete all nn objectives, subject to these constraints? As this value can be very large, output it modulo 109+710^9+7.

输入格式

There will be multiple test cases in the input. Each test case will begin with two integers nn (1n2001 \leq n \leq 200) and kk (0k<n0 \leq k < n), indicating the number of objectives Cosmo must complete (nn) and the maximum number of cheats he's willing to use (kk). On the next line will be n1n-1 space-separated integers pp (1pn1 \leq p \leq n), indicating the prerequisite objective for objectives 2, 3, ..., nn (skipping 1, since objective 1 has no prerequisite). The input will end with a line with two 0s.

输出格式

For each test case, output a single integer, which indicates the number of ways Cosmo can achieve all nn objectives while using kk or fewer cheats. Output this number modulo 109+710^9+7. Do not output any spaces, and do not print any blank lines between answers.

5 1
1 1 5 1
0 0
38

提示

This table lists all of the orders in which Cosmo can achieve all of the objectives for the Sample Input/Output using at most one cheat.

:::align{center} :::