编程介的小学生 2017-08-02 10:34 采纳率: 20.5%
浏览 672
已采纳

color

Problem Description
You are given a directed graph, which is constructed as follows:

  • Pick an connected undirected graph with exactly n vertices and n edges. The vertices are numbered 1 through n.
  • Convert each undirected edge into a directed edge, satisfying each vertice's outdegree equals 1.

Also you're given m different colors to color the vertices. Your task is to calculate the number of different colored graphs that can be made.

Two colored graphs A and B are considered the same if and only if there exists a mapping P satisfying the following constraints:

  • Vertice u in graph A has the same color as vertice P(u) in graph B.
  • For any two different vertices u and v in graph A, P(u) and P(v) are different vertices in graph B.
  • For any directed edge u -> v in graph A, there exists a corresponding directed edge P(u) -> P(v) in graph B.

Print the answer module 10^9 + 7.

Input
The first line contains a number T(1 <= T <= 10), denoting the number of testcases.

For each testcase, the first line contains two space-seperated integers n and m(3 <= n <= 10^5, 1 <= m <= 10^9), representing the number of vertices in the graph and the number of colors you have.

Then, n lines follow. The i-th of them contains an integer f_i, denoting a directed edge from vertice i to vertice f_i in the given graph.(1 <= f_i <= n, f_i != i)

Output
For each testcase, print a line containing the answer.

Sample Input
1
6 3
2
3
4
1
1
3

Sample Output
378

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog