#K44812. Tree Diameter

    ID: 27615 Type: Default 1000ms 256MiB

Tree Diameter

Tree Diameter

You are given an undirected tree with n nodes. The nodes are labeled from 1 to n. The tree is represented by a list of n-1 edges. Your task is to compute the diameter of the tree.

The diameter of a tree is defined as the number of nodes on the longest path between any two nodes in the tree. Formally, if the longest path in the tree goes through nodes \(v_1, v_2, \dots, v_k\), then the diameter is \(k\). Note that if the tree consists of a single node, the diameter is 1.

Input Format: The first line contains an integer n which represents the number of nodes. If n > 1, the following n-1 lines each contain two integers u and v representing an edge between nodes u and v.

Example:
For a tree with 5 nodes and edges:

5
1 2
1 3
2 4
2 5

The longest path is from node 4 to node 5 passing through nodes 2 and 1 (or a similar variation), including 4 nodes in total.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains a single integer n — the number of nodes in the tree.
  • If n > 1, the next n-1 lines each contain two space-separated integers u and v denoting an edge between node u and node v.

outputFormat

Print to stdout a single integer denoting the diameter of the tree (i.e. the number of nodes on the longest path).

## sample
5
1 2
1 3
2 4
2 5
4

</p>