L2-030 冰岛人 (25 分)

L2-030 冰岛人 (25 分)
坑点:
两个人维京人可能不是同辈的
需要一方搜到顶部,另一方搜 4 4 4 代以内的
因为可能搜到顶部的一方辈分更大,所以搜交换搜索顺序再搜一次就可以了
还有一个细节就是建边的时候只需要建维京人内部的就好了,不能涉及普通人
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
map <pair<string, string>, int> ma;
/*
男
	维京 n
	普通 m
女  维京 r
	普通 f 
*/
map <string, int> id;
vector <int> v[maxn];
int cnt;
bitset <maxn> vis;
bool flag = 0;

void dfs1(int x, int dep){
	vis[x] = 1;
	for(auto to : v[x]){
		dfs1(to, dep + 1);
	}
}
void dfs2(int x, int dep){
	if(dep > 4) return;
	if(vis[x]) flag = 1;
	for(auto to : v[x]){
		dfs2(to, dep + 1);
	}
}

void work()
{	
	cin >> n;
	for(int i = 1; i <= n; ++i){
		string a, b;cin >> a >> b;
		string t = "";
		if(b.back() == 'n'){
			for(int i = 0; i < b.size() - 4; ++i) t += b[i];
			ma[{a, t}] = 1;
		}
		else if(b.back() == 'm'){
			for(int i = 0; i < b.size() - 1; ++i) t += b[i];
			ma[{a, t}] = 1;
		}
		else if(b.back() == 'r'){
			for(int i = 0; i < b.size() - 7; ++i) t += b[i];
			ma[{a, t}] = 2;
		}
		else {
			for(int i = 0; i < b.size() - 1; ++i) t += b[i];
			ma[{a, t}] = 2;
		}
		if(!id.count(a)) id[a] = ++cnt;
		if(!id.count(t)) id[t] = ++cnt;
		if(b.back() == 'n' || b.back() == 'r')
			v[id[a]].push_back(id[t]);
	}	
	cin >> m;
	while(m--){
		string a, b, c, d;cin >> a >> b >> c >> d;
		if(!ma.count({a, b}) || !ma.count({c, d})) cout << "NA\n";
		else if(ma[{a, b}] == ma[{c, d}]) cout << "Whatever\n";
		else {
			flag = 0;
			vis.reset();
			dfs1(id[a], 1);
			dfs2(id[c], 1);
			if(!flag){
				vis.reset();
				dfs1(id[c], 1);
				dfs2(id[a], 1);
			}
			cout << (flag ? "No" : "Yes") << endl;
		}
	}
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

### L2-030 冰岛人 Java 编程题解 针对冰岛人的姓氏特点以及避免近亲繁衍的需求,可以通过构建家族树来解决此问题。具体来说,在给定两个人的情况下,程序需要追溯两者的祖先关系直到找到共同祖先为止,并计算两者之间的最近公共祖先距离。 #### 解决方案概述 为了处理这个问题,可以采用广度优先搜索(BFS)或深度优先搜索(DFS),从每个人开始向上遍历其父母节点直至遇到相同的祖先节点。在此过程中记录路径长度以便最终判断是否存在血缘关系及其紧密程度。 #### 数据结构的选择 考虑到查询效率的要求,适合选用哈希表存储每个人的直系亲属信息,即键为人名而值为其父亲的名字列表形式表示多代祖先链路。这样可以在O(1)时间内访问任意一个人的所有已知长辈信息[^4]。 #### 关键逻辑描述 对于每一对待比较的人X和Y: - 初始化两个队列别用于保存当前正在探索的层次级别人物; - 同时初始化两个集合用来标记已经访问过的节点以防循环依赖; - 当任一队列为空之前持续迭代: - 取出队首元素尝试匹配另一侧队伍中的成员; - 如果发现相同则返回真并结束搜索过程; - 若未命中继续将其尚未被考察的父亲加入相应队尾等待下一轮检查。 一旦完成上述流程即可得知二者间是否有交集以及具体的关联级别。 ```java import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); Map<String, String> parentMap = new HashMap<>(); Set<String> visitedA = new HashSet<>(); Set<String> visitedB = new HashSet<>(); Queue<String> queueA = new LinkedList<>(); Queue<String> queueB = new LinkedList<>(); int n = Integer.parseInt(sc.nextLine().split(" ")[1]); while (n-- > 0){ String line = sc.nextLine(); String child = line.split(",")[0]; String father = line.substring(line.indexOf(',') + 1).trim(); parentMap.put(child,father); } String personAX = sc.next(),personAY=sc.next(); String personBX = sc.next(),personBY= sc.next(); boolean flag=false; queueA.offer(personAY);visitedA.add(personAY); queueB.offer(personBY);visitedB.add(personBY); while (!queueA.isEmpty()&&!queueB.isEmpty()){ if(!flag&&search(queueA,visitedA,parentMap,visitedB)){ System.out.println("Related"); break; } if(search(queueB,visitedB,parentMap,visitedA)){ System.out.println("Related"); flag=true;break; }else{ flag=false; } } if(!flag && !queueA.isEmpty() || !queueB.isEmpty())System.out.println("Not related"); } private static boolean search(Queue<String> queue,Set<String> visited, Map<String,String> map ,Set<String> otherVisited){ int size = queue.size(); for(int i=0;i<size;++i){ String cur = queue.poll(); if(map.containsKey(cur)){ String next = map.get(cur); if(otherVisited.contains(next))return true; if(!visited.contains(next)){ queue.offer(next); visited.add(next); } } } return false; } } ``` 该段代码实现了基于输入数据建立父子映射关系的功能,并利用双向广搜策略高效地解决了寻找最近公共祖先的问题,进而判定两名个体之间是否具有亲戚关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值