7-5 Family Tree Processing (30 points) data structure

Source: Internet
Author: User

7-5 Family Tree Processing (30 points)

Anthropological studies were of interest to the family, so the researchers collected some family genealogy for research. In the experiment, the computer is used to process genealogy. To achieve this, the researchers converted the genealogy into a text file. The following is an example of a genealogy text file:

John
Robert
Frank
Andrew
Nancy
David

In a genealogy text file, each row contains a person's name. The first line of the name is the earliest ancestor of the family. The family tree contains only the descendants of the earliest ancestors, and their husbands or wives do not appear in the home spectrum. Each person's children indent 2 spaces more than their parents. Take the above genealogy text file for example, John the family's earliest ancestor, he has two children Robert and Nancy,robert have two children, Frank and andrew,nancy have only one child David.

In the experiment, the researchers also collected family papers and extracted statements about the two-person relationship in the genealogy. The following is an example of a statement of relationships in a family tree:

John is the parent of Robert
Robert is a sibling of Nancy
David is a descendant of Robert

The researchers need to determine whether each statement is true or false, and write a program to help the researchers determine.
Input format:

Input first gives 2 positive integers n (2≤n≤100) and M (≤100), where n is the number of names in the family tree, M is the number of statements in the family tree, and the input is no more than 70 characters per line.

The string of names consists of no more than 10 English letters. No spaces are indented before the first row in the family tree is given a name. The other names in the family tree are indented at least 2 spaces, that is, they are descendants of the earliest ancestors in the family tree (the first row gives the name), and if a name in the family tree indents K spaces, the first name in the next line indents at most k+2 spaces.

The same name does not appear two times in a family tree, and names that do not appear in the family tree do not appear in the statement. Each statement is formatted as follows, where x and Y are different names in the family tree:

X is a child of Y
X is the parent of Y
X is a sibling of Y
X is a descendant of Y
X is an ancestor of Y

Output format:

For each statement in a test case, output true in one line, if the statement is true, or false if the statement is false.
Input Sample:

6 5
John
Robert
Frank
Andrew
Nancy
David
Robert is a child of John
Robert is an ancestor of Andrew
Robert is a sibling of Nancy
Nancy is the parent of Frank
John is a descendant of Andrew

Output Sample:

True
True
True
False
False

Ideas
There are two ways to find parents.
0. You can traverse the first number of spaces that are less than his parents.
1. You can use an array to maintain the current number of spaces/2 so the current number of spaces/2-1 of the people is his parents

Then be aware
0.sibling is the same person as the parents, not the same number of spaces.
1.ancestor is the parents of parents or parents ... And not just the number of spaces is less than the

AC Code

#include <cstdio> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cmath > #include <climits> #include <ctime> #include <iostream> #include <algorithm> #include < deque> #include <vector> #include <queue> #include <string> #include <map> #include <stack > #include <set> #include <numeric> #include <sstream> #include <iomanip> #include <limits
> #define CLR (a) memset (a, 0, sizeof (a)) #define PB push_back using namespace std;
typedef long Long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair &LT;INT, int> PII;
typedef pair &LT;LL, ll> PLL;
typedef pair<string, Int> psi;

typedef pair<string, string> PSS;
Const double PI = 3.14159265358979323846264338327;
Const DOUBLE E = exp (1);

Const double EPS = 1e-30;
const int INF = 0X3F3F3F3F;
const int MAXN = 1e2 + 5;

const int MOD = 1e9 + 7;

int PRE[MAXN]; struct Node {
    string S;
    string P;
int k;

}Q[MAXN]; 

Map <string, int> vis;

String W[MAXN];


void Init () {for (int i = 0; i < MAXN i++) pre[i] = i;}
    int main () {init ();
    int n, m, k = 0;
    scanf ("%d%d", &n, &m);
    string s, temp;
    Getline (CIN, temp);
    Q[1].S = temp;
    Q[1].K = k;
    W[0] = temp;
    Vis[temp] = 1; 
    PRE[1] =-1;
    int Len;
        for (int i = 2; I <= n; i++) {getline (CIN, s);
        k = 0;
        Len = S.size ();
        Temp.clear ();
            for (int j = 0; J < Len; J +) {if (s[j] = = ") k++;
        else temp = s[j];
        } Q[I].S = temp;
        Vis[temp] = i;
        Q[I].K = k;
        Pre[i] = VIS[W[K/2-1]];
    W[K/2] = temp;
    } string A, B;
        for (int i = 0; i < m; i++) {cin >> A;
        Cin >> S;
        Cin >> S;
      if (s = = "a")//1 3 5 {      Cin >> S;
                if (s = = "Child")//1 {cin >> s;
                CIN >> B;
                int id_a = Vis[a], id_b = vis[b];
                if (pre[id_a] = = id_b) printf ("true\n");
            else printf ("false\n");
                else if (s = = "sibling")//3 {cin >> S;
                CIN >> B;
                int id_a = Vis[a], id_b = vis[b];
                if (pre[id_a] = = Pre[id_b] && id_a!= id_b) printf ("true\n");
            else printf ("false\n");
                else if (s = = "descendant")//5 {cin >> S;
                CIN >> B;
                int id_a = Vis[a], id_b = vis[b];
                while (Pre[id_a]!= id_b && id_a!= 1) id_a = pre[id_a];
        if (id_a!= 1)            printf ("true\n");
            else printf ("false\n");
            } else if (s = = "an")//2 {cin >> S;
            Cin >> S;
            CIN >> B;
            int id_a = Vis[a], id_b = vis[b];
            while (Pre[id_b]!= id_a && id_b!= 1) id_b = Pre[id_b];
            if (id_b!= 1) printf ("true\n");
        else printf ("false\n");
            else if (s = = "the")//4 {cin >> S;
            Cin >> S;
            CIN >> B;
            int id_a = Vis[a], id_b = vis[b];
            if (pre[id_b] = = id_a) printf ("true\n");
        else printf ("false\n");






 }
    }
}

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.