Rainbow Trees
Time limit5sMemory limit512 MB
Count edge colorings of a small tree with k colors so that any two or three consecutive edges on a path get distinct colors, modulo 1e9+9.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Tree, Combinatorics
- Solved
- No attempts yet
Problem
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 that are connected: every two consecutive edges of the sequence share a vertex.
You are given a tree with vertices and edges. You can color each edge in one of colors.
A coloring of the edges is a rainbow coloring if the edges of every path of 2 edges and of every path of 3 edges all have different colors. That is, any two consecutive edges have different colors, and any three consecutive edges have three different colors.
Given the tree and the number of colors , count the rainbow colorings modulo .
Input
The first line contains the number of test cases . Each of the cases follows in this format.
- One line with two integers and . Here is the number of nodes of the tree and is the number of available colors.
- lines, one per edge, each with two integers and , meaning that an edge joins node and node . Nodes are numbered from 1 to .
Limits
- Every node number is between 1 and , inclusive.
Output
For each test case print one line in the form Case #X: Y, where is the 1-based number of the case and is the answer for that case.
Hint
In the first sample case the tree has four nodes, and its three edges all meet at one node. Each pair of these edges is adjacent, so a rainbow coloring gives them three different colors, which leaves colorings.
In the second sample case the tree is a path of 4 edges and there are 3 colors. The first three edges must all have different colors, so they can be colored in ways, and then only one color is left for the fourth edge. That gives 6 rainbow colorings.