Making a Constellation
Time limit1sMemory limit512 MB
Attach N tree decorations in order, each red root to the shallowest unconnected blue star, and find the minimum possible maximum depth.
Problem
A rainbow-colored star is shining in the night sky. Star wants to use this rainbow star and decorations to build a constellation and decorate the night sky beautifully.
The decorations have the following properties.
- Each decoration is a tree made of stars and strings. A string connects two different stars.
- Each decoration has exactly one red star. When the red star is treated as the root, every leaf star is blue. All other stars are white.
- Each star has a number from 1 to . The red star is always numbered 1, and every star has a different number.
In a constellation, choose a star and move from the rainbow star to the chosen star along strings. The minimum number of strings passed on this path is the depth of the chosen star.
The length of a constellation is the maximum depth among all stars in it.
Star attaches the decorations one at a time, in the order received. Every decoration must be attached in that order and follow these rules.
- The red star of the first decoration is connected to the rainbow star with a string.
- For the -th decoration (), connect its red star with a string to a blue star that has the minimum depth among the blue stars in the constellation built from the first decorations and is not yet connected to another decoration. If several such stars exist, choose any one.
Star wants to build a constellation that follows these rules and has the minimum possible length. Find the minimum length of a constellation that Star can build.
Input
The first line contains the number of decorations . ()
From the second line, the information about the decorations is given in the order Star receives them.
For each decoration, the first line gives the number of stars . The following lines each give two star numbers and connected by a string, separated by a space. (; ; )
The total number of stars in all decorations does not exceed 500000.
Output
Print the minimum length of a constellation that Star can build.