找出直系亲属

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

#include
#include
int find(int array[],int x,int y)
{
int k=0,mark=0;
while(array[x] > 0)
{
k++;
if(array[x]==y){mark=1;break;}
x=array[x];
}
if(mark)
return k;
else
return 0;
}
int main()
{
int child[27];
int i,k,j,flag,tmpflag;
char strchild[10],relation[10];
while(1)
{
scanf("%d%d",&i,&k);
if(!i&&!k)break;
scanf("\n");
for(j=1;j<=26;j++)
child[j]=0;
while(i--)
{
gets(strchild);
if(strchild[1] != '-')
child[strchild[1]-'A'+1] =strchild[0]-'A'+1;
if(strchild[2] != '-')
child[strchild[2]-'A'+1] =strchild[0]-'A'+1;
}

while(k--)
{
    gets(relation);
    flag = find(child,relation[0]-'A'+1,relation[1]-'A'+1);
    tmpflag = find(child,relation[1]-'A'+1,relation[0]-'A'+1);
    if(!flag&&!tmpflag)printf("-\n");
    else if(flag&&!tmpflag)
    {
        while(flag>2)
        {
            printf("great-");
            flag--;
        }
        if(flag==2)printf("grandparent\n");
        else if(flag==1)printf("parent\n");
    }
    else if(!flag&&tmpflag)
    {
        while(tmpflag>2)
        {
            printf("great-");
            tmpflag--;
        }
        if(tmpflag==2)printf("grandchild\n");
        else if(tmpflag==1)printf("child\n");
    }
}
}
return 0;

}