#P13484. [GCJ 2008 EMEA SemiFinal] Rainbow Trees
[GCJ 2008 EMEA SemiFinal] Rainbow Trees
题目描述
In graph theory, a tree is a connected, undirected simple graph with no cycles. A tree with nodes always has edges.
A path in a tree is a sequence of distinct edges which are connected (each pair of consecutive edges in the path share a vertex).
Consider a tree with vertices and edges. You can color each edge in one of colors.
An assignment of colors to edges is a rainbow coloring if in every path of or edges, the colors of the edges are different. (i.e., every two consecutive edges have different colors, and every three consecutive edges have different colors).
Given a tree and the number of colors , find the number of rainbow colorings modulo .
输入格式
The first line of input gives the number of test cases, . Then for each of the cases, there will be:
- One line containing two integers in the format " ". is the number of nodes in the tree, and is the number of colors available.
- lines, one for each edge, containing two integers " ", indicating that the edge is between node and node . Nodes are numbered from to .
输出格式
For each test case, output one line. That line should contain "Case #: ", where is 1-based number of the case, and is the answer for that test case.
2
4 10
1 2
1 3
1 4
5 3
1 2
2 3
3 4
4 5
Case #1: 720
Case #2: 6
提示
Sample Explanation
In the first case, the tree has four nodes. There are edges from one node to each of the other three. Each pair of these edges are adjacent, so for there to be a rainbow coloring, all the edges must have different colors. There are therefore rainbow colorings.
In the second case, the tree itself is a path of edges, and there are colors. The first three edges must all have different colors, so there are colorings for these, and then there is only one choice for the fourth edge, so there are rainbow colorings.
Limits
- All the node numbers are between and , inclusive.
Small dataset (9 Pts, Test set 1 - Visible)
Large dataset (15 Pts, Test set 2 - Hidden)