#P10124. [USACO18OPEN] Family Tree B
[USACO18OPEN] Family Tree B
Problem Description
Farmer John owns a family-run farm that has been passed down for generations, and the origins of the cows on this farm can also be traced back for many generations. By looking through old records, Farmer John is curious about how the cows in the current herd are related to each other. Please help him.
Input Format
The first line of input contains (), followed by the names of two cows. Each cow’s name is a string of at most uppercase letters (A-Z). Farmer John wants to know the relationship between these two cows named on this line.
The next lines each contain two cow names and , indicating that is the mother of .
Output Format
Output one line describing the relationship between the two cows specified in the first line (for simplicity, in the examples below, we call them BESSIE and ELSIE). The possible relationships are:
- If BESSIE and ELSIE have the same mother, output
SIBLINGS. - ELSIE might be a direct ancestor of BESSIE, meaning ELSIE is BESSIE’s mother (
mother), grandmother (grand-mother), great-grandmother (great-grand-mother), great-great-grandmother (great-great-grand-mother), and so on. In this case, outputELSIE is the (relation) of BESSIE, where(relation)is the appropriate relationship, such asgreat-great-grand-mother. - If ELSIE is not an ancestor or sibling of BESSIE, but is the child of one of BESSIE’s ancestors, then ELSIE is BESSIE’s aunt (
aunt). (Note: the original English statement is incorrect here; the problem setter has made a clarification.) If ELSIE is the child of BESSIE’s grandmother, outputELSIE is the aunt of BESSIE; if ELSIE is the child of BESSIE’s great-grandmother, outputELSIE is the great-aunt of BESSIE; if ELSIE is the child of BESSIE’s great-great-grandmother, outputELSIE is the great-great-aunt of BESSIE; and so on. - If BESSIE and ELSIE have any other kind of family relationship (that is, they have a common ancestor), then they are cousins; output
COUSINS. - If BESSIE and ELSIE do not share any common ancestor, and neither is a direct ancestor of the other, output
NOT RELATED.
The diagram below illustrates the relationships above; you only need to consider these relationships. Note that some relationships such as “niece” (niece) (a sister’s daughter) are unnecessary, because if BESSIE is ELSIE’s niece, then ELSIE is BESSIE’s aunt.

Input Format
Output Format
7 AA BB
MOTHER AA
GGMOTHER BB
MOTHER SISTER
GMOTHER MOTHER
GMOTHER AUNT
AUNT COUSIN
GGMOTHER GMOTHER
BB is the great-aunt of AA
Hint
Translated by ChatGPT 5