L2-016 wish all lovers in the world are brothers and sisters who have been separated for many years (25 points)
ha-ha. We all know that intermarriage is not allowed within five clothes, that is, if the nearest common ancestor of two people is within five generations (i.e. myself, parents, grandparents, great grandparents and high grandparents), they cannot intermarry. Please help a couple to judge whether they can get married or not?
Input format:
Input the first line to give a positive integer N (2 ≤ N ≤ 10 4), and then N lines. Each line gives a person's information in the following format:
My ID gender father ID mother ID
The ID is 5 digits, which is different for each person; Gender M stands for male and F for female. If a person's father or mother is no longer available, the corresponding ID position is marked with - 1.
Next, a positive integer k is given, followed by K lines. Each line gives the ID s of a pair of lovers, separated by spaces.
Note: the title ensures that two people are of the same generation, each has only one gender, and there is no incest or marriage between 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 it is heterosexual and related to five clothes, output Yes; If the heterosexual relationship does not produce five clothes, output No.
Input sample:
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
The test site of this question is still obvious, test tree
Therefore, vector is indispensable, and one point needs to be paid attention to
How to judge the "five clothes"? Enter the number of two people in the title as the starting point of DFS, and perform DFS traversal respectively. If the traversal is completed, the number of layers is greater than or equal to 5 (I thought it was > 5, because it is not possible within five generations, but it is only greater than five, and the sample output cannot be obtained, so it can only be > =). If the previous person has traversed this node in the traversal process, However, this node is also encountered in the traversal process of the second person, and it is less than 5, which is within the "five services"
To put it bluntly, it is to check whether duplicate nodes have been traversed when the DFS process is less than 5 layers. If yes, send it
Then there are some details. The first one is the vis [] array, which needs to be reset every cycle. Here I use fill, and then the global variable flag.
The last point is also very important. At the beginning, I didn't notice that I carefully observed the input samples. The input samples have numbers of > 24 such as 09999. However, when checking whether there are five clothes, it is not stipulated that the input number must be < = 24, so every time I input the number of my parents, I should add gender to them, so that they can be fully covered!
#include <iostream> #include <algorithm> #include <vector> using namespace std; const int maxn = 100050; int n, k; vector<int> child[maxn]; bool vis[maxn]; string sexList[maxn]; bool flag; void DFS(int s, int depth) { vis[s] = true; if (depth >= 5) return; for (int i = 0; i < child[s].size(); i++) { if (vis[child[s][i]] == false) DFS(child[s][i], depth + 1); else flag = false; } } int main() { int id, mother, father; int p1, p2; char sex; cin >> n; for (int i = 0; i < n; i++) { cin >> id >> sex >> father >> mother ; sexList[id] = sex; if (mother != -1) { sexList[mother] = "F"; child[id].push_back(mother); } if (father != -1) { sexList[father] = "M"; child[id].push_back(father); } } cin >> k; for (int i = 0; i < k; i++) { cin >> p1 >> p2; if (sexList[p1] == sexList[p2]) cout << "Never Mind" << endl; else { //Reset after each input fill(vis, vis + maxn, false); flag = true; DFS(p1, 1); DFS(p2, 1); if (flag == false) cout << "No"<< endl; else cout << "Yes" << endl; } } return 0; }