天天看點

問題 : 找出直系親屬

題目描述

如果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-。

輸入

輸入包含多組測試用例,每組用例首先包含2個整數n(0<=n<=26)和m(0

great-grandparent

AC代碼

#include<cstdio>
#include<iostream>
using namespace std;
const int INF=;

int map[][];

void Floyd() {
    for(int k =  ; k <=  ; k ++)
        for(int i =  ; i <=  ; i ++)
            for(int j =  ; j <=  ; j ++)
                if(map[i][j] > map[i][k] + map[k][j])
                    map[i][j] = map[i][k] + map[k][j];
}

int minn(int x, int y) {
    return x < y ? x : y;
}

int main () {
    int n,m,i,j,a,b,c;
    char str[];
    while(scanf("%d %d",&n,&m) && n + m) {
        for(i =  ; i <=  ; i ++)
            for(j =  ; j <=  ; j ++)
                if(i == j)map[i][j] = ;
                else map[i][j] = INF;
        for(i =  ; i <= n ; i ++) {
            scanf("%s",str);
            a = str[];
            b = str[];
            c = str[];
      //      cout<<a<<b<<c<<endl;
            map[a][b] = map[a][c] = ;
        }
        Floyd();
        for(i =  ; i <= m ; i ++) {
            scanf("%s",str);
            a = str[];
            b = str[];
            c = minn(map[a][b],map[b][a]);
            if(c == INF || !c) {
                printf("-\n");
                continue;
            }
            if(c == ) {
                if(map[a][b] == )
                    printf("child\n");
                else
                    printf("parent\n");
            } else {
                if(map[a][b] != INF) {
                    c -= ;
                    for(j =  ; j <= c ; j ++)
                        printf("great-");
                    printf("grandchild\n");
                } else {
                    c -= ;
                    for(j =  ; j <= c ; j ++)
                        printf("great-");
                    printf("grandparent\n");
                }
            }
        }
    }
    return ;
}