E. Family Tree(最近公共祖先)

http://codeforces.com/group/NVaJtLaLjS/contest/238202/problem/E

E. Family Tree

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 NN (1≤N≤1001≤N≤100) followed by the names of two cows. Cow names are each strings of at most 10 uppercase letters (A…ZA…Z). Farmer John is curious about the relationship between the two cows on this line of input.

The next NN lines each contain two cow names XX and YY, indicating that XX is the mother of YY.

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.

Example

input

Copy

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

output

Copy

BB is the great-aunt of AA

题意:

找A B之间的关系

题目分析:

找最近公共祖先。分别记录层数。在根据题目判断。

代码:

#include<bits/stdc++.h>
using namespace std;
int n;
string mother[110],cow[110];
string find(string a){
	for(int i=0;i<n;i++){
		if(a==cow[i])return mother[i];
	}
	return "";
}
int issame(string a,string b){
	int ct=0;
	while(b!=""){
		if(a==b)return ct;
		b=find(b);
		ct++;
	}
	return -1;
}
int main()
{
	string a,b;
	cin>>n>>a>>b;for(int i=0;i<n;i++)cin>>mother[i]>>cow[i];
	string now=a;int cnta=0;
	while(now!=""){//找祖先 
		if(issame(now,b)!=-1)break;
		now=find(now);
		cnta++;//a层 
	}
	if(now==""){
        cout<<"NOT RELATED"<<endl;
        return 0;
    }
    int cntb=issame(now,b);//b层 
    if(cnta==cntb&&cnta==1){
        cout<<"SIBLINGS"<<endl;
        return 0;
    }
    else if(cnta>1&&cntb>1){
        cout<<"COUSINS"<<endl;
        return 0;
    }
    else {
        if(cnta<cntb) {
            swap(a,b);
            swap(cnta,cntb);
        }
        cout<<b<<" is the ";
        for(int i=0;i<cnta-2;i++)
            cout<<"great-";
        if(cnta>1&&cntb==0)
            cout<<"grand-";
        if(cntb==0)
            cout<<"mother";
        else
            cout<<"aunt";
         cout<<" of "<<a<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43490894/article/details/87871675
今日推荐