Under construction

Still being built. Things may change

Tree Product

Time limit2sMemory limit256 MB

Summary
Given n rooted trees, find the orderings of their tree product that give the maximum and the minimum diameter.
Level

Medium7 of 10

Topics
Tree, Sorting, Greedy
Solved
No attempts yet

Problem

Given nn rooted trees T1,T2,…,TnT_1,T_2,\ldots,T_n, find two permutations p1,p2,…,pnp_1,p_2,\dots,p_n and q1,q2,…,qnq_1,q_2,\ldots,q_n such that the diameter of Tp1×Tp2×…×TpnT_{p_1} \times T_{p_2} \times \ldots \times T_{p_n} is maximum and the diameter of Tq1×Tq2×…×TqnT_{q_1} \times T_{q_2} \times \ldots \times T_{q_n} is minimum.

For two rooted trees AA and BB, their tree product T=A×BT = A \times B is defined as follows: copy tree AA, and then for each vertex xx in it, make a copy of BB and merge its root with vertex xx. See the table below for an example:

It can be shown that tree product is associative: (A×B)×C=A×(B×C)(A \times B) \times C = A \times (B \times C). So the parentheses in a product of three or more trees can be omitted.

Recall that:

  • A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex vv is the last vertex different from vv on the path from the root to vv.
  • The diameter of a rooted tree is the length of the longest simple path in the tree, where the length of a path is the number of edges in the path.

Input

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1≤n≤106)(1 \le n \le 10^6), indicating the number of rooted trees.

Each of the next nn lines starts from an integer mim_i (1≤mi≤1051 \le m_i \le 10^5), indicating the number of vertices in the ii-th rooted tree. It is followed by mim_i integers pi,1,pi,2,…,pi,mip_{i,1},p_{i,2},\ldots,p_{i,m_i} (0≤pi,j≤mi0 \le p_{i,j} \le m_i) on the same line, where the jj-th of them denotes the parent of the jj-th vertex. The root of the tree has 00 as parent.

It is guaranteed that the sum of mim_i over all test cases does not exceed 10610^6.

Output

For each test case, output two integers: the maximum and the minimum diameter, in that order.

Hint

For the first sample test case, T1×T2×T3T_1 \times T_2 \times T_3 will provide the maximum diameter, while T3×T2×T1T_3 \times T_2 \times T_1 will provide the minimum diameter.

Examples1

  1. Example 1

    Input
    2
    3
    5 0 1 2 1 4
    3 2 0 2
    2 2 0
    2
    1 0
    1 0
    
    Expected output
    8 7
    0 0