题目很水,直接用Floyd即可
/*
* hdu3786/win.cpp
* Created on: 2011-9-6
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define SIZE 100
#define MAX 0x7fffffff
int map[SIZE][SIZE];
int N;
void init() {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
map[i][j] = MAX;
}
map[i][i] = 0;
}
}
void Floyd() {
int i, j, k;
for (k = 0; k < N; k++) {
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (map[i][k] < map[i][j] - map[k][j]) {
map[i][j] = map[i][k] + map[k][j];
}
}
}
}
}
void work() {
int n, m, a, b, temp;
char input[20];
N = 26;
while (scanf("%d %d", &n, &m) == 2) {
if (n == 0 && m == 0) {
break;
}
init();
for (int i = 0; i < n; i++) {
scanf("%s", input);
a = input[0] - 'A';
if (input[1] != '-') {
b = input[1] - 'A';
map[a][b] = 1;
}
if (input[2] != '-') {
b = input[2] - 'A';
map[a][b] = 1;
}
}
Floyd();
for (int i = 0; i < m; i++) {
scanf("%s", input);
a = input[0] - 'A';
b = input[1] - 'A';
if (map[a][b] < MAX) {
temp = map[a][b];
while (temp > 2) {
printf("great-");
temp--;
}
if (temp > 1) {
printf("grand");
}
printf("child");
} else if (map[b][a] < MAX) {
temp = map[b][a];
while (temp > 2) {
printf("great-");
temp--;
}
if (temp > 1) {
printf("grand");
}
printf("parent");
} else {
putchar('-');
}
putchar('\n');
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
work();
return 0;
}