#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
****************************************************************/
题目1035:找出直系亲属
最新推荐文章于 2020-10-03 15:30:30 发布