Tree Coloring
InterviewTime limit1sMemory limit128 MB
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 nodes and edges. You want to color the tree, that is, give every node one color from 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 .
Input
The first line contains the number of test cases (). Then test cases follow, each in this format.
- The first line of a test case contains two integers and . is the number of nodes () and is the number of colors you may use (). The nodes are numbered from to .
- Each of the next lines describes one edge of the tree with two integers and (; ; ), meaning an edge joins node and node .
Output
Print lines. For each test case, print the number of colorings modulo on one line, in input order.