/* * 2005/12/03 * * 空行が来るとかでハマった…。 * こういうのはJavaの方が書きやすい感じ。 */ import java.util.Scanner; import java.util.Vector; public class Family { public static void main(String[] args) { int hoge = 0; Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String str = sc.nextLine(); Scanner sc1 = new Scanner(str); int n1 = sc1.nextInt(); int n2 = sc1.nextInt(); if(n1 == n2 && n1 == 0) { break; } if(hoge++ != 0) { System.out.println(); } Node[] nodes = new Node[n1]; for(int i = 0; i < n1; i++) { String line = sc.nextLine(); int start = 0; for(; ; start++) { if(line.charAt(start) != ' ') { break; } } String name = line.substring(start); nodes[i] = new Node(name, start); } for(int i = 1; i < n1; i++) { Node n = nodes[i]; Node pre = nodes[i-1]; if(n.gen > pre.gen) { pre.addChild(n); } else if(n.gen < pre.gen) { while(n.gen != pre.gen) { pre = pre.parent; } pre.parent.addChild(n); } else { // if n.gen == pre.gen pre.parent.addChild(n); } } for(int i = 0; i < n2; i++) { String line = sc.nextLine(); while(line.equals("")) { line = sc.nextLine(); } sc1 = new Scanner(line); String name1 = sc1.next(); String dumy = sc1.next(); dumy = sc1.next(); String type = sc1.next(); dumy = sc1.next(); String name2 = sc1.next(); name2 = name2.substring(0,name2.length() - 1); int index1=0, index2=0; for(int j = 0; j < nodes.length; j++) { if(nodes[j].name.equals(name1)) { index1 = j; } else if(nodes[j].name.equals(name2)) { index2 = j; } } if(check(nodes[index1], type, nodes[index2])) { System.out.println("True"); } else { System.out.println("False"); } } } } public static boolean check(Node X, String type, Node Y) { if(type.equals("child")) { return Y.hasChild(X); } else if(type.equals("parent")) { return X.hasChild(Y); } else if(type.equals("sibling")) { return X.parent == Y.parent; } else if(type.equals("descendant")) { return X.hasAncestor(Y); } else if(type.equals("ancestor")) { return Y.hasAncestor(X); } return false; } } class Node { Node parent; Vector children = new Vector(); public int gen; public String name; public Node(String name, int gen) { this.name = name; this.gen = gen; } public void addChild(Node child) { child.parent = this; children.add(child); } public boolean hasChild(Node child) { return children.contains(child); } public boolean hasAncestor(Node ancestor) { Node n = this; do { if(n == ancestor) { return true; } n = n.parent; } while(n != null); return false; } }