Family Tree

题目

  • Problem Description
    Farmer John owns a family-run farm that has been passed down over several generations, with a herd of cows whose familial roots can similarly be traced back several generations on the same farm. By examining old records, Farmer John is curious how the cows in his current herd are related to each-other. Please help him in this endeavor!

  • Input
    The first line of input contains N (1≤N≤100) followed by the names of two cows. Cow names are each strings of at most 10 uppercase letters (A…Z). Farmer John is curious about the relationship between the two cows on this line of input.

    The next N lines each contain two cow names X and Y, indicating that X is the mother of Y.

  • Output
    You should print one line of output indicating the relationship between the two cows specified on the first line of input (for simplicity, let’s call these two cows BESSIE and ELSIE for the examples below). Here are the different types of relationships that are possible:

    You should output “SIBLINGS” if BESSIE and ELSIE have the same mother.
    BESSIE might be a direct descendant of ELSIE, meaning that ELSIE is either the mother, grand-mother, great-grand-mother, great-great-grand-mother, etc., of BESSIE. If this is the case, you should print “ELSIE is the (relation) of BESSIE”, where (relation) is the appropriate relationship, for example “great-great-grand-mother”.
    If ELSIE is a child of an ancestor of BESSIE (and ELSIE is not herself an ancestor or sister of BESSIE), then ELSIE is BESSIE’s aunt. You should output “ELSIE is the aunt of BESSIE” if ELSIE is a child of BESSIE’s grand-mother, “ELSIE is the great-aunt of BESSIE” if ELSIE is a child of BESSIE’s great-grand-mother, “ELSIE is the great-great-aunt of BESSIE” if ELSIE is a child of BESSIE’s great-great-grand-mother, and so on.
    If BESSIE and ELSIE are related by any other means (i.e., if they share a common ancestor), they are cousins, and you should simply output “COUSINS”.
    You should output “NOT RELATED” if BESSIE and ELSIE have no common ancestor, or neither is directly descended from the other.
    The following diagram helps illustrate the relationships above, which are the only relationship types you need to consider.

    Observe that some relationships like “niece” (daughter of sister) are not necessary since if BESSIE is the niece of ELSIE, then ELSIE is BESSIE’s aunt.
    在这里插入图片描述

  • input
    7 AA BB
    MOTHER AA
    GGMOTHER BB
    MOTHER SISTER
    GMOTHER MOTHER
    GMOTHER AUNT
    AUNT COUSIN
    GGMOTHER GMOTHER

  • output
    BB is the great-aunt of AA

题意

从输入的家谱中找到其他两个人的关系并输出
关系如图

思路

我是先寻找两人的所有祖先,再遍历双方所有祖先确定有无共同祖先。
如果有共同祖先,就知道两人距离共同祖先的距离,通过距离确认关系。
A距离公共祖先距离为0,则A就是B的祖先
A距离公共祖先距离为1,则A是B的sibings或者aunt
……

代码

#include <bits/stdc++.h>
using namespace std;

map<string,string>mother;
char A[12],B[12];//B是A的什么

int main() {
    int N;

    scanf("%d %s %s ",&N,A,B);
    char m_name[12];
    char d_name[12];
    int k=1;
    for(int i=1; i<=N; ++i) {
        scanf("%s %s",m_name,d_name);
        mother[d_name]=m_name;
    }

    string A_an[205];
    string B_an[205];
    string t=A;
    A_an[0]=A;
    B_an[0]=B;
    int a,b;
    for(a=1 ; mother[t][0]!='\0' ; ++a) {
        A_an[a]=mother[t];
        t=A_an[a];
    }
    t=B;
    for(b=1 ; mother[t][0]!='\0'; ++b) {
        B_an[b]=mother[t];
        t=B_an[b];
    }

    for(int i=0; i<a; ++i)
        for(int j=0; j<b; ++j)
            if(A_an[i] == B_an[j]) {
                //cout << i <<" "<<j<<endl;
                //j==0 -> 直系
                if( j==0 ) {
                    cout <<B << " is the ";
                    for(int k=1; k<=i-1; ++k) {
                        if(k==1) cout << "grand-";
                        else cout << "great-";
                    }
                    cout << "mother of "<<A<<endl;
                }
                else if( i==0 ) {
                    cout <<A << " is the ";
                    for(int k=1; k<=j-1; ++k) {
                        if(k==j-1) cout << "grand-";
                        else cout << "great-";
                    }
                    cout << "mother of "<<B<<endl;
                }
                //j==1 -> sibings or aunt
                else if( i==1 and j==1 ) {
                    cout << "SIBLINGS" <<endl;
                }

                else if( i>1 and j==1 ) {
                    cout << B << " is the ";
                    for(int k=1; k<=i-2; ++k)
                        cout << "great-";
                    cout <<"aunt of "<<A<<endl;
                }
                else if( i==1 and j>1 ) {
                    cout << A << " is the ";
                    for(int k=1; k<=j-2; ++k)
                        cout << "great-";
                    cout <<"aunt of "<<B<<endl;
                }
                //j>1 -> cousin
                else if( i>1 and j>1 ) {
                    cout << "COUSINS"<<endl;
                }
                return 0;
            }
    cout <<"NOT RELATED"<<endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值