题目

  • 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.
    Family Tree

  • 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;
}

相关文章: