参考博客:
冰岛人稍微有改动,可以看看题目是啥样的
#include<iostream>
#include<map>
#include<string>
using namespace std;
struct Peoson {
char sex;
string father;
};
map<string, Peoson> people;
int judge(string a, string b)
{
int i = 1, j;
for (string A = a; !A.empty(); A = people[A].father, i++) {
j = 1;
for (string B = b; !B.empty(); B = people[B].father, j++) {
if (i >= 5 && j >= 5) break; //双方都超出5代之后,不需要继续寻找(测试点6 运行超时)
if (A == B && (i < 5 || j < 5)) //五代内出现共同祖先,返回false(测试点3、6答案错误)
return 0;
}
}
return 1;
}
int main()
{
int n, m;
string str, a, b;
cin.sync_with_stdio(false);//优化
cin >> n;
while(n--)
{
cin >> a >> b;
// 每一个是儿子为基础,根据后缀确定性别和父亲的名,
if (b.back() == 'n') //儿子
people[a] = { 'm',b.substr(0,b.size() - 4) };//用map存的是,一个是性别,一个父亲 减四是sson
else if (b.back() == 'r') //女儿
people[a] = { 'f',b.substr(0, b.size() - 7) };
else people[a].sex = b.back(); //其他人
}
cin >> m;
while(m--)
{
cin >> a >> str >> b >> str; //我们是以名字为基础存的,所以姓氏没有用
if (people.find(a) == people.end() || people.find(b) == people.end()) printf("NA\n");//有一人不在名单中
else if (people[a].sex == people[b].sex) printf("Whatever\n");
else printf("%s\n", judge(a, b) ? "Yes" : "No");
}
return 0;
}
知识普及:
本题样例:
查询结果: