eolymp
bolt
Try our new interface for solving problems
Problems

Tokens

Tokens

Consider a tree-graph. In each node of this tree one or more tokens can be placed. After the placement a node with two or more tokens can be selected and two tokens can be removed from the selected node and one token can be placed to any adjacent node to the selected one. Such operation can be repeated several times. Your task is to find the maximal number of tokens (modulo \textbf{M}) that can be placed on the tree nodes to fulfill the following condition: there exists at least one node, to which it is impossible to move a token applying given operations. \InputFile First line of input contains the quantity of tests \textbf{T} (\textbf{1} ≤ \textbf{T} ≤ \textbf{100}). First line of each test case contains two numbers: \textbf{N} (\textbf{2} ≤ \textbf{N} ≤ \textbf{30000}) -- the number of nodes in the tree and \textbf{M} (\textbf{2} ≤ \textbf{M^31--1} ≤ 2). Then \textbf{N}--\textbf{1} lines follows, each of which contains \textbf{2} adjacent node numbers (nodes are numbered from \textbf{1} to \textbf{N}) separated by space. \OutputFile Output \textbf{T} lines of the form "\textbf{Case} #\textbf{A}: \textbf{B}", where \textbf{A} is the number of test (beginning from \textbf{1}), \textbf{B} is the desired number for this test case.
Time limit 2 seconds
Memory limit 64 MiB
Input example #1
2
6 997
1 2
1 4
3 4
5 3
3 6
7 13
1 2
1 3
1 4
2 5
3 6
4 7
Output example #1
Case #1: 16
Case #2: 5