PTA 7-2 Family Tree Processing--analog __ data structure

Source: Internet
Author: User
Tags first row

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 was 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) and M (≤), 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 are a sibling of Y X is a descendant of Y × is a
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.


Ideas: All kinds of blind

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <
vector> #include <string> #include <map> using namespace std;

const int MAXN = 1000;
Char STR[MAXN];
string s, op;
int n, M, ZEROCNT[MAXN], HEAD[MAXN], PAR[MAXN];
Vector<int> SON[MAXN];

Map<string, int> key;
    BOOL DFS1 (int x, int y) {if (x = = y) return true;
    for (unsigned int i = 0; i < son[x].size (); i++) {if (DFS1 (son[x][i), y)) return true;
return false;
    BOOL DFS2 (int x, int y) {if (x = = 0) return false;
    if (x = = y) return true;
    if (DFS2 (par[x], y)) return true;
return false;
    } void solve (int x) {gets (str);
    int i = 0;
    string S;
    for (; str[i] = = '; i++) zerocnt[x]++;
    for (; str[i]; i++) S.push_back (str[i));
    Key[s] = x;
HEAD[ZEROCNT[X]] = x;
    int main () {scanf ("%d%d", &n, &m); GetChar ();
    memset (zerocnt, 0, sizeof (zerocnt)); memset (Head, 0, sizeof (head));
    memset (par, 0, sizeof (PAR));
    for (int i = 1; I <= n; i++) son[i].clear ();
    Solve (1);
        for (int i = 2; I <= n; i++) {Solve (i);
        int u = head[zerocnt[i]-2], v = i; PAR[V] = u;
    Son[u].push_back (v);
        for (int i = 1; I <= m i++) {cin >> s; int u = key[s];
        Cin >> s >> S >> op >> s; Cin >> S;
        int v = key[s];
        BOOL OK;
            if (op[0] = = ' C ') {OK = false;
        for (unsigned int j = 0; J < Son[v].size (); j + +) if (son[v][j] = = u) {ok = true; break;}
        else if (op[0] = = ' P ') {OK = (par[v] = u);
        else if (op[0] = = ' d ') {OK = DFS1 (v, u);
        else if (op[0] = = ' a ') {OK = dfs2 (v, u);
            else {if (par[u] = = Par[v] && zerocnt[u] = = Zerocnt[v] OK = true;
        else OK = false; if (OK) printf("true\n");
    else printf ("false\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.