呵呵。大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人、父母、祖父母、曾祖父母、高祖父母)则不可通婚。本题就请你帮助一对有情人判断一下,他们究竟是否可以成婚?
输入格式:
输入第一行给出一个正整数N(2 ≤ N ≤104),随后N行,每行按以下格式给出一个人的信息:
本人ID 性别 父亲ID 母亲ID
其中ID是5位数字,每人不同;性别M代表男性、F代表女性。如果某人的父亲或母亲已经不可考,则相应的ID位置上标记为-1。
接下来给出一个正整数K,随后K行,每行给出一对有情人的ID,其间以空格分隔。
注意:题目保证两个人是同辈,每人只有一个性别,并且血缘关系网中没有乱伦或隔辈成婚的情况。
输出格式:
对每一对有情人,判断他们的关系是否可以通婚:如果两人是同性,输出Never Mind;如果是异性并且关系出了五服,输出Yes;如果异性关系未出五服,输出No。
输入样例:
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
输出样例:
Never Mind
Yes
Never Mind
No
Yes
No
Yes
No
No
#include
using namespace std;
typedef struct Per{
int fuid = -1;//1父亲ID
int muid = -1;//母亲ID
string sex;//性别,防止字符型读取缓冲区问题,直接采用字符串
}Per;
Per per[100000];
bool check(int a,int b,int n){
//a,b分别为待判断的两个人,n代表代数,用于判断是否五服之外
if(a==-1||b==-1||n>4)//有不可考,或五服之外
return true;
if((per[a].fuid!=-1&&per[a].fuid==per[b].fuid)||(per[a].muid!=-1&&per[a].muid==per[b].muid))
return false;//同父或同母
return check(per[a].fuid,per[b].fuid,n+1)&&check(per[a].muid,per[b].muid,n+1)&&check(per[a].muid,per[b].fuid,n+1)&&check(per[a].fuid,per[b].muid,n+1);
}
int main(){
int n;
cin >> n;
for(int i = 0;i<n;i++){
int id;
string sex;
cin >> id;
cin >> per[id].sex >> per[id].fuid >> per[id].muid;
if(per[id].fuid!=-1)
per[per[id].fuid].sex = "M";
if(per[id].muid!=-1)
per[per[id].muid].sex = "F";
}
int k;
cin >> k;
for(int i = 0; i<k;i++){
int a,b;
cin >> a >> b;
if(per[a].sex==per[b].sex)
cout << "Never Mind" << endl;
else{
bool flag = check(a,b,1);
if(flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}
#include
#include
#include
using namespace std;
const int maxn = 100010;
int father[maxn], mother[maxn];
char sex[maxn];
//递归
int judge(int a, int b, int num) //判断a,b是否为5代以内近亲
{
//如果全部是是-1无从考证直接符合
if(a == -1 || b == -1 ) return 1;
//可以考证的前提下相同 那肯定不行呀判断为近亲结婚
if((father[a] != -1 && father[a] == father[b])||( mother[a] != -1 && mother[a] == mother[b])) return 0;
num++;//以上两种情况都不符合的情况下开始计数 用来判断是否5代以内
if(num >= 4) return 1;
//接下来判断双方父母是否为近亲 4种组合
return judge(mother[a], mother[b],num) && judge(mother[a], father[b], num) && judge(father[a],mother[b],num) && judge(father[a], father[b],num);
}
int main()
{
//考虑到有些长辈祖先不在咱们输入的id里边 所以父母数组需要初始化
memset(father, -1, sizeof(father));
memset(mother, -1, sizeof(mother));
int n, k;
cin >> n;
while(n--)
{
int id;
cin >> id ;
cin >> sex[id] >> father[id] >> mother[id];
sex[father[id]] = 'M'; //录入的时候记得要跟上父母的性别 因为可以再婚所以父母的id也是可以作为判断阶段的输入
sex[mother[id]] = 'F';
}
cin >> k;
while(k--)
{
int a, b;
cin >> a >> b;
if(sex[a] == sex[b]) //同性是真爱哈
{
cout << "Never Mind" << endl;
}
else if(judge(a, b, 0)) cout << "Yes" << endl; //五代开外就牵手
else cout << "No" << endl; //五代以内就掰掰
}
return 0;
}