QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 1024 MB مجموع النقاط: 100

#3983. 国王的颜色

الإحصائيات

在遥远的地方,有一个神秘的树之王国(更正式地说是“连通无向简单无环图的皇家联邦”)。那里的国王非常难过,因为他的王国没有被联合国承认为主权国家。为了成为会员,他需要制作一面联合国可以放在其网站上的旗帜。

这面旗帜当然要由国王最喜欢的树组成,这棵树包含 $n$ 个节点。国王本来很乐意只用黑白两色来给树着色,但为了他的王后,他决定这棵树必须包含他们 $k$ 个孩子的所有最喜欢的颜色(他们每个人都有独特的喜好颜色)。显然,没有两个相邻的节点可以拥有相同的颜色,除此之外,任何使用恰好 $k$ 种颜色对树进行的着色方案都可以作为候选旗帜。

请问有多少种不同的旗帜方案?

输入格式

第一行包含两个整数 $n$ 和 $k$ ($2 \le k \le n \le 2500$),其中 $n$ 是国王最喜欢的树的节点数,$k$ 是孩子的数量。接下来有 $n - 1$ 行描述树中的边;这 $n - 1$ 行中的第 $i$ 行包含一个非负整数 $p_i < i$,表示节点 $p_i$ 是节点 $i$ 的父节点。

节点编号从 $0$ 到 $n - 1$,树的根节点为 $0$。注意,旗帜上树的嵌入方式已经固定,剩下的唯一任务就是分配颜色。

输出格式

输出不同颜色分配方案的数量。这个数字可能非常大,因此国王要求输出答案对 $1\,000\,000\,007$ 取模后的结果。

样例

输入 1

4 3
0
1
1

输出 1

18

输入 2

6 4
0
1
1
3
4

输出 2

600

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.