All in the family
题号:NC227006
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 1024 M,其他语言2048 M
64bit IO Format: %lld

题目描述

The scientists working at the Family Genetics Institute are tracing the spread of a hereditary disease through family trees.  They have started by listing the family members that have the disease, and the parent who passed the disease on to each child (we will assume that each child gets the disease from only one parent).  However, the scientists are confused about the names of different family relationships.  Parents, grandparents, and siblings they have a handle on.  But a relationship like "third cousin, twice removed" has been hard for them to wrap their heads around.   After much discussion they came up with some definitions that cleared things up.
Suppose we have two people conveniently named  and  and their closest common ancestor is named  (what are the odds!).  We say that  is  generations removed from / if there are  direct descendants from  ending with .  Thus if  is the daughter of  she is  generation removed; if she is the granddaughter of  she is  generations removed, and so on.  Any person is  generations removed from themselves.
Now let  be  generations removed from  and  be  generations removed from  where .  We can determine the relationship between  and  using the following rules:
1. if  then  is the child of  if  or the  grandchild of  if .
2. if  then  and  are siblings if  or -th cousins if .
3. if  then  and  are -th cousins  times removed.
Notice that if  and  we get the interestingly named "th cousins,  time removed" for the relationships we typically describe as "aunt/uncle" or "niece/nephew".
Figure 1 below shows some examples for two new people named (what else)  and .
The scientists have given you the description of a family tree and pairs of people in the tree and have asked you to determine the relationships between members of each pair.

输入描述:

Input begins with a line containing two positive integers   () specifying the  number of tree descriptions (described below) and the number of query pairs.  Following these are  lines, each with one tree description.  Each tree description will be of the form     indicating that person  has  children named  through .  All names are unique and contain only alphabetic characters.  Tree descriptions may be given in any order (i.e., the root of the entire tree may not necessarily be in the very first tree description).  No name will appear more than once as  in the tree descriptions.  All the tree descriptions will combine to form exactly one tree, and the tree will have at least  nodes and at most  nodes.
Following this are  lines of the form   where  and both names are guaranteed to be in the tree.


输出描述:

Output the relationship for each pair of people, one per line, using the formats shown in Figure 1.  Always output 's name first for each pair except when  is the direct descendant of  (as in the first example in Figure 1).  For the -th ordinal number output th except for   in which case you should output 1st, 2nd, 3rd, 21st, 22nd, 23rd, 31st, 32nd, 33rd, etc.  Also be sure to use times for all times removed except one, where you should use the word time.
示例1

输入

复制
4 5
Horatio 1 Irene
Chris 2 Gina Horatio
Alice 3 Dan Emily Frank
Bob 2 Alice Chris
Irene Bob
Dan Frank
Chris Emily
Alice Chris
Dan Irene

输出

复制
Irene is the great grandchild of Bob
Dan and Frank are siblings
Chris and Emily are 0th cousins, 1 time removed
Alice and Chris are siblings
Dan and Irene are 1st cousins, 1 time removed
示例2

输入

复制
4 6
A 4 B C D E
H 3 I J K
C 2 F G
D 1 H
G C
H A
F G
F H
F K
B K

输出

复制
G is the child of C
H is the grandchild of A
F and G are siblings
F and H are 1st cousins
F and K are 1st cousins, 1 time removed
B and K are 0th cousins, 2 times removed