#P6653. [YsOI2020] 造林

    ID: 7422 远端评测题 1000~4000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>树形数据结构O2优化树形 DP哈希 hashing

[YsOI2020] 造林

Background

“Continuing from before.”

In response to the call, Ysuperman decided to plant a forest outside the kindergarten.

Well, if that is the case, Ysuperman will be able to play games with the kids in this hot summer.

Problem Description

To carry out environmental protection work, Ysuperman bought a batch of trees, and they all look the same. Since the trees have not been planted yet, they do not have roots, so they can be considered unrooted trees.

Ysuperman thinks it is too boring to plant trees that all look the same, so they asked a gardening company to plan it. The gardening company provided a method—“grafting”.

The definition of the “grafting” operation is given below:

Define a “leaf node” as a node in the tree with degree 11.

A “grafting” operation means: adding a new “leaf node” to an unrooted tree.

For example:

Figure 2 is the tree obtained by performing one valid grafting operation on the tree in Figure 1, and Figure 3 is also a tree obtained by performing one valid grafting operation on the tree in Figure 1.

Also, we know that a tree has a basic property: “species”.

The “species” of a tree is the multiset formed by the maximum subtree size of each node.

Two trees have different “species” if and only if the multisets formed by the maximum subtree size of each node are different.

Here, the maximum subtree size of a node means: after deleting this node, the number of nodes contained in the largest connected component.

For example:

The multiset formed by the maximum subtree size of each node in the tree in Figure 4 is: {2,2,3,3} \{ 2,2,3,3 \}
The multiset formed by the maximum subtree size of each node in the tree in Figure 5 is: {2,2,3,3} \{ 2,2,3,3 \}
The multiset formed by the maximum subtree size of each node in the tree in Figure 6 is: {1,3,3,3} \{ 1,3,3,3 \}
So, the tree in Figure 4 has the same “species” as the tree in Figure 5, and a different “species” from the tree in Figure 6.

Ysuperman wants to know: with exactly one “grafting” operation, how many different “species” of trees can be constructed, and for each “species”, how many different “grafting” methods can construct it. Please output, from small to large, the number of “grafting” methods for each “species”.

Two “grafting” plans are different if and only if, in the “grafting” operation, the node directly connected to the newly added “leaf node” is different.

Input Format

The first line contains an integer nn.
The next n1n-1 lines each contain two integers u,vu,v, indicating that there is an undirected edge between uu and vv.

Output Format

The first line contains an integer cntcnt, indicating how many different “species” of trees can be constructed.
From the second line to the (cnt+1)(cnt+1)-th line, output, from small to large, the number of “grafting” methods for each “species”.

5
1 2
2 3
3 4
4 5

3
1
2
2

7
1 2
1 3
2 4
2 5
3 6
3 7

3
1
2
4

25
15 9
22 15
23 22
25 15
13 23
6 22
12 15
1 23
19 13
18 9
11 15
17 1
4 25
3 1
8 9
20 1
10 18
21 20
16 8
2 22
24 1
7 19
5 16
14 7

17
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
3

Hint

This problem uses bundled tests.

Sample Explanation 1

It is possible to construct 11 kind of tree with “species” {2,4,4,5,5,5}\{2,4,4,5,5,5\}.
It is possible to construct 22 kinds of trees with “species” {3,3,4,5,5,5}\{3,3,4,5,5,5\}.
It is possible to construct 22 kinds of trees with “species” {3,3,4,4,5,5}\{3,3,4,4,5,5\}.

Sample Explanation 2

It is possible to construct 11 kind of tree with “species” {3,5,5,7,7,7,7,7}\{3,5,5,7,7,7,7,7\}.
It is possible to construct 22 kinds of trees with “species” {4,4,5,7,7,7,7,7}\{4,4,5,7,7,7,7,7\}.
It is possible to construct 44 kinds of trees with “species” {4,4,5,6,7,7,7,7}\{4,4,5,6,7,7,7,7\}.

For 100%100\% of the data, 1n21061 \le n\le2\cdot 10^6.

Define a “path” as a tree in which all nodes have degree at most 22.
Define a “star” as a tree that contains n1n-1 “leaf nodes”.

Special Property 1: The shape of the tree is guaranteed to be a “path”.
Special Property 2: The shape of the tree is guaranteed to be a “star”.
Special Property 3: The shape of the tree is guaranteed to be a complete binary tree.

subtask nn Special Property Score Time Limit
1 2106\le 2\cdot 10^6 2 4s
2 1 3
3 300\le 300 None 5 1s
4 2106\le 2\cdot10^6 3 7 4s
5 5000\le 5000 None 23 1s
6 5104\le 5\cdot10^4 29 2s
7 2106\le 2\cdot10^6 31 4s

Notes

If you do not know what a complete binary tree means, Ysuperman provides a link: Link.

The input and output are large, so please use a fast I/O method.

If you use a recursive algorithm that requires a large amount of stack space, you can first run sudo su locally (under NOI linux) to get permission, and then use the command ulimit -s unlimited to enable an unlimited stack.

The problem is not difficult.

Translated by ChatGPT 5