hdu-3786-找出直系親屬

Problem Description
如果A,B是C的父母親,則A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,則A,B是C的grandparent,C是A,B的grandchild,如果A,B是C的(外)曾祖父,曾祖母,則A,B是C的great-grandparent,C是A,B的great-grandchild,之後再多一輩,則在關係上加一個great-。

Input
輸入包含多組測試用例,每組用例首先包含2個整數n(0<=n<=26)和m(0<m<50), 分別表示有n個親屬關係和m個問題, 然後接下來是n行的形式如ABC的字符串,表示A的父母親分別是B和C,如果A的父母親信息不全,則用-代替,例如A-C,再然後是m行形式如FA的字符串,表示詢問F和A的關係。
當n和m爲0時結束輸入。

Output
如果詢問的2個人是直系親屬,請按題目描述輸出2者的關係,如果沒有直系關係,請輸出-。
具體含義和輸出格式參見樣例.

Sample Input
3 2 ABC CDE EFG FA BE 0 0

Sample Output
great-grandparent -

二叉樹(數組實現) + dfs

#include <stdio.h>

#include <stdlib.h>
#include <string.h>


typedef struct tagTNode
{
    int f;
    int m;
}TNode;


TNode table[26];
int dep, result, find;


void srh(int x, int y)
{
    if (x < 26) 
    {
        if (find) 
        {
            return;
        }


        if (table[x].f == y || table[x].m == y) 
        {
            dep++;
            result = dep;
            find = 1;
            return;
        }


        if (table[x].f != '-') 
        {
            dep++;
            srh(table[x].f, y);
            dep--;
        }


        if (table[x].m != '-') 
        {
            dep++;
            srh(table[x].m, y);
            dep--;
        }
    }
}


void output(int gener, int postfixFthr)
{
    if (gener == 1) 
    {
        if (postfixFthr) 
        {
            printf("parent\n");
        }
        else
        {
            printf("child\n");
        }
    }
    else if (gener == 2) 
    {
        if (postfixFthr) 
        {
            printf("grandparent\n");
        }
        else
        {
            printf("grandchild\n");
        }
    }
    else
    {
        int i;


        for (i = 0; i < gener - 2; ++i) 
        {
            printf("great-");
        }
        if (postfixFthr) 
        {
            printf("grandparent\n");
        }
        else
        {
            printf("grandchild\n");
        }
    }
}


int main()
{
    char ch, fa, mo;
    char str[6];
    int n, m, i;
    int x, y;


    while (scanf("%d%d", &n, &m) != EOF && n != 0 && m != 0) 
    {
        for (i = 0; i < 26; ++i) 
        {
            table[i].f = '-';
            table[i].m = '-';
        }
        getchar();
        for (i = 0; i < n; ++i) 
        {
            gets(str);
            if (str[1] != '-') 
            {
                table[str[0] - 'A'].f = str[1] - 'A';
            }
            if (str[2] != '-')
            {
                table[str[0] - 'A'].m = str[2] - 'A';
            }
        }
        for (i = 0; i < m; ++i) 
        {
            gets(str);
            x = str[0] - 'A';
            y = str[1] - 'A';


            dep = 0;
            result = 0;
            find = 0;
            srh(x, y);
            if (find) 
            {
                output(result, 0);
            }
            else
            {
                dep = 0;
                result = 0;
                find = 0;
                srh(y, x);
                if (find) 
                {
                    output(result, 1);
                }
                else
                {
                    printf("-\n");
                }
            }
        }
    }


    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章