http://www.usaco.org/index.php?page=viewproblem2&cpid=833&lang=zh
模拟就完事了
#include <bits/stdc++.h>
using namespace std;
const int maxn=105;
const int INF=1<<30;
int n;
string da[maxn];
string mo[maxn];
string find_mother(string cow){
for (int i=0;i<n;i++){
if (cow==da[i]){
return mo[i];
}
}
return "";
}
//如果cow1是cow2的直系祖先,返回cow1与cow2的代数
//否则返回-1
int is_ancestor(string cow1,string cow2){
int cnt=0;
while (cow2!=""){
if (cow1==cow2){
return cnt;
}
cow2=find_mother(cow2);
cnt++;
}
return -1;
}
int main(){
string x,y;
cin >> n;
cin >> x >> y;
for (int i=0;i<n;i++){
cin >> mo[i] >> da[i];
}
string cow=x;
int b=0;
while (cow!=""){
//如果cow是y的直系祖先
if (is_ancestor(cow,y)!=-1){
break;
}
cow=find_mother(cow);
b++;
}
if (cow==""){
cout << "NOT RELATED" << endl;
return 0;
}
int a=is_ancestor(cow,y);
if (a==1 && b==1) cout << "SIBLINGS" << endl;
else
if (a>1 && b>1) cout << "COUSINS" << endl;
else{
if (a>b) swap(x,y),swap(a,b);
cout << y << " is the ";
for (int i=0;i<b-2;i++) cout << "great-";
if (b>1 && a==0) cout << "grand-";
if (a==0) cout << "mother";
else cout << "aunt";
cout << " of " << x << endl;
}
}