知识点:模拟
思路:
首先,设两只奶牛为 。
因为族谱是一棵树,所以除非 和 之间没有关系,否则可以确定, 的 辈祖先是 的 辈祖先。如果 是 的祖先,那么 。
我们求的是辈分最小的祖先,所以这不就是最小公共祖先?
由于数据范围小,时间复杂度低,可以直接暴力求。
求出 之后,就好办了,直接判断:
如果 , 是 的祖先。
如果 , 是 的祖先。
如果 且 , 和 是姐妹。
如果 且 , 是 的姨母。
如果 且 , 是 的姨母。
如果 且 , 和 是表姐妹。
没有查找到关系,那么这两头奶牛就是没有关系。 最后,纯模拟就可以了。
code
#include<bits/stdc++.h>//万能头
using namespace std;
int n,k1=114514,k2=1919810,q=0,w=0;
string a,b,s1[101],s2[101],s3[101],s4[101];
void f(string s){
s3[q]=s;
q++;
for(int i=1;i<=n;i++){
if(s2[i]==s)f(s1[i]);
}
}
void sc2(string s){
s4[w]=s;
w++;
for(int i=1;i<=n;i++){
if(s2[i]==s)sc2(s1[i]);
}
}
void init(){//输入
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
cin>>s1[i]>>s2[i];
}
f(a);
sc2(b);
}
void now(){
for(int i=0;i<q;i++){
for(int j=0;j<w;j++){
if(s3[i]==s4[j]){
k1=i;
k2=j;
return;
}
}
}
}
int main(){
init();//输入
now();
if((k1==1)&&(k2==1))cout<<"SIBLINGS";
else if(k1==0){
cout<<a<<" is the ";
while(k2>=3){
cout<<"great-";
k2--;
}
if(k2==2)cout<<"grand-";
cout<<"mother of "<<b;
}
else if(k2==0){
cout<<b<<" is the ";
while(k1>=3){
cout<<"great-";
k1--;
}
if(k1==2)cout<<"grand-";
cout<<"mother of "<<a;
}
else if(k1==1){
cout<<a<<" is the ";
while(k2>=3){
cout<<"great-";
k2--;
}
cout<<"aunt of "<<b;
}
else if(k2==1){
cout<<b<<" is the ";
while(k1>=3){
cout<<"great-";
k1--;
}
cout<<"aunt of "<<a;
}
else if(k1==114514)cout<<"NOT RELATED";
else cout<<"COUSINS";
return 0;//完美结尾
}