This page is still under construction.

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

All in the Family

Time limit1sMemory limit1024 MB

Summary
Given a family tree built from parent-child descriptions, answer queries about the relationship between two people using a defined cousin/removed naming scheme with correct ordinals.
Level

Medium7 of 10

Topics
Tree, DFS, String, Implementation
Solved
No attempts yet

Problem

The scientists at the Family Genetics Institute are tracing the spread of a hereditary disease through family trees. They start by listing the family members who have the disease and the parent who passed the disease on to each child (we assume each child gets the disease from only one parent). The scientists are confused about the names of different family relationships. Parents, grandparents, and siblings they understand. But a relationship like "third cousin, twice removed" is hard for them to wrap their heads around. After much discussion they come up with definitions that clear things up.

Suppose we have two people named AA and BB, and their closest common ancestor is named CC (what are the odds!). We say that AA is mm generations removed from CC if there are mm direct descendants from CC ending with AA. Thus if AA is the daughter of CC she is 11 generation removed; if she is the granddaughter of CC she is 22 generations removed, and so on. Any person is 00 generations removed from themselves.

Now let AA be mm generations removed from CC and BB be nn generations removed from CC, where m≤nm \leq n. We can determine the relationship between AA and BB using the following rules:

  1. if m=0m = 0 then BB is the child of AA if n=1n=1, or the \mboxgreatn−2\mbox{great}^{n-2} grandchild of AA if n>1n > 1.
  2. if 0<m=n0 < m = n then AA and BB are siblings if n=1n=1, or (n−1)(n-1)-th cousins if n>1n > 1.
  3. if 0<m<n0 < m < n then AA and BB are (m−1)(m-1)-th cousins (n−m)(n-m) times removed.

Notice that if m=1m = 1 and n=2n = 2 we get the interestingly named "00th cousins, 11 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) DD and EE.

# of generations removed from common ancestorRelationship
DDEE
0011EE is the child of DD
4400DD is the great great grandchild of EE
3333DD and EE are 22nd cousins
9988DD and EE are 77th cousins, 11 time removed
1144DD and EE are 00th cousins, 33 times removed

Figure 1: Some example relationships

The scientists give you the description of a family tree and pairs of people in the tree, and ask you to determine the relationship between the members of each pair.

Input

Input begins with a line containing two positive integers tt pp (t≤100,p≤1 000t \leq 100, p \leq 1\,000) specifying the number of tree descriptions (described below) and the number of query pairs. Following these are tt lines, each with one tree description. Each tree description is of the form s0s_0 dd s1s_1 s2…sds_2 \ldots s_d, indicating that person s0s_0 has dd children named s1s_1 through sds_d. 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 s0s_0 in the tree descriptions. All the tree descriptions combine to form exactly one tree, and the tree has at least 22 nodes and at most 100100 nodes.

Following this are pp lines of the form sis_i sjs_j where si≠sjs_i \neq s_j and both names are guaranteed to be in the tree.

Output

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

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    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. Example 2

    Input
    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
    
    Expected output
    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