This page is still under construction.

Parts of this page are still being built. What you see may change.

Tree Similarity

Time limit3sMemory limit128 MB

Summary
Given two ordered rooted trees, find the minimum number of node relabel, delete, and insert operations to turn the first tree into the second.
Level

Hard8 of 10

Topics
Dynamic programming, Tree, DFS, Recursion
Solved
No attempts yet

Problem

You are given two labeled, ordered, rooted trees TT and T′T' (each node holds a number). Write a program that computes the distance between TT and T′T', defined as the minimum number of operations needed to make TT equal to T′T'.

There are three operations:

  1. Change the number written on one node of TT.
  2. Delete one non-root node of TT.
  3. Insert one node somewhere below the root of TT.

Because TT and T′T' are ordered trees, if a non-leaf node has cc children, those children are ordered from the 1st to the cc-th.

Two trees XX and YY are equal if their roots hold the same number and have the same number of children, and, for every ii, the subtree rooted at the ii-th child of XX is equal to the subtree rooted at the ii-th child of YY (i=1,2,…,ci = 1, 2, \dots, c).

Deletion and insertion are defined as follows. Suppose a non-root node ww has dd children, its parent is uu, and ww is the ii-th child of uu. When ww is deleted, its children move up into ww's place: the first child of ww becomes the ii-th child of uu, the second becomes the (i+1)(i+1)-th child of uu, and so on. The children of uu at positions j<ij < i keep their order, while every child at position j>ij > i is shifted to become the (j+d−1)(j + d - 1)-th child of uu.

To insert a non-root node ww, first choose the node uu that will become its parent. Then pick a contiguous subsequence of uu's children, make them the children of ww, and place ww in their position. When inserting ww, you may write any number on it.

You may not delete the root of TT or insert a new node above the root. However, you may change the number written on the root.

(For example, one may delete a node ww, or take the 2nd through 4th children of uu, group them as the children of a new node ww, and insert ww below uu.)

Input

The first line contains the node counts nn and mm of the two trees TT and T′T' (1≤n,m≤601 \le n, m \le 60).

The next nn lines describe TT. Nodes are numbered from 00 to n−1n-1; the (i+1)(i+1)-th of these lines describes node ii. Each line gives the number written on that node and its number of children, followed by the indices of its children in order (indices are 0-based).

The following mm lines describe T′T' in the same format.

Every number written on a node is a non-negative integer, and the root of each tree is the node that is not a child of any other node.

Output

Print the minimum number of operations needed to make TT equal to T′T'.

Examples3

  1. Example 1

    Input
    3 2
    6 0
    1 2 0 2
    4 0
    2 1 1
    4 0
    
    Expected output
    2
    
  2. Example 2

    Input
    1 1
    5 0
    5 0
    
    Expected output
    0
    
  3. Example 3

    Input
    1 1
    5 0
    7 0
    
    Expected output
    1