#P7238. 迷失森林

迷失森林

Description

First, you are given a tree with nn nodes rooted at 11, called the “unit tree”.

Now there are nn trees with exactly the same structure as the “unit tree”. You need to connect these nn trees together using n1n - 1 edges.

For convenience, use the notation (a,b)(a,b) to represent: in the tree represented by node aa, the node whose label is bb.

The connection method is as follows:

  1. Arrange the nn trees according to the structure of the “unit tree”.

  2. For each i(1<in)i(1<i\leq n), connect nodes (i,1)(i,1) and (fi,fi)(f_i,f_i). Here fif_i is the parent of node ii in the “unit tree”.

After connecting the nn trees, you get a tree with n2n^2 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 nn, the number of nodes in the “unit tree”.

The next n1n - 1 lines each contain two positive integers u,vu,v, 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 (3,4)(3,4) and (4,4)(4,4) contains 99 nodes, and its length is 99.

Sample #3: As shown in the figure below, choose (6,6)(6,6) and (9,9)(9,9), and it contains 1111 nodes.

In fact, for any two orange nodes whose lowest common ancestor is 11, the answer is always 1111.

Constraints

This problem uses bundled testdata.

Subtask ID Score nn\le Special Property
11 1010 ×\times
22 1212 10610^6 v=u+1v=u+1
33 66 u=1u=1
44 1818 =2k(kZ)=2^k(k\in\mathbf{Z}) u=v2u=\left\lfloor\dfrac{v}{2}\right\rfloor
55 2727 10310^3 The shape of the tree is random.
66 10610^6 ×\times

For 100%100\% of the testdata: 1n1061\leq n\leq10^6, and node labels are between 11 and nn.

This problem may be slightly tight on constants.

The time limit has been reduced to 1s1\text{s} for the following reasons:

  • The discussion board believes this problem is the same as an existing one. To prevent the so-called “enhanced version” O(nlogn)O(n\log n) solution from directly passing this problem, the time limit was reduced.

  • The optimal solution can run within 200ms200\text{ms}.

  • Because the author is lazy and did not want to change n106n107n\le10^6\rightarrow n\le10^7, they can only reduce the time limit to block the so-called original-problem solution (which is actually incorrect).

Translated by ChatGPT 5