#K83042. Longest Path in a Tree

    ID: 36109 Type: Default 1000ms 256MiB

Longest Path in a Tree

Longest Path in a Tree

You are given one or more trees. For each tree, you must compute the diameter of the tree. The diameter is defined as the length of the longest path between any two nodes in the tree. In mathematical terms, if the tree is represented by a set of vertices \(V\) and edges \(E\), the diameter \(D\) is:

\(D = \max_{u,v \in V} d(u,v)\)

where \(d(u,v)\) is the shortest distance between nodes \(u\) and \(v\). Each tree is given in the form of an undirected graph with \(n\) nodes and \(n-1\) edges and is guaranteed to be connected. The input ends with a line containing a single zero (0).

Your task is to output the diameter for each tree.

inputFormat

The input is read from standard input (stdin) and consists of multiple datasets. Each dataset has the following format:

  • The first line contains an integer \(n\) (\(1 \leq n \leq \) some upper limit) representing the number of nodes in the tree.
  • The next \(n-1\) lines each contain two integers \(u\) and \(v\), indicating an edge between nodes \(u\) and \(v\).

The input terminates with a line containing a single 0, which should not be processed.

outputFormat

For each dataset, output a single integer on a new line representing the diameter (the length of the longest path) of the tree.

## sample
3
1 2
1 3
2
1 2
0
2

1

</p>