#K37692. Tree Diameter

    ID: 26033 Type: Default 1000ms 256MiB

Tree Diameter

Tree Diameter

You are given multiple datasets, each representing an undirected tree. Your task is to compute the diameter of each tree. The diameter of a tree is the longest distance between any two vertices.

Each dataset is provided in the following format:

  1. The first line contains an integer \( n \) (\(2 \le n \le 500\)) indicating the number of vertices in the tree.
  2. The next \( n-1 \) lines each contain two space-separated integers \( a_i \) and \( b_i \) (\(1 \le a_i, b_i \le n\)), representing an edge connecting vertices \( a_i \) and \( b_i \).
  3. A dataset with \( n = 0 \) terminates the input.

For each dataset, output a single line that displays the diameter of the corresponding tree.

inputFormat

The input is read from standard input (stdin) and consists of multiple datasets. Each dataset starts with an integer \( n \) (\(2 \le n \le 500\)). Following this, there are \( n-1 \) lines, each containing two space-separated integers that represent an edge in the tree. The input ends with a line containing the integer 0.

outputFormat

For every dataset, print the diameter of the tree on a separate line to standard output (stdout).

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

2

</p>