题目1035:找出直系亲属
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
new Task().solve() ;
}
}
class Task{
InputReader in = new InputReader(System.in) ;
PrintWriter out = new PrintWriter(System.out) ;
class Node{
Node father , mother ;
char val ;
Node(char val , Node father , Node mother){
this.val = val ;
this.father = father ;
this.mother = mother ;
}
}
int dfs(Node first , Node second){
if(first == second) return 0 ;
if(first.father != null){
int high = dfs(first.father , second) ;
if(high != -1) return high + 1 ;
}
if(first.mother != null){
int high = dfs(first.mother , second) ;
if(high != -1) return high + 1 ;
}
return -1 ;
}
void solve(){
int n , m ;
while(true){
n = in.nextInt() ;
m = in.nextInt() ;
if(n == 0 && m == 0) break ;
Map<Character , Node> mapper = new HashMap<Character, Task.Node>() ;
while(n-- > 0){
String args = in.next() ;
Node father = mapper.get(args.charAt(1)) ;
if(father == null){
father = new Node(args.charAt(1) , null , null) ;
mapper.put(args.charAt(1), father) ;
}
Node mother = mapper.get(args.charAt(2)) ;
if(mother == null){
mother = new Node(args.charAt(2) , null , null) ;
mapper.put(args.charAt(2), mother) ;
}
Node node = mapper.get(args.charAt(0)) ;
if(node == null){
node = new Node(args.charAt(0) , father, mother) ;
mapper.put(args.charAt(0), node) ;
}
else{
node.father = father ;
node.mother = mother ;
}
}
while(m-- > 0){
String args = in.next() ;
if(args.charAt(0) == args.charAt(1)){
out.println("-") ;
continue ;
}
Node first = mapper.get(args.charAt(0)) ;
Node second = mapper.get(args.charAt(1)) ;
if(first == null || second == null){
out.println("-") ;
continue ;
}
int cnt = dfs(first, second) ;
if(cnt != -1){
if(cnt == 1) out.println("child") ;
else if(cnt == 2) out.println("grandchild") ;
else{
for(int i = 1 ; i <= cnt - 2 ; i++) out.print("great-") ;
out.println("grandchild") ;
}
}
else{
cnt = dfs(second , first) ;
if(cnt != -1){
if(cnt == 1) out.println("parent") ;
else if(cnt == 2) out.println("grandparent") ;
else{
for(int i = 1 ; i <= cnt - 2 ; i++) out.print("great-") ;
out.println("grandparent") ;
}
}
else out.println("-") ;
}
}
}
out.flush();
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = new StringTokenizer("");
}
private void eat(String s) {
tokenizer = new StringTokenizer(s);
}
public String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}