eolymp
bolt
Try our new interface for solving problems

Tree

Given: an ordered rooted tree \textbf{T} and a list of its modifications. The modifications are the following: cut off node with the given number along with the subtree from its current parent node and add it as a rightmost child node to the other node. You must determine the modification after which the tree becomes isomorphic to one of the previous trees. In a rooted ordered tree each non-leaf node has an established order of child nodes. Two trees are considered isomorphic if the following is retained in bijective mapping: \begin{itemize} \item the parent-child relation, as in "the node s is a child of the node \textbf{f}", \item the order of children for every node. \end{itemize} \InputFile The first line of the input file contains two integers: \textbf{N} is the number of nodes in the tree and \textbf{M} is the number of modifications (\textbf{2} ≤ \textbf{N} ≤ \textbf{100000}, \textbf{1} ≤ \textbf{M} ≤ \textbf{100000}). The tree nodes are numbered by integer numbers from \textbf{1} to \textbf{N}. The root node has the number \textbf{1}. The following \textbf{N} lines contain ordered lists of child nodes for the nodes of the initial tree. Each \textbf{i}-th list is described by the number \textbf{K_i} of children of the \textbf{i}-th node and by the sequence of its child node numbers (\textbf{0}≤ \textbf{K_i} ≤ \textbf{N--1}). The remaining \textbf{M} lines contain the description of the tree modifications. Each modification is defined by two integers \textbf{S_j} and \textbf{F_j} -- -- the number of the node being cut from the parent along with its subtree and the number of the node to which it is added as its rightmost child (\textbf{2} ≤ \textbf{S_j} ≤ \textbf{N}, \textbf{1} ≤ \textbf{F_j} ≤ \textbf{N}). It is guaranteed that the node \textbf{F_j} is not a descendant of the node \textbf{S_j} and does not coincide with it. \OutputFile If all \textbf{M+1} resulting trees are different, the output file must contain the number \textbf{--1}. Otherwise it must contain two integers \textbf{A} and \textbf{B} separated by a space --- the numbers of two isomorphic trees (\textbf{0} ≤ \textbf{A} < \textbf{B} ≤ \textbf{M}). If there are several pairs of isomorphic trees, the output file must contain the pair with the lowest number \textbf{B}. Trees are numbered in the following way: the initial tree has the number \textbf{0}, and \textbf{j}-th modification results in \textbf{j}-th tree (\textbf{1} ≤ \textbf{j} ≤ \textbf{M}). \textbf{Comment} Below are some of the trees for the first test from the given data. \includegraphics{https://static.e-olymp.com/content/9f/9f17ae0b44aac616845d560e0152ed85cd88b030.jpg}
Time limit 1 second
Memory limit 64 MiB
Input example #1
12 15
2 5 2
0
2 11 9
2 8 7
2 3 4
0
0
0
0
0
3 6 12 10
0
3 1
8 2
7 2
6 4
10 4
12 4
9 5
6 8
10 8
12 8
8 5
9 2
4 5
6 9
6 8
Output example #1
7 13
Source XIII All-Siberian Programming Contest named after I.V.Pottosin, November 11, 2012