You are given two labeled and ordered rooted trees T and T' and would like to calculate the distance from T to T' , which is the minimum number of operations you can perform on T to make it equivalent to T'. For each operation you can choose to do one of three things:
We now describe what we mean by deletion and insertion of non-root nodes in T. When deleting a non-root node w with d children, let u be its parent and suppose w is u's i-th child. Then the first child of w becomes u's i-th child, the second child of w becomes u's (i+1)-st child, etc. For j<i, the j-th child of u remains the same, but for all j>i, the child which was formerly the j-th child of u now becomes its (j+d-1)-st child (they get "shifted over" due to the insertion of w's children into u's child list). To insert a non-root node w into the tree, we can choose any node u to be its parent, and we can choose any contiguous subsequence (possibly empty) of u's children to become w's children, putting w in their place. When inserting a node, we can give it any label we want at the time of insertion.
The root of T can never be deleted, and you can never insert a new node above the root to become the old root's parent. You can, however, change the label of the root.
The first line of the input file contains an integer indicating the number of test cases.
For each test case, the first line contains n and m separated by a space, the sizes of the trees T and T', respectively (1<= n,m<=60). The next n lines describe T. On the i-th line is a description of the i-th node in the tree: its label, the number of children it has, then a list of its children in order from first to last, all space-separated. The next m lines similarly describe T'. Labels are always nonnegative integers. The root of each tree is the node which is not the child of any other node in the tree.
For each test case output the minimum number of operations that can be performed on T to make it equivalent to T' on a single line.
1 3 2 6 0 1 2 0 2 4 0 2 1 1 4 0
2
Migrated from old NTUJ.
MIT Individual 2008
No. | Testdata Range | Score |
---|