L2-030 冰岛人

题目详情 - L2-030 冰岛人 (pintia.cn)

坑点:如果两个人有公共祖先的话那么这个祖先必须比他们的曾祖父(爷爷的父亲)辈分高。

思路:由于父辈只有一个,所以可以建立一个类似链表的数据结构,如果他们有公共祖先且比他们的曾祖父(爷爷的父亲)辈分高返回true,否则返回false。如果没有公共祖先直接返回true

#include<iostream>
#include<set>
#include<map>
#define sex first
#define ne second
using namespace std;
typedef pair<bool,string> PBS;

map<string,PBS> names;
string fname,sname;

bool check(string a,string b){
	map<string,int> ss;
	int cnt1 = 1;
	while(a.size()){
		ss[a] = cnt1; 
		a = names[a].ne;
		cnt1++;
	}
	int cnt2 = 1;
	while(b.size()){
		if(ss.count(b)) {
			//两人的公共祖先(如果存在的话)必须比任何一方的曾祖父辈分高。
			if(ss[b] < 5 || cnt2 < 5) return false;
			else return true;
		} 
		b = names[b].ne;
		cnt2++;
	}
	return true;
}

int main(){
    int n; cin>>n;
    for(int i = 1; i <= n; i++){
    	cin>>fname>>sname;
    	char c = sname.back();
    	if(c == 'n'){
    		names[fname] = {1,sname.substr(0,sname.size()-4)};
		} else if(c == 'r'){
			names[fname] = {0,sname.substr(0,sname.size()-7)};
		} else if(c == 'm'){
			names[fname].first = 1;
		} else {
			names[fname].first = 0;
		}
	}
    
    int k; cin>>k;
    string n1,n2,tmp;
    while(k--) {
    	cin>>n1>>tmp>>n2>>tmp;
    	if(!names.count(n1) || !names.count(n2)) puts("NA");
    	else if(names[n1].sex == names[n2].sex) puts("Whatever");
    	else if(check(n1,n2)) puts("Yes");
    	else puts("No");
	}
    
    return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值