#P4672. Diameter of a Tree‐Mirrored Graph

    ID: 17918 Type: Default 1000ms 256MiB

Diameter of a Tree‐Mirrored Graph

Diameter of a Tree‐Mirrored Graph

Consider a rooted tree \(T\) (i.e., a connected undirected acyclic graph) and let \(S\) be an identical copy of \(T\). A new graph is constructed by taking the union of \(T\) and \(S\) and merging the corresponding leaf nodes (with the exception of the root). Such a graph is called a tree-mirrored graph.

Your task is to compute the diameter of the tree-mirrored graph. It can be shown that if the maximum distance (in number of edges) from the root to any leaf in \(T\) is \(h\), then the diameter of the tree-mirrored graph is \(2h\). (Recall that the diameter of a graph is the maximum distance between any two vertices.)

You are given the original tree \(T\) in the following format. The tree is rooted at node 1. For each node \(i\) from 2 to \(N\), you are provided its parent.

inputFormat

The first line contains a single integer \(N\) (\(2 \leq N \leq 10^5\)) representing the number of nodes in the tree \(T\).

The second line contains \(N-1\) space-separated integers. The \(i^{th}\) integer (for \(i = 2,3,\dots,N\)) is the parent of node \(i\) in \(T\). It is guaranteed that \(T\) forms a valid rooted tree with node 1 as the root.

outputFormat

Output a single integer — the diameter of the corresponding tree-mirrored graph.

Note: Since it can be shown that the answer is always \(2h\), where \(h\) denotes the maximum depth (number of edges) from the root to any leaf in \(T\), your solution should compute this quantity accordingly.

sample

3
1 2
4