天梯赛L2-016愿天下有情人都是失散多年的兄妹

pat并查集题目太多了。这道就用了dfs写。其实用bfs+set去重写更简单,在下比较辣鸡吧

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int a, b, N, d;
char c;
struct person {
	char sex;
	int papa, mama;
}Lu[100100] = {0};
map Hui;
bool dfs(int a, int b) {
	if (a == -1)
		return true;
	if (b == 4)  // 5
		return true;
	if (Lu[a].papa != -1)
	{
		if (Hui[Lu[a].papa] == NULL)
			Hui[Lu[a].papa] = 1;
		else//  if (Hui[Lu[a].papa] == 1)
			return false;
	}
	if (Lu[a].mama != -1)
	{
		if (Hui[Lu[a].mama] == NULL)
			Hui[Lu[a].mama] = 1;
		else //if (Hui[Lu[a].mama] == 1)
			return false;
	}
	if (!dfs(Lu[a].papa, b + 1) || !dfs(Lu[a].mama, b + 1))
		return false;
	return true;
}
int main() {
	freopen("in.txt", "r", stdin);
	while (cin >> N) {
		memset(Lu, -1, sizeof(Lu));
		Hui.clear();
		for (int i = 0; i < N; i++) {
			cin >> d >> c >> a >> b;
			Lu[d].sex = c;
			Lu[d].papa = a;
			Lu[d].mama = b;
			Lu[a].sex = 'M';
			Lu[b].sex = 'F';
		}
		int k;
		cin >> k;
		while (k--) {
			cin >> a >> b;
			if (Lu[a].sex == Lu[b].sex) {
				cout << "Never Mind\n";
				continue;
			}
			dfs(a, 0);
			if (dfs(b, 0))
				cout << "Yes\n";
			else
				cout << "No\n";
			Hui.clear();
		}
	}
	return 0;
}


你可能感兴趣的:(天梯赛L2-016愿天下有情人都是失散多年的兄妹)