Hide

Problem F
Bonsai

Many people love to grow bonsai trees because they say that it is “difficult” and “harmonious”. That’s not why Torstina grows bonsai trees. She only wants to sell them and make lots of money so she can buy lots of cherimoya. She has just planted a new nodule and is really craving cherimoya. Therefore she is wonder how many years she has to wait before she can have a bonsai tree that her customers would want.

Bonsai trees have $2\leq N \leq 10^5$ nodules and $N-1$ branches. The nodules are numbered from $0$ to $N-1$. All bonsai trees begin with a little nodule that you put in the ground. Each year it grows a new branch from each nodule and forms a new nodule at the branch’s end. One can also cut off branches from the tree whenever they wish. She reminds you that it doesn’t matter which nodule is in the ground.

Given the bonsai tree that a customer wants, how many years must Torstina wait before she has grown a tree that is exactly the same?

Input

The first line contains an integer $2 \leq N \leq 10^5$, the number of nodules in the customer’s bonsai tree. The following $N$ lines describe the bonsai tree according to the following: On line $i$, first there is an integer $0 < m_ i < N$, the number of branches that leave nodule $i$. After that follow $m_ i$ integers, the nodules that are connected to nodule $i$.

Output

Print an integer $A$ which is the number of years it takes for Torstina to grow the bonsai tree her customer wants.

Points

Your solution will be tested on several test case groups. To get the points for a group, it must pass all the test cases in the group.

Group

Point value

Bounds

$1$

$9$

$m_ i\leq 2$, it’s best to begin growing the tree with node $0$.

$2$

$11$

$m_ i \leq 3$, it’s best to begin growing the tree with node $0$.

$3$

$18$

$A \leq 15$, it’s best to begin growing the tree with node $0$.

$4$

$22$

It’s best to begin growing the tree with node $0$.

$5$

$12$

$N \leq 250$.

$6$

$28$

No additional constraints.

Explanation of test case $1$

We can grow the tree in $2$ years if we let it grow according to the picture below.

\includegraphics[width=0.5\textwidth ]{Bonsai_tree}
Figure 1: One of the two ways one can grow the bonsai tree in sample $1$ in two years.
Sample Input 1 Sample Output 1
4
2 1 2
1 0
2 0 3
1 2
2
Sample Input 2 Sample Output 2
5
3 1 2 3
2 0 4
1 0
1 0
1 1
3
Sample Input 3 Sample Output 3
7
4 1 2 3 4
3 0 5 6
1 0
1 0
1 0
1 1
1 1
4

Please log in to submit a solution to this problem

Log in