编程介的小学生 2017-02-12 14:14 采纳率: 20.5%
浏览 807
已采纳

Papa

问题描述 :

While cleaning your parents’ attic, you discovered a box containing many documents describing the relationships among your ancestors. Given these documents, you are interested in answering a number of questions about the relationships implied by the document. Fortunately, all your ancestors have unique names so it is possible to make many inferences without any confusion.

It is assumed that all relationships (implied or given) satisfy the following:

*A person can be either male, female, or have an unknown (undetermined by the data set) sex;
*a person can have at most one spouse (of the opposite sex), and X is Y’s wife if and only if Y is X’s husband;
*a person can have at most one mother and at most one father that can be inferred from the given data;
*if a person has a mother and father, then the mother and father are married;
*the children of a person’s spouse are that person’s children as well;
*the spouse of a son (or daughter) is not considered to be a daughter (or son). In other words, "sons" and "daughters" refer to biological sons and daughters.

The information provided will be consistent, and you may assume there are no hidden relationships that are not explicitly stated or implied by the above rules of consistency.

输入:

The first part of the input consists of a number of lines describing the known relationships. Each relationship is listed on one line in the form:

NAME is NAME’s RELATIONSHIP.

where NAME is a lowercase alphabetic word (never ‘is’), and RELATIONSHIP is one of

*wife
*husband
*daughter
*son
*mother
*father

This first part is terminated by a blank line. You may assume that there is at least one known relationship given, and there are at most 100 distinct names mentioned in the known relationships.

This is followed by a list of questions (one per line) of the form:
is NAME NAME’s RELATIONSHIP?
where NAME is as before, but RELATIONSHIP is one of

*wife
*husband
*daughter
*son
*mother
*father
*niece: X is a niece of Y if there are W and Z such that X is a daughter of  W, and W and Y are distinct children of Z.
*nephew: X is a nephew of Y if there are W and Z such that X is a son of W, and W and Y are distinct children of Z.
*grandfather: X is a grandfather of Y if there is Z such that X is a father of Z, and Z is the father or mother of Y.
*grandmother: X is a grandmother of Y if there is Z such that X is a mother of Z, and Z is the father or mother of Y.
*grandson: X is a grandson of Y if there is Z such that X is a son of Z, and Z is a child of Y.
*granddaughter: X is a granddaughter of Y if there is Z such that X is a daughter of Z, and Z is a child of Y.

All names appearing in the questions will be mentioned in the list of known relationships. The list of questions is terminated by the end of file.

输出:

The first part of the input consists of a number of lines describing the known relationships. Each relationship is listed on one line in the form:

NAME is NAME’s RELATIONSHIP.

where NAME is a lowercase alphabetic word (never ‘is’), and RELATIONSHIP is one of

*wife
*husband
*daughter
*son
*mother
*father

This first part is terminated by a blank line. You may assume that there is at least one known relationship given, and there are at most 100 distinct names mentioned in the known relationships.

This is followed by a list of questions (one per line) of the form:
is NAME NAME’s RELATIONSHIP?
where NAME is as before, but RELATIONSHIP is one of

*wife
*husband
*daughter
*son
*mother
*father
*niece: X is a niece of Y if there are W and Z such that X is a daughter of  W, and W and Y are distinct children of Z.
*nephew: X is a nephew of Y if there are W and Z such that X is a son of W, and W and Y are distinct children of Z.
*grandfather: X is a grandfather of Y if there is Z such that X is a father of Z, and Z is the father or mother of Y.
*grandmother: X is a grandmother of Y if there is Z such that X is a mother of Z, and Z is the father or mother of Y.
*grandson: X is a grandson of Y if there is Z such that X is a son of Z, and Z is a child of Y.
*granddaughter: X is a granddaughter of Y if there is Z such that X is a daughter of Z, and Z is a child of Y.

All names appearing in the questions will be mentioned in the list of known relationships. The list of questions is terminated by the end of file.

样例输入:

john is mary's husband.
john is tom's father.
mary is jane's mother.
jane is anna's mother.

is mary john's wife?
is jane mary's daughter?
is tom mary's husband?
is anna jane's daughter?
样例输出:

yes
yes
no
unknown

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-02-17 20:48
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!