Topic restatement
Ha-ha. We all know that intermarriage is not allowed within five uniforms, that is, two people cannot intermarry if their nearest common ancestor is within five generations (i.e. themselves, parents, grandparents, great grandparents, and great grandparents). Please help a couple of lovers to judge whether they can get married or not?
Input format:
Enter the first line to give a positive integer N (2 ≤ N ≤ 10 4), then N lines, each line to give a person's information in the following format:
My ID gender father ID mother ID
Among them, ID is 5 digits, each person is different; gender M represents male and F represents female. If someone's father or mother is no longer available, the corresponding ID location is marked - 1.
Next, we give a positive integer k, followed by K lines, each line giving a pair of lovers' ID S, separated by spaces.
Note: the topic ensures that two people are of the same generation, each of them has only one gender, and there is no incest or marriage between different generations in the blood relationship network.
Output format:
For each pair of lovers, judge whether their relationship can be intermarried: if they are of the same sex, output Never Mind; if they are of the opposite sex and the relationship has five services, output Yes; if the relationship has not five services, output No.
Input example:
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
Output example:
Never Mind Yes Never Mind No Yes No Yes No No
Title Solution
The specific idea is to traverse the code from the bottom to the top, traverse five generations, and set up a global variable to determine whether there is a common ancestor.
The problem lies in that you have to consider inputting the gender of the sample parents, because it allows you to compare between the parents. If you do not initialize, the gender field is empty, so it will always be the same, and there is no way to output the correct results.
C++ AC
#include <iostream> #include <bits/stdc++.h> using namespace std; struct node { char sex; int f_Id; int m_Id; }; node nds[100010]; bool isHasCommon=false; void dfs(int f_id, int m_id, int generation) { //cout<<f_id<<":"<<m_id<<endl; //If any of them don't exist, there's no need to compare them if(f_id == -1 || m_id == -1) return; //Number of control generations if(generation >= 5) return; //Have a common ancestor if(f_id == m_id) { isHasCommon = true; } //It can be understood as the father of the man and the father of the man dfs(nds[f_id].f_Id, nds[m_id].f_Id, generation + 1); dfs(nds[f_id].m_Id, nds[m_id].m_Id, generation + 1); dfs(nds[f_id].m_Id, nds[m_id].f_Id, generation + 1); dfs(nds[f_id].f_Id, nds[m_id].m_Id, generation + 1); } int main() { for(int i=0;i<100010;i++) { nds[i].f_Id=nds[i].m_Id=-1; } int n,id; cin>>n; for(int i=0; i<n; i++) { scanf("%d",&id); cin>>nds[id].sex>>nds[id].f_Id>>nds[id].m_Id; //Here is the point!!! You have to think about your parents' gender!! if(nds[id].f_Id!=-1) { nds[nds[id].f_Id].sex='M'; } if(nds[id].m_Id!=-1) { nds[nds[id].m_Id].sex='F'; } //cout<<id<<endl; } int k,tmp1,tmp2; cin>>k; for(int i=0; i<k; i++) { isHasCommon=false; cin>>tmp1>>tmp2; if(nds[tmp1].sex == nds[tmp2].sex) { cout << "Never Mind" << endl; } else { dfs(tmp1, tmp2, 0); if(!isHasCommon) cout << "Yes" << endl; else cout << "No" << endl; } } return 0; }