当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

4425:Fantastic Necklace

题目描述
Alice is one of the most famous necklace designers. She is designing a fantastic necklace of new styles. Like other necklaces, this necklace consists of n beads and n strings connecting the bead together. The most significant difference is that the beads are not connected one by one but has many branches. The branches connecting to the same bead can be arranged arbitrarily. The shaking of the branches makes the necklace more glaring. Another difference is that the beads are not connected with ordinary strings. The two ends of each string differ, which makes the necklace more unique.
She has finished the structure of the new necklace. However, she is hesitating what color of bead should be used for each position. As a famous designer, she has beads of m colors and each type has infinite number of beads. She wants to make a necklace that any pair of adjacent beads has different colors. Now can you tell her how many different necklaces can she make with the colorful beads? Two necklaces are counted as one if each bead coincides after rotating the necklace or rearranging the braches. But because of the special strings, two necklaces with mirroring arrangements are not considered the same.
输入解释
The input consists of about 50 test cases. The first line of each test case contains two integers, n and m, the number of beads of the necklace and the number of colors of beads Alice has (2 < n ≤ 100000, 1 ≤ m ≤ 100000). The second line contains n integers, the i-th of which, named pi, means that there is a string from the i-th bead to the pi-th bead (1 ≤ pi ≤ n and pi does not equal to i). The necklace is always valid and there is no duplicated string connecting the same pair of beads.
输出解释
For each test case, output the number of different necklaces that Alice can make in one line. For the answer is large, so please give the result of answer module 1,000,000,007.
输入样例
3 3
2 3 1
3 2
2 3 1
6 3
2 3 1 1 2 3
6 2
2 3 4 1 1 3
6 3
2 3 1 1 4 4
输出样例
2
0
16
2
36
来自杭电HDUOJ的附加信息
Recommend zhuyuanchen520

该题目是Virtual Judge题目,来自 杭电HDUOJ

源链接: HDU-4425

最后修改于 2020-10-25T23:13:23+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
2000/1000MS(Java/Others) 32768/32768K(Java/Others)