Rainbow Trees

Count rainbow edge colorings of a tree where any two adjacent edges differ and any three consecutive edges all differ, modulo 1e9+9.

Medium7TreeGreedyDynamic programmingMathNo attempts yetTime limit5sMemory limit512 MB

Problem

In graph theory, a tree is a connected undirected simple graph with no cycles. A tree with nn vertices always has n1n-1 edges.

A path in a tree is a sequence of distinct edges in which every two consecutive edges share a vertex.

You are given a tree with nn vertices and n1n-1 edges. Paint each edge with one of kk colors.

A painting is a rainbow coloring if the edges of every path made of 2 edges have different colors and the edges of every path made of 3 edges have different colors. In other words, two consecutive edges always differ in color, and three consecutive edges always use three different colors.

Given the tree and the number of colors kk, compute the number of rainbow colorings modulo 10000000091000000009.

Input

The first line contains the number of test cases CC. Each test case then consists of the following.

  • One line with two integers nn and kk separated by a space. nn is the number of vertices of the tree and kk is the number of available colors.
  • n1n-1 lines, one per edge, each with two integers xx and yy: the endpoints of that edge. Vertices are numbered from 1 to nn.

Limits

  • 1C401 \le C \le 40
  • 2n5002 \le n \le 500
  • 1k10000000001 \le k \le 1000000000
  • Every vertex number is between 1 and nn, inclusive.
  • The n1n-1 given edges always form a tree.

Output

For each test case, print one line in the format Case #X: Y, where XX is the 1-based test case number and YY is the answer for that case.

Notes

Take a tree with 4 vertices where one vertex is joined to each of the other three, with 10 colors available. All three edges are pairwise adjacent, so a rainbow coloring must give them three different colors. That leaves 10×9×8=72010 \times 9 \times 8 = 720 colorings.

Now take a tree whose 5 vertices form a single path, with 3 colors available. The first three edges must all differ, which gives 3×2×13 \times 2 \times 1 choices, and the color of the fourth edge is then forced. So there are 6 rainbow colorings.