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 ≤), 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
Thinking: two dfs functions, first search the five generations of relatives of the first person and mark them, then find out whether the five generations of relatives of the other person repeat with them.
#include<bits/stdc++.h> using namespace std; int vis[100005],f; struct peo { int fu,mu; char sex; peo():fu(-1),mu(-1){} //Structure initialization,Be sure to assign both parents' values to-1,Otherwise, it will recurse to id Where is 0, an error occurred!! }a[100005]; int dfs(int x,int y) //Find out the five generations of the first one { if(y>5) return 0; vis[x]=1; //Relative position is marked as 1 if(a[x].fu!=-1) dfs(a[x].fu,y+1); if(a[x].mu!=-1) dfs(a[x].mu,y+1); } int dfss(int x,int y) //Find out the next five generations of relatives { if(y>5) return 0; if(vis[x]==0) { if(a[x].fu!=-1) dfss(a[x].fu,y+1); if(a[x].mu!=-1) dfss(a[x].mu,y+1); } else { f=1; //If visited, mark as 1, jump out return 0; } } int main() { int i,x,y,k,id,fu,mu,n; char sex; cin>>n; for(i=1;i<=n;i++) { scanf("%d %c %d %d",&id,&sex,&fu,&mu); a[id].fu=fu; a[id].mu=mu; a[id].sex=sex; a[fu].sex='M'; //Be sure to assign a value to the gender of your parents!!!! a[mu].sex='F'; } cin>>k; while(k--) { scanf("%d%d",&x,&y); if(a[x].sex==a[y].sex) { printf("Never Mind\n"); } else { memset(vis,0,sizeof(vis)); f=0; dfs(x,1); dfss(y,1); if(f==1) printf("No\n"); else printf("Yes\n"); } } return 0; }