luogu#P4776. [NOI2018] 多边形
[NOI2018] 多边形
Problem Description
Jiulian is a girl who likes to create problems.
After this year’s World Final ended, Jiulian became very interested in computational geometry, so she decided to set a computational geometry problem in the NOI contest. This is a problem whose only relation to computational geometry is its title.
First, Jiulian gives a rooted tree with nodes, where the root is node . A leaf node is defined as any node whose degree is exactly , excluding the root. The figure below shows an example of a tree , where the set of leaf nodes is .

Next, using this tree, Jiulian constructs a sequence :
- Starting from the root, perform a depth-first traversal of the whole tree. During the traversal, visit children in increasing order of their labels. This yields a DFS order of the tree .
- Then, according to their order of appearance in the DFS order from front to back, Jiulian lines up all leaf nodes to obtain a sequence .
Furthermore, based on the sequence , Jiulian defines the distance between two leaf nodes : suppose is the -th element in and is the -th element, then . Here is the length of the sequence, i.e. the number of leaves of , and are their positions, counted starting from .
In the example above, the sequence is , where , and the positions of are respectively.
Finally, Jiulian gives a parameter . Using the rooted tree and the sequence , we can construct an undirected graph with vertices, without multiple edges and without self-loops. There is an edge between two distinct vertices if and only if they satisfy at least one of the following conditions:
- There is an edge connecting and in the tree .
- In the tree , both and are leaf nodes, and .
When or , the graph obtained from the example above is as follows:

Now Jiulian wants you to compute the number of distinct Hamiltonian cycles in . The answer may be large, so output it modulo .
Here are some additional definitions:
- A Hamiltonian cycle of an undirected graph without multiple edges and self-loops is a subset of edges of such that for every vertex, there are exactly two distinct incident edges in , and any two vertices can be connected directly or indirectly using edges in .
- Two Hamiltonian cycles of an undirected graph without multiple edges and self-loops are different if and only if there exists an edge such that is in but not in .
Input Format
Read input from file polygon.in.
The first line contains two integers , representing the number of nodes in the tree and the parameter chosen by Jiulian.
The second line contains integers , where means that there is an edge in the tree .
Output Format
Write output to file polygon.out.
Output one line with one integer, representing the number of Hamiltonian cycles modulo .
5 1
1 1 2 2
2
Hint
Sample Explanation
This sample is exactly the same as the example in the statement. The orders of nodes visited by the two Hamiltonian cycles are and , respectively.
Subtasks
::cute-table{tuack=4}
| ID | Special Property | ID | Special Property | ||||
|---|---|---|---|---|---|---|---|
| None | A | ||||||
| ^ | ^ | ^ | ^ | ||||
| None | |||||||
| A | ^ | ||||||
| ^ | ^ | ||||||
| ^ | A | ||||||
| None | ^ | ^ | |||||
| ^ | None | ||||||
| ^ | |||||||
Property A means that every node in the tree has at most two children.
For all testdata, it is guaranteed that , .
Translated by ChatGPT 5