Description:
呵呵。大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人、父母、祖父母、曾祖父母、高祖父母)则不可通婚。本题就请你帮助一对有情人判断一下,他们究竟是否可以成婚?
Input:
输入第一行给出一个正整数N(2 <= N <= 104),随后N行,每行按以下格式给出一个人的信息:
本人ID 性别 父亲ID 母亲ID
其中ID是5位数字,每人不同;性别M代表男性、F代表女性。如果某人的父亲或母亲已经不可考,则相应的ID位置上标记为-1。
接下来给出一个正整数K,随后K行,每行给出一对有情人的ID,其间以空格分隔。
注意:题目保证两个人是同辈,每人只有一个性别,并且血缘关系网中没有***或隔辈成婚的情况。
Output:
对每一对有情人,判断他们的关系是否可以通婚:如果两人是同性,输出“Never Mind”;如果是异性并且关系出了五服,输出“Yes”;如果异性关系未出五服,输出“No”。
Sample Input:
24
00001 M 01111 -1
00002 F 02222 03333
00003 M 02222 03333
00004 F 04444 03333
00005 M 04444 05555
00006 F 04444 05555
00007 F 06666 07777
00008 M 06666 07777
00009 M 00001 00002
00010 M 00003 00006
00011 F 00005 00007
00012 F 00008 08888
00013 F 00009 00011
00014 M 00010 09999
00015 M 00010 09999
00016 M 10000 00012
00017 F -1 00012
00018 F 11000 00013
00019 F 11100 00018
00020 F 00015 11110
00021 M 11100 00020
00022 M 00016 -1
00023 M 10012 00017
00024 M 00022 10013
9
00021 00024
00019 00024
00011 00012
00022 00018
00001 00004
00013 00016
00017 00015
00019 00021
00010 00011
Sample Output:
Never Mind
Yes
Never Mind
No
Yes
No
Yes
No
No
题目链接
这道题目用结构体里的两个变量存储父和母,在判断使用bfs搜索两人五服之内是否有相同的id,这里在搜索第一个人五服之内id时可以用set存储id,然后在搜索第二个人五服之内id时直接判断,也可以在搜索第一个人五服之内id时用一个vis数组保存id访问情况,然后在搜索第二个人五服之内id时判断此id是否有被访问过。
坑:输入父母信息时一定要存储父母性别信息;样例按照id递增顺序输入,但是题目没有说一定会这么输入,可能输入id不递增甚至不连续。
AC代码(set):
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <set>
#include <map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 100010;
const double eps = 1e-5;
const double e = 2.718281828459;
struct person {
char sex;
int mother_path;
int father_path;
person() {
sex = 'M';
mother_path = -1;
father_path = -1;
}
}people[maxn];
int n, m;
void Get_information() {
cin >> n;
for (int i = 1; i <= n; ++i) {
int id;
cin >> id;
cin >> people[id].sex >> people[id].father_path >> people[id].mother_path;
if (people[id].father_path != -1) {
people[people[id].father_path].sex = 'M';
}
if (people[id].mother_path != -1) {
people[people[id].mother_path].sex = 'F';
}
}
}
bool Judge(int a, int b) {
set<int> a_ancestor;
queue<int> a_each_ancestor[5];
a_each_ancestor[0].push(a);
a_ancestor.insert(a);
for (int i = 0; i < 5; ++i) {
while (!a_each_ancestor[i].empty()) {
int find_ancestor = a_each_ancestor[i].front();
a_each_ancestor[i].pop();
if (people[find_ancestor].mother_path != -1) {
if (i < 4) {
if (people[find_ancestor].mother_path <= n) {
a_each_ancestor[i + 1].push(people[find_ancestor].mother_path);
}
a_ancestor.insert(people[find_ancestor].mother_path);
}
}
if (people[find_ancestor].father_path != -1) {
if (i < 4) {
if (people[find_ancestor].father_path <= n) {
a_each_ancestor[i + 1].push(people[find_ancestor].father_path);
}
a_ancestor.insert(people[find_ancestor].father_path);
}
}
}
}
if (a_ancestor.count(b) != 0) {
return false;
}
queue<int> b_each_ancestor[5];
b_each_ancestor[0].push(b);
for (int i = 0; i < 5; ++i) {
while (!b_each_ancestor[i].empty()) {
int find_ancestor = b_each_ancestor[i].front();
b_each_ancestor[i].pop();
if (people[find_ancestor].mother_path != -1 && i < 4 && people[find_ancestor].mother_path <= n) {
b_each_ancestor[i + 1].push(people[find_ancestor].mother_path);
}
if (a_ancestor.count(people[find_ancestor].mother_path)) {
return false;
}
if (people[find_ancestor].father_path != -1 && i < 4 && people[find_ancestor].father_path <= n) {
b_each_ancestor[i + 1].push(people[find_ancestor].father_path);
}
if (a_ancestor.count(people[find_ancestor].father_path)) {
return false;
}
}
}
return true;
}
void Ask_output() {
cin >> m;
for (int i = 1; i <= m; ++i) {
int ask1, ask2;
cin >> ask1 >> ask2;
if (people[ask1].sex == people[ask2].sex) {
cout << "Never Mind" << endl;
}
else {
if (Judge(ask1, ask2)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
Get_information();
Ask_output();
return 0;
}
AC代码(vis array):
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <set>
#include <map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 100010;
const double eps = 1e-5;
const double e = 2.718281828459;
struct person {
char sex;
int mother_path;
int father_path;
person() {
sex = 'M';
mother_path = -1;
father_path = -1;
}
}people[maxn];
int n, m;
bool vis[maxn];
void Get_information() {
cin >> n;
for (int i = 1; i <= n; ++i) {
int id;
cin >> id;
cin >> people[id].sex >> people[id].father_path >> people[id].mother_path;
if (people[id].father_path != -1) {
people[people[id].father_path].sex = 'M';
}
if (people[id].mother_path != -1) {
people[people[id].mother_path].sex = 'F';
}
}
}
bool Judge(int a, int b) {
mem(vis, 0);
queue<int> a_ancestor[3];
vis[a] = 1;
if (people[a].father_path != -1) {
a_ancestor[0].push(people[a].father_path);
vis[people[a].father_path] = 1;
}
if (people[a].mother_path != -1) {
a_ancestor[0].push(people[a].mother_path);
vis[people[a].mother_path] = 1;
}
for (int i = 0; i <= 2; ++i) {
while (!a_ancestor[i].empty()) {
int temp_ancestor = a_ancestor[i].front();
a_ancestor[i].pop();
if (temp_ancestor == -1) {
continue;
}
if (i < 2) {
if (people[temp_ancestor].father_path != -1) {
a_ancestor[i + 1].push(people[temp_ancestor].father_path);
vis[people[temp_ancestor].father_path] = 1;
}
if (people[temp_ancestor].mother_path != -1) {
a_ancestor[i + 1].push(people[temp_ancestor].mother_path);
vis[people[temp_ancestor].mother_path] = 1;
}
}
else {
if (people[temp_ancestor].father_path != -1) {
vis[people[temp_ancestor].father_path] = 1;
}
if (people[temp_ancestor].mother_path != -1) {
vis[people[temp_ancestor].mother_path] = 1;
}
}
}
}
queue<int> b_ancestor[4];
if (vis[b]) {
return 0;
}
if (vis[people[b].father_path]) {
return 0;
}
else {
b_ancestor[0].push(people[b].father_path);
}
if (vis[people[b].mother_path]) {
return 0;
}
else {
b_ancestor[0].push(people[b].mother_path);
}
for (int i = 0; i <= 2; ++i) {
while (!b_ancestor[i].empty()) {
int temp_ancestor = b_ancestor[i].front();
b_ancestor[i].pop();
if (temp_ancestor == -1) {
continue;
}
if (people[temp_ancestor].father_path != -1) {
if (vis[people[temp_ancestor].father_path]) {
return 0;
}
b_ancestor[i + 1].push(people[temp_ancestor].father_path);
}
if (people[temp_ancestor].mother_path != -1) {
if (vis[people[temp_ancestor].mother_path]) {
return 0;
}
b_ancestor[i + 1].push(people[temp_ancestor].mother_path);
}
}
}
return 1;
}
void Ask_output() {
cin >> m;
for (int i = 1; i <= m; ++i) {
int ask1, ask2;
cin >> ask1 >> ask2;
if (people[ask1].sex == people[ask2].sex) {
cout << "Never Mind" << endl;
}
else {
if (Judge(ask1, ask2)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
Get_information();
Ask_output();
return 0;
}