#P7238. 迷失森林
迷失森林
Description
First, you are given a tree with nodes rooted at , called the “unit tree”.
Now there are trees with exactly the same structure as the “unit tree”. You need to connect these trees together using edges.
For convenience, use the notation to represent: in the tree represented by node , the node whose label is .
The connection method is as follows:
-
Arrange the trees according to the structure of the “unit tree”.
-
For each , connect nodes and . Here is the parent of node in the “unit tree”.
After connecting the trees, you get a tree with nodes. Find the maximum number of nodes on a simple path between two nodes in this tree.
Input Format
The first line contains a positive integer , the number of nodes in the “unit tree”.
The next lines each contain two positive integers , representing an edge in the “unit tree”.
Output Format
Output one line with a positive integer, the maximum number of nodes on a simple path between two nodes in the tree.
3
1 2
1 3
5
5
1 2
1 3
2 4
2 5
9
9
1 2
1 3
1 4
1 5
2 6
2 7
5 8
5 9
11
5
1 2
2 3
2 4
2 5
8
Hint
Sample Explanation
Sample #1

Sample #2: As shown in the figure below, the path with endpoints and contains nodes, and its length is .

Sample #3: As shown in the figure below, choose and , and it contains nodes.

In fact, for any two orange nodes whose lowest common ancestor is , the answer is always .
Constraints
This problem uses bundled testdata.
| Subtask ID | Score | Special Property | |
|---|---|---|---|
| The shape of the tree is random. | |||
For of the testdata: , and node labels are between and .
This problem may be slightly tight on constants.
The time limit has been reduced to for the following reasons:
-
The discussion board believes this problem is the same as an existing one. To prevent the so-called “enhanced version” solution from directly passing this problem, the time limit was reduced.
-
The optimal solution can run within .
-
Because the author is lazy and did not want to change , they can only reduce the time limit to block the so-called original-problem solution (which is actually incorrect).
Translated by ChatGPT 5
京公网安备 11011102002149号