1986 ACM East Central Regional Programming Contest

Problem #4: Family Trees

This problem deals with family trees. A family tree (or, more accurately, a family graph) is a set of nodes, representing individuals, connected by directed edges, representing the "offspring" relation. An edge from one node to another means that the latter node is an offspring of the former. In general, one node is a descendant of another (its ancestor) if there is a directed sequence of edges from the ancestor node to the descendant node. Nodes can have at most two edges in (one for each parent), and an arbitrary number of edges out (one for each offspring).

The distance between an ancestor and a descendant is the number of edges in the graph between their nodes. Thus, the distance between a parent and child is one; between a grandparent and child is two.

Two people are related if they share a common ancestor; that is, if there exists a person from whom they are both desdended. The distance between two related people via a common ancestor is the sum of the distances between each person and the common ancestor. A closest common ancestor is one who minimizes the distance between the two related people. Note that, in general, two related people may have several closest common ancestors (for example, if twins marry twins, their children will have the four grandparents as closest common ancestors).

For this problem you are to read a set of family history records - each record containing a father, mother and list of offspring - and then accept queries concerning pairs of people in the database. The input is divided into two sections: a data section and a query section. The data section is a sequence of family records, each record containing the names of a single family, in the order father, mother, children. A name is a sequence of alphabetic characters followed by one or more spaces. The data section is terminated by a line containing a peroid (".") in column one.

The second section of the input consists of queries, one per line, each line containing two names. The query section is terminated by end of file. For each query your program is to print a line containing the query (i.e., echo the input line) and one of the following:

  1. If the two people are unrelated, print "unrelated", starting in column five.
  2. If one is a descendant of the other, print "descendant", starting in column five.
  3. If the two people are related, but one is not a descendant of the other, print the names of all of the closest common ancestors, each on a separate line and starting in column five.
You may make the following assumptions:
  1. Input is free format: there are one or more blanks between names and 0 or more blanks at the beginning and end of each line.
  2. Input lines contain not more than 74 characters.
  3. Names are no longer than 20 characters.
  4. There are not more than 100 distinct names in the input, nor more than 100 generations.
  5. Records appearing in the family section have at least three names (i.e., each family has at least one child).
  6. Names appearing in the query section will have appeared in the data section.

Sample Input

robert   loretta bob jerry   lauri 
bob linda dave dan   dede  donna
todd dona   robby
dan heather billy   
dave   mary george
ernie dorothy   linda todd terry tim
tom marianne bill jim
tom cathy betty
.
robby  donna
dorothy loretta
bob   dede
billy george
betty jim

Sample Output

robby  donna
    ernie
    dorothy
dorothy loretta
    unrelated
bob   dede
    descendant
billy george
    bob
    linda
betty jim
    tom