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 TT with n (n2)n\ (n \ge 2) nodes, where the root is node 11. A leaf node is defined as any node whose degree is exactly 11, excluding the root. The figure below shows an example of a tree TT, where the set of leaf nodes is {3,4,5}\{3, 4, 5\}.

Next, using this tree, Jiulian constructs a sequence AA:

  • 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 TT.
  • 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 AA.

Furthermore, based on the sequence AA, Jiulian defines the distance d(u,v)d(u, v) between two leaf nodes u,vu, v: suppose uu is the ii-th element in AA and vv is the jj-th element, then d(u,v)=min(ij,Aij)d(u, v) = \min(|i − j|, |A| − |i − j|). Here A|A| is the length of the sequence, i.e. the number of leaves of TT, and i,ji, j are their positions, counted starting from 11.

In the example above, the sequence AA is {4,5,3}\{4, 5, 3\}, where d(3,5)=d(3,4)=d(4,5)=1d(3, 5) = d(3, 4) = d(4, 5) = 1, and the positions of 3,4,53, 4, 5 are 3,1,23, 1, 2 respectively.

Finally, Jiulian gives a parameter KK. Using the rooted tree TT and the sequence AA, we can construct an undirected graph GG with nn vertices, without multiple edges and without self-loops. There is an edge between two distinct vertices u,vu, v if and only if they satisfy at least one of the following conditions:

  • There is an edge connecting uu and vv in the tree TT.
  • In the tree TT, both uu and vv are leaf nodes, and d(u,v)Kd(u, v) \le K.

When K=1K = 1 or 22, the graph GG obtained from the example above is as follows:

Now Jiulian wants you to compute the number of distinct Hamiltonian cycles in GG. The answer may be large, so output it modulo 998244353998244353.

Here are some additional definitions:

  • A Hamiltonian cycle HH of an undirected graph GG without multiple edges and self-loops is a subset of edges of GG such that for every vertex, there are exactly two distinct incident edges in HH, and any two vertices can be connected directly or indirectly using edges in HH.
  • Two Hamiltonian cycles H1,H2H_1, H_2 of an undirected graph GG without multiple edges and self-loops are different if and only if there exists an edge ee such that ee is in H1H_1 but not in H2H_2.

Input Format

Read input from file polygon.in.

The first line contains two integers n,Kn, K, representing the number of nodes in the tree TT and the parameter KK chosen by Jiulian.

The second line contains n1n - 1 integers fi (1fii)f_i\ (1 \le f_i \le i), where fif_i means that there is an edge (fi,i+1)( f_i, i + 1) in the tree TT.

Output Format

Write output to file polygon.out.

Output one line with one integer, representing the number of Hamiltonian cycles modulo 998244353998244353.

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 (1,2,4,5,3)(1, 2, 4, 5, 3) and (1,2,5,4,3)(1, 2, 5, 4, 3), respectively.

Subtasks

::cute-table{tuack=4}

ID nn KK Special Property ID nn KK Special Property
11 5\le 5 3\le 3 None 1111 1000\le 1000 2\le 2 A
22 10\le 10 ^ 1212 ^ ^ ^
33 15\le 15 1313
44 20\le 20 1414 None
55 1000\le 1000 =1=1 A 1515 ^
66 ^ ^ 1616
77 ^ 1717 3\le 3 A
88 None 1818 ^ ^
99 ^ 1919 None
1010 2020 ^

Property A means that every node in the tree has at most two children.

For all testdata, it is guaranteed that 1fii1 \leq f_i \leq i, 2n10002 \leq n \leq 1000.

Translated by ChatGPT 5