L2-030 冰岛人

费解啊。
"所谓“五代以内无公共祖先”是指两人的公共祖先(如果存在的话)必须比任何一方的曾祖父辈分高。"
也就是这个祖先出现在任意一方的五代中,都认为是近亲。
只有他是A的五代之外并且是B的五代之外,才认为不是近亲。

#include <bits/stdc++.h>
using namespace std;
map<string, pair<int, string>> mp;
bool check(string a,string b) {
	map<string, int> ss;//名字,第几代
	int count = 1;
	while (a.size()) {
		ss[a] = count;
		a = mp[a].second;
		count++;
	}
	int count2 = 1;
	while (b.size()) {
		if (ss.count(b)) {//如果在a中找到了b
			if (ss[b] < 5 || count2 < 5) return false;//只要在一方当中属于近亲那么就是近亲
			else return true;//不是近亲
		}
		b = mp[b].second;
		count2++;
	}
	return true;//不是近亲
}
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string sname, fname;
		cin >> sname >> fname;
		int len = fname.size();
		if (fname[len - 1] == 'm') {
			mp[sname].first = 1;//男孩
		}
		else if (fname[len - 1] == 'f') {
			mp[sname].first = -1;//女孩
		}
		else {
			int pos = fname.find("sson");
			if (pos != -1) {
				string s = fname.substr(0, pos);
				mp[sname] = { 1,s };//男孩
			}
			pos = fname.find("sdottir");
			if (pos != -1) {
				string s = fname.substr(0, pos);
				mp[sname] = { -1,s };//女孩
			}
		}
	}
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		string a, b, c, d;
		cin >> a >> b >> c >> d;//a和c是有用的
		if (mp.count(a) && mp.count(c)) {
			int sex1 = mp[a].first;
			int sex2 = mp[c].first;
			if (sex1 == sex2) {
				cout << "Whatever" << '\n';
				continue;
			}
		}
		if (!mp.count(a) || !mp.count(c)) {
			cout << "NA" << '\n';
			continue;
		}
		int isok = check(a, c);
		if (isok) cout << "Yes" << '\n';
		else cout << "No" << '\n';
	}
	return 0;
}

参考博客: https://blog.csdn.net/hys__handsome/article/details/124484080

posted @ 2024-03-20 10:37  YuKiCheng  阅读(16)  评论(0编辑  收藏  举报