#K63172. Tree Verification and Diameter Calculation

    ID: 31695 Type: Default 1000ms 256MiB

Tree Verification and Diameter Calculation

Tree Verification and Diameter Calculation

You are given several datasets representing a set of houses connected by pathways. For each dataset, you must determine whether the given pathways form a valid tree, and if so, compute the diameter of the tree. The diameter of a tree is defined as the longest shortest path between any two nodes in the tree, i.e., the maximum number of edges in a shortest path between any two nodes.

A valid tree must satisfy all of the following conditions:

  • It contains exactly N - 1 edges if there are N nodes.
  • It is connected; every node can be reached from any other node.
  • It does not contain any cycles.

For each dataset, if the pathways form a valid tree, print a line with "Yes D" where D is the diameter of the tree. Otherwise, print "No".

The input terminates when a dataset begins with 0.

inputFormat

The input is read from standard input and consists of multiple datasets. Each dataset starts with an integer N (N ≥ 1) indicating the number of nodes. The following N - 1 lines each contain two integers A and B, which represent a bidirectional pathway between nodes A and B. The input is terminated by a line containing a single 0.

outputFormat

For each dataset, output one line to standard output. If the provided pathways form a valid tree, print "Yes D" (without quotes), where D is the diameter of the tree. Otherwise, print "No".## sample

5
1 2
1 3
3 4
3 5
0
Yes 3

</p>