题目1035:找出直系亲属

#include <cstdio>
#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<string.h>
#include<string>
#include<set>
#include<queue>
#include<stack>
using namespace std;
struct N
{
    int f;
    int m;
}node[30];
void findParent(int a,int b,bool &flag,int level,int &ret)
{
    if(a==b)
    {
        flag=true;
        ret=level;
        return;
    }
    if(node[a].f!=-1)
    findParent(node[a].f,b,flag,level+1,ret);
    if(!flag)
        if(node[a].m!=-1)
        findParent(node[a].m,b,flag,level+1,ret);
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            break;
        memset(node,-1,sizeof(node));
        for(int i=0;i<n;i++)
        {
            char a,b,c;
            cin>>a>>b>>c;
            int x=a-'A';
            if(b!='-')
                node[x].f=b-'A';
            if(c!='-')
                node[x].m=c-'A';
        }
        while(m--)
        {
            char a,b;
            cin>>a>>b;
            bool flag1=false,flag2=false;
            int level1=0,level2=0;
            findParent(a-'A',b-'A',flag1,0,level1);
            findParent(b-'A',a-'A',flag2,0,level2);
            if(flag1)   ///a是b的儿子
            {
                while(level1>2)
                {
                    cout<<"great-";
                    level1--;
                }
                if(level1>1)
                    cout<<"grand";
                cout<<"child"<<endl;
            }
            else if(flag2)
            {
                while(level2>2)
                {
                    cout<<"great-";
                    level2--;
                }
                if(level2>1)
                    cout<<"grand";
                cout<<"parent"<<endl;
            }
            else
                cout<<"-"<<endl;
        }
    }
    return 0;
}
 
/**************************************************************
    Problem: 1035
    User: cust123
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值