团体程序设计天梯赛 L2-016 愿天下有情人都是失散多年的兄妹 (25分)

题目链接:

L2-016 愿天下有情人都是失散多年的兄妹 (25分)

思路:

又是一道很想diss的题目:
1.测试数据没有考虑共同祖先是五服以内通婚的情况;
2.题目告诉你同性的话如何输出,异性的话如何输出;测试点4里应该是有没有出现在给定信息中的人的,那么就不知道此人性别,那如何输出?
//over
整体想法就是对于每两个人,先dfs或者bfs第一个人五代以内的人,遍历时做标记,在遍历第二个人时比较是否会遍历到第一个人遍历过的;
注意爹妈的性别也要标记上;

代码:

#include<bits/stdc++.h>

using namespace std;

const int maxn = 123456;
vector<int> G[maxn], vst;
int n, k, sex[maxn], len[maxn];

inline bool bfs(int & u, int flag) {
	queue<int> que;
	que.push(u), vst.push_back(u);
	len[u] = flag;
	while(!que.empty()) {
		int now = que.front();
		if(abs(len[now]) == 5) break;
		que.pop();
		for(int & x : G[now]) {
			if(len[x] && (len[x] ^ flag) < 0) return false;
			len[x] = len[now] + flag;
			que.push(x), vst.push_back(x);
		}
	}
	return true;
}

inline bool ok(int & x, int & y) {
	for(int & x : vst) len[x] = 0;
	vst.clear();
	return (bfs(x, 1) , bfs(y, -1));
}

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif	
	cin >> n;
	for(int i = 0; i < n; i++) {
		int id, f, m;
		char c;
		cin >> id >> c >> f >> m;
		sex[id] = (c == 'M' ? 1 : -1);
		if(~f) G[id].push_back(f), sex[f] = 1;
		if(~m) G[id].push_back(m), sex[m] = -1;
	}
	cin >> k;
	while(k--) {
		int x, y;
		cin >> x >> y;
		if(sex[x] == sex[y] && sex[x]) puts("Never Mind");
		else puts(ok(x,y) ? "Yes" : "No");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/yuhan-blog/p/12308660.html