{"trustable":true,"prependHtml":"\u003cstyle type\u003d\"text/css\"\u003e\n div.illustration {\n float: right;\n padding-left: 20px;\n }\n div.illustration .illustration {\n width: 100%;\n border-radius: 4px;\n }\n pre {\n display: block;\n margin: 0 0 10px;\n font-size: 13px;\n line-height: 1.42857143;\n color: #333;\n word-break: break-all;\n word-wrap: break-word;\n }\n\u003c/style\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027\\\\[\u0027, right: \u0027\\\\]\u0027, display: true}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\n \u003cp\u003eIn graph theory, a \u003cem\u003etree\u003c/em\u003e is a connected, undirected\n simple graph with no cycles. A tree with \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e nodes always has \u003cspan class\u003d\"tex2jax_process\"\u003e$n - 1$\u003c/span\u003e edges.\u003c/p\u003e\n\n \u003cp\u003eA \u003cem\u003epath\u003c/em\u003e in a tree is a sequence of distinct edges\n which are connected (each pair of consecutive edges in the path\n share a vertex).\u003c/p\u003e\n\n \u003cp\u003eConsider a tree with \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e vertices and \u003cspan class\u003d\"tex2jax_process\"\u003e$n-1$\u003c/span\u003e edges. You can color each edge\n in one of \u003cspan class\u003d\"tex2jax_process\"\u003e$k$\u003c/span\u003e colors.\u003c/p\u003e\n\n \u003cp\u003eAn assignment of colors to edges is a \u003cem\u003erainbow\n coloring\u003c/em\u003e if in every path of 2 or 3 edges, the colors of\n the edges are different. (i.e., every two consecutive edges\n have different colors, and every three consecutive edges have\n different colors).\u003c/p\u003e\n\n \u003cp\u003eGiven a tree and the number of colors \u003cspan class\u003d\"tex2jax_process\"\u003e$k$\u003c/span\u003e, find the number of rainbow\n colorings modulo \u003cspan class\u003d\"tex2jax_process\"\u003e$1\\, 000\\, 000\\,\n 009$\u003c/span\u003e.\u003c/p\u003e\n\n \u003ch2\u003eInput\u003c/h2\u003e\n\n \u003cp\u003eThe first line of input gives the number of test cases,\n \u003cspan class\u003d\"tex2jax_process\"\u003e$C$\u003c/span\u003e. Then for each of the\n \u003cspan class\u003d\"tex2jax_process\"\u003e$C$\u003c/span\u003e cases, there will\n be:\u003c/p\u003e\n\n \u003cul class\u003d\"itemize\"\u003e\n \u003cli\u003e\n \u003cp\u003eOne line containing two integers \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e and \u003cspan class\u003d\"tex2jax_process\"\u003e$k$\u003c/span\u003e, where \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e is the number of nodes in the\n tree, and \u003cspan class\u003d\"tex2jax_process\"\u003e$k$\u003c/span\u003e is the\n number of colors available.\u003c/p\u003e\n \u003c/li\u003e\n\n \u003cli\u003e\n \u003cp\u003e\u003cspan class\u003d\"tex2jax_process\"\u003e$n - 1$\u003c/span\u003e lines, one\n for each edge, containing two integers \u003cspan class\u003d\"tex2jax_process\"\u003e$x, y$\u003c/span\u003e, indicating that the edge\n is between node \u003cspan class\u003d\"tex2jax_process\"\u003e$x$\u003c/span\u003e\n and node \u003cspan class\u003d\"tex2jax_process\"\u003e$y$\u003c/span\u003e. Nodes\n are numbered from 1 to \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e.\u003c/p\u003e\n \u003c/li\u003e\n \u003c/ul\u003e\n\n \u003cp\u003eYou may assume that \u003cspan class\u003d\"tex2jax_process\"\u003e$1 \\leq k\n \\leq 1\\, 000\\, 000\\, 000$\u003c/span\u003e and all the node numbers are\n between 1 and \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e,\n inclusive. Furthermore, \u003cspan class\u003d\"tex2jax_process\"\u003e$2 \\leq n\n \\leq 500$\u003c/span\u003e.\u003c/p\u003e\n\n \u003ch2\u003eOutput\u003c/h2\u003e\n\n \u003cp\u003eFor each test case, output one line. That line should\n contain \"Case #\u003cspan class\u003d\"tex2jax_process\"\u003e$X$\u003c/span\u003e:\n \u003cspan class\u003d\"tex2jax_process\"\u003e$Y$\u003c/span\u003e\", where \u003cspan class\u003d\"tex2jax_process\"\u003e$X$\u003c/span\u003e is 1-based number of the case, and\n \u003cspan class\u003d\"tex2jax_process\"\u003e$Y$\u003c/span\u003e is the answer for that\n test case.\u003c/p\u003e\n\n \u003ch2\u003eSample 1\u003c/h2\u003e\u003cbody\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e2\n4 10\n1 2\n1 3\n1 4\n5 3\n1 2\n2 3\n3 4\n4 5\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003eCase #1: 720\nCase #2: 6\n\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/body\u003e\n "}}]}