#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = 32;
int child[MAXN];
void Initial() {
for (int i = 0; i < MAXN; i++) {
child[i] = i;
}
}
bool FindChild(int x, int y, int& generation) {
while (x != y && x != child[x]) {
generation++;
x = child[x];
}
if (x == y) {
return true;
}
else {
return false;
}
}
int main() {
int n, m;
string str;
while (scanf("%d%d", &n, &m) != EOF) {
Initial();
for (int i = 0; i < n; i++) {
cin >> str;
child[str[1] - 'A'] = str[0] - 'A';
child[str[2] - 'A'] = str[0] - 'A';
}
while (m--) {
cin >> str;
int a = str[0] - 'A';
int b = str[1] - 'A';
int g1 = 0, g2 = 0;
if (FindChild(a, b, g1)) {
if (g1 == 1) {
printf("parent\n");
}
else {
while (g1 > 2) {
printf("great-");
g1--;
}
printf("grandparent\n");
}
}
else if (FindChild(b, a, g2)) {
if (g2 == 1) {
printf("child\n");
}
else {
while (g2 > 2) {
printf("great-");
g2--;
}
printf("grandchild\n");
}
}
else {
printf("-\n");
}
}
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/104447.html
標籤:C++ 語言
上一篇:求救急
