#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 nn nodes always has n1n - 1 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 nn vertices and n1n - 1 edges. You can color each edge in one of kk colors.

An assignment of colors to edges is a rainbow coloring if in every path of 22 or 33 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 kk, find the number of rainbow colorings modulo 10000000091000000009.

输入格式

The first line of input gives the number of test cases, CC. Then for each of the CC cases, there will be:

  • One line containing two integers in the format "nn kk". nn is the number of nodes in the tree, and kk is the number of colors available.
  • n1n - 1 lines, one for each edge, containing two integers "xx yy", indicating that the edge is between node xx and node yy. Nodes are numbered from 11 to nn.

输出格式

For each test case, output one line. That line should contain "Case #XX: YY", where XX is 1-based number of the case, and YY 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 10×9×8=72010 \times 9 \times 8 = 720 rainbow colorings.

In the second case, the tree itself is a path of 44 edges, and there are 33 colors. The first three edges must all have different colors, so there are 3×2×13 \times 2 \times 1 colorings for these, and then there is only one choice for the fourth edge, so there are 66 rainbow colorings.

Limits

  • 1k10000000001 \leq k \leq 1000000000
  • All the node numbers are between 11 and nn, inclusive.

Small dataset (9 Pts, Test set 1 - Visible)

  • 1C1001 \leq C \leq 100
  • 2n202 \leq n \leq 20

Large dataset (15 Pts, Test set 2 - Hidden)

  • 1C401 \leq C \leq 40
  • 2n5002 \leq n \leq 500