Tree Coloring

Interview

Time limit1sMemory limit128 MB

Summary
Count colorings of an N-node tree with K colors so adjacent nodes differ, modulo 93563.
Level

Medium4 of 10

Topics
Dynamic programming, Tree, Combinatorics
Solved
No attempts yet

Problem

A tree is a connected undirected graph with no cycle. You are given a tree with NN nodes and N−1N-1 edges. You want to color the tree, that is, give every node one color from {1,2,…,K}\{1, 2, \dots, K\} so that two nodes joined by an edge always get different colors.

Write a program that counts how many colorings there are. The count can be very large, so print it modulo 9356393563.

Input

The first line contains the number of test cases TT (1≤T≤101 \le T \le 10). Then TT test cases follow, each in this format.

  • The first line of a test case contains two integers NN and KK. NN is the number of nodes (2≤N≤2002 \le N \le 200) and KK is the number of colors you may use (1≤K≤101 \le K \le 10). The nodes are numbered from 11 to NN.
  • Each of the next N−1N-1 lines describes one edge of the tree with two integers AA and BB (1≤A≤N1 \le A \le N; 1≤B≤N1 \le B \le N; A≠BA \ne B), meaning an edge joins node AA and node BB.

Output

Print TT lines. For each test case, print the number of colorings modulo 9356393563 on one line, in input order.

Examples1

  1. Example 1

    Input
    3
    2 3
    1 2
    4 2
    1 2
    1 3
    1 4
    5 5
    1 2
    2 3
    3 4
    4 5
    
    Expected output
    6
    2
    1280