#P7635. [2018年杭电多校]Rikka with Spanning Tree
[2018年杭电多校]Rikka with Spanning Tree
Rikka with Spanning Tree
Problem Description
Rikka is interested in researching traditional problems on particular graphs. Today, she chooses the task "counting the number of spanning trees in an undirected edge". With an array of length , Rikka constructs an undirected graph with vertices in the following way:
- Construct an auxiliary array :
- Assign a color to each vertex. The color of vertex is an integer which satisfies .
- For each pair , if vertex and have the same color, link an undirected edge between and . In other words, Rikka constructs a graph which contains cliques, and the th clique's size is . Rikka finds that if , the graph cannot be connected, and there must not be any spanning trees in it. To avoid this, Rikka adds extra edges to this graph. Now, Rikka wants to count the number of different spanning trees in this graph. Two spanning trees are different if and only if there is one edge which occurs in exactly one of the two trees.
Input
The first line contains a single integer , the number of the testcases. For each testcase, the first line contains two integers . The second line contains integers . Then lines follows, each line contains two integers , which describes an extra edge. The input guarantees that:
- There are at most testcases with .
- The graph does not have multiplicate edges and self circle. i.e., , have different colors and unordered pairs are different from each other.
- The final graph is connected.
Output
For each testcase, output a single line with a single integer, the answer modulo .
Sample Input
3
1 0
5
2 1
5 5
1 6
4 4
1 2 3 4
1 2
3 4
6 7
10 1
Sample Output
125
15625
296
Source
2018 Multi-University Training Contest 9