[PTA-tiandti Competition Training] may all lovers in the world be siblings who have been lost for many years, pta-all lovers in the world

Source: Internet
Author: User

[PTA-tiandti Competition Training] may all lovers in the world be siblings who have been lost for many years, pta-all lovers in the world

Haha. We all know that marriage is not allowed within five servers. That is to say, if the recent ancestor of two people is within five generations (that is, I, my parents, grandparents, grandparents, and grandparents), marriage is not allowed. In this question, please help a lover determine whether they can get married?

Input Format:

Enter the first line to give a positive integer.N(2 ≤N≤), FollowedNLine. Each line provides a person's information in the following format:

ID: Gender, father ID, mother ID

WhereIDIt is a five-digit number, different from each other; GenderMRepresents male,FRepresenting women. If one's father or mother cannot take the testIDMark the position-1.

Next, a positive integer is given.K, And thenKLine. Each line provides a pairIDAnd are separated by spaces.

Note: The subject ensures that two people are of the same gender and each person has only one gender, and there is no incest or marriage between the generations in the kinship network.

Output Format:

Determine whether a pair of lovers can intermarry their relationship: if the two are of the same sex, outputNever Mind; If the link is out of five servers, the output isYes; If the heterosexual relationship does not output five servers, the outputNo.

Input example:
2400001 M 01111 -100002 F 02222 0333300003 M 02222 0333300004 F 04444 0333300005 M 04444 0555500006 F 04444 0555500007 F 06666 0777700008 M 06666 0777700009 M 00001 0000200010 M 00003 0000600011 F 00005 0000700012 F 00008 0888800013 F 00009 0001100014 M 00010 0999900015 M 00010 0999900016 M 10000 0001200017 F -1 0001200018 F 11000 0001300019 F 11100 0001800020 F 00015 1111000021 M 11100 0002000022 M 00016 -100023 M 10012 0001700024 M 00022 10013900021 0002400019 0002400011 0001200022 0001800001 0000400013 0001600017 0001500019 0002100010 00011
Output example:
Never MindYesNever MindNoYesNoYesNoNo
Train of Thought: two dfs functions, first search for the first generation of kinship and mark it, and then find another generation of kinship to repeat it.
# Include <bits/stdc ++. h> using namespace std; int vis [100005], f; struct peo {int fu, mu; char sex; peo (): fu (-1), mu (-1) {} // initialize the struct. You must first assign the value of both parents to-1. Otherwise, it will recursively go to the place where the id is 0 and an error occurs !!} A [100005]; int dfs (int x, int y) // first find the first generation of kinship {if (y> 5) return 0; vis [x] = 1; // The kinship location 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 the five generation relatives of the second person {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 you have accessed it, mark it as 1 and 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", & id, & sex, & fu, & mu); a [id]. fu = fu; a [id]. mu = mu; a [id]. sex = sex; a [fu]. sex = 'M'; // You must assign a gender value to your parents !!!! A [mu]. sex = 'F';} cin> k; while (k --) {scanf ("% 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 ;}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.