Problem:
Problem B:
Family Tree
A professor of anthropology was interested in people living in isolated islands and their history.
He collected their family trees to conduct some anthropological experiment. For the experiment,
he needed to process the family trees with a computer. For that purpose he translated them
into text files. The following is an example of a text file representing a family tree.
John
 Robert
  Frank
  Andrew
 Nancy
  David
Each line contains the given name of a person. The name in the first line is the oldest ancestor
in this family tree. The family tree contains only the descendants of the oldest ancestor. Their
husbands and wives are not shown in the family tree. The children of a person are indented
with one more space than the parent. For example, Robert and Nancy are the children of John,
and Frank and Andrew are the children of Robert. David is indented with one more space than
Robert, but he is not a child of Robert, but of Nancy. To represent a family tree in this way,
the professor excluded some people from the family trees so that no one had both parents in a
family tree.
For the experiment, the professor also collected documents of the families and extracted the
set of statements about relations of two persons in each family tree. The following are some
examples of statements about the family above.
John is the parent of Robert.
Robert is a sibling of Nancy.
David is a descendant of Robert.
For the experiment, he needs to check whether each statement is true or not. For example, the
first two statements above are true and the last statement is false. Since this task is tedious, he
would like to check it by a computer program.
Input
The input contains several data sets. Each data set consists of a family tree and a set of
statements. The first line of each data set contains two integers
n
(0 <
n
< 1000) and
m
(0 <
m
< 1000) which represent the number of names in the family tree and the number of
statements, respectively. Each line of the input has less than 70 characters.
As a name, we consider any character string consisting of only alphabetic characters. The names
in a family tree have less than 20 characters. The name in the first line of the family tree has
no leading spaces. The other names in the family tree are indented with at least one space, i.e.,
they are descendants of the person in the first line. You can assume that if a name in the family
tree is indented with
k
spaces, the name in the next line is indented with at most
k
+ 1 spaces.
This guarantees that each person except the oldest ancestor has his or her parent in the family
tree. No name appears twice in the same family tree. Each line of the family tree contains no
redundant spaces at the end.
Each statement occupies one line and is written in one of the following formats, where
X
and
Y
are different names in the family tree.
X
is a child of
Y
.
X
is the parent of
Y
.
X
is a sibling of
Y
.
X
is a descendant of
Y
.
X
is an ancestor of
Y
.
Names not appe
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
int dep[100010];
int par[100010][19];
vector<int> g[100010];
void dfs(int v,int u,int d){
  dep[v]=d;
  for(int i=0; i<g[v].size(); i++){
    int w=g[v][i];if(w==u) continue;
    par[w][0]=v;dfs(w,v,d+1);
  }
}
int lcp(int a,int b){
  if(dep[a]<dep[b]) swap(a,b);
  for(int i=0; i<19; i++){
    if((dep[a]-dep[b])&(1<<i)) a=par[a][i];
  }
  if(a==b) return a;
  for(int i=18;i>=0;i--){
    if(par[a][i]!=par[b][i]) a=par[a][i],b=par[b][i];
  }
  return par[a][0];
}
int n,m;
void init() {
  memset(par,0,sizeof(par));
  dfs(0,-1,0);
  for(int i=0; i<18; i++)for(int j=0; j<n; j++) par[j][i+1]=par[par[j][i]][i];
}

string r[111111];
int main() {
  while(cin >> n >> m && n) {
    for(int i=0; i<n; i++) g[i].clear();
    string s;
    getline(cin,s);
    map<string,int> ma;
    for(int i=0; i<n; i++) {
      getline(cin,s);
      stringstream ss;
      string t;
      ss << s;
      ss >> t;
      ma[t]=i;
      int c=0;
      for(int j=0; j<s.size(); j++) {
        if(s[j]==' ') c++;
        else break;
      }
      if(c) {
        g[ma[t]].push_back(ma[r[c-1]]);
        g[ma[r[c-1]]].push_back(ma[t]);
      }
      r[c]=t;
    }
    init();
    while(m--) {
      getline(cin,s);
      stringstream ss;
      ss << s;
      string x,y,z;
      ss >> x;
      ss >> z;ss >> z;ss >> z;
      ss >> y;ss >> y;
      y=y.substr(0,y.size()-1);
      int xx=ma[x],yy=ma[y];
      bool f=0;
      if(z=="parent") {
        if(lcp(xx,yy)==xx&&dep[yy]-dep[xx]==1) f=1;
      } else if(z=="child") {
        if(lcp(xx,yy)==yy&&dep[xx]-dep[yy]==1) f=1;
      } else if(z=="sibling") {
        if(dep[xx]==dep[yy]&&dep[xx]-dep[lcp(xx,yy)]==1) f=1;
      } else if(z=="descendant") {
        if(lcp(xx,yy)==yy&&dep[xx]-dep[yy]>=1) f=1;
      } else {
        if(lcp(xx,yy)==xx&&dep[yy]-dep[xx]>=1) f=1;
      }
      if(f) cout << "True" << endl;
      else cout << "False" << endl;
    }
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 13916
final_rank: 18

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"

struct tree {
	string name;
	int depth;
	vector<unique_ptr<tree>>childs;
	tree(const string _name,const int _depth) :name(_name),depth(_depth),childs() {

	}
};
unique_ptr<tree> maketree(vector<string>&sts, vector<int>&ns,int depth,int &a) {
	unique_ptr<tree> t(std::make_unique<tree>(sts[a],depth));
	while (a+1!=sts.size()&&ns[a+1] > depth) {
		a++;
		t->childs.push_back(maketree(sts, ns, depth + 1, a));
		
	}
	return t;
}

vector<string> split(const string &str, char delim) {
	vector<string> res;
	size_t current = 0, found;
	while ((found = str.find_first_of(delim, current)) != string::npos) {
		res.push_back(string(str, current, found - current));
		current = found + 1;
	}
	res.push_back(string(str, current, str.size() - current));
	return res;
}
int dfs(const unique_ptr<tree>&tp,int type,string a,string b,bool flag) {
	//a is parant of b
	if (type == 0) {
		if (a == tp->name) {
			for (auto&& c : tp->childs) {
				if (c->name == b)return 1;
			}
			return 0;
		}
		else {
			for (auto &&c : tp->childs) {
				int num=dfs(c, type, a, b,flag);
				if (num != -1)return num;
			}
			return -1;
		}
	}
	else if (type == 1) {
		if (flag) {
			if (tp->name == b)return 1;
			for (auto &&c : tp->childs) {
				int num = dfs(c, type, a, b, flag);
				if (num != -1)return num;
			}
			return -1;
		}
		else {
			if (tp->name == a)flag = true;
			for (auto &&c : tp->childs) {
				int num = dfs(c, type, a, b, flag);
				if (num != -1)return num;
			}
			if (flag)return 0;
			else return -1;
		}
	}
	else {
		int same = 0;
		for (auto&&c : tp->childs) {
			if (c->name == a)same++;
			if (c->name == b)same++;
			int num = dfs(c, type, a, b, flag);
			if (num != -1)return num;
		}
		if (same == 2)return 1;
		else if (same == 1)return 0;
		else return -1;
	}
}

int main() {
	while (1) {

		int N, M; cin >> N >> M;
		if (!N)break;
		unique_ptr<tree> t;
		{
			vector<string>sts;
			vector<int>ns;
			string st; getline(cin, st);
			for (int i = 0; i < N; ++i) {
				getline(cin, st);
				int n = st.find_first_not_of(' ');
				sts.push_back(st.substr(n));
				ns.push_back(n);
			}

			int a = 0;
			t = maketree(sts, ns, 0, a);
		}
		
		while (M--) {
			string st;
			getline(cin, st);
			vector<string>sts(split(st, ' '));
			string a = sts[0];
			string b = sts[5].substr(0, sts[5].size() - 1);
			int ans;
			if (sts[3] == "child") {
				ans = dfs(t, 0, b, a, false);
			}
			else if (sts[3] == "ancestor") {
				ans = dfs(t,1, a, b, false);
			}
			else if (sts[3] == "sibling") {
				ans = dfs(t, 2, a, b, false);
			}
			else if (sts[3] == "parent") {
				ans = dfs(t, 0, a, b, false);
			}
			else if (sts[3] == "descendant") {
				ans = dfs(t, 1, b, a, false);
			}
			if (ans == 1)cout << "True" << endl;
			else if (ans == 0)cout << "False" << endl;
			else assert(false);
		}
		cout << endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3168
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;

map <string, string> child_parent;
string lastname[2000];

bool child(string a, string b){
	if (child_parent[a] == b) return true;
	else return false;
}

bool parent(string a, string b){
	if (a == child_parent[b]) return true;
	else return false;
}

bool sibling(string a, string b){
	if (child_parent[a] == child_parent[b]) return true;
	else return false;
}

bool ancestor(string a, string b){
	if (child_parent[b] == "none") return false;
	else if (parent(a, b)) return true;
	else return ancestor(a, child_parent[b]);
}

bool descendant(string a, string b){
	return ancestor(b, a);
}

int main(void){

	int n, m;

	while (cin >> n >> m, n, m){

		child_parent.clear();
		cin.ignore();

		for (int i = 0; i < n; i++){
			string s;
			getline(cin, s);

			int count = 0;
			for (int j = 0; j < s.size(); j++){
				if (s[j] == ' ') count++;
			}

			if (count == 0) child_parent[s] = "none";
			else child_parent[s.substr(count)] = lastname[count - 1];

			lastname[count] = s.substr(count);

		}

		for (int i = 0; i < m; i++){
			string a, b,relate, buff;
			cin >> a >> buff >> buff >> relate >> buff;
			char bb[200];
			scanf(" %[^.].", &bb);
			b = bb;

			bool flag = false;
			if (relate == "child") flag = child(a, b);
			else if (relate == "parent") flag = parent(a, b);
			else if (relate == "sibling") flag = sibling(a, b);
			else if (relate == "descendant") flag = descendant(a, b);
			else if (relate == "ancestor") flag = ancestor(a, b);

			if (flag) cout << "True" << endl;
			else cout << "False" << endl;
		}

		

		cout << endl;

	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1396
final_rank: 8

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int INF = 1<<28;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

bool des(string a, string b, map<string, string> &m) {
	string s = a;
	while(s != "") {
		if(s == b)
			return true;;
		s = m[s];
	}
	return false;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	while(cin >> n >> m, n|m) {
		cin.ignore();
		vector<string> v(1000);
		map<string, string> ma;
		REP(i, n) {
			string s;
			getline(cin, s);
			int cnt = 0;
			while(s[cnt] == ' ')
				cnt++;
			s = s.substr(cnt);
			ma[s] = cnt ? v[cnt-1] : "";
			v[cnt] = s;
		}
		REP(i, m) {
			string in1, in2, in3;
			cin >> in1;
			cin >> in2 >> in2 >> in2;
			cin >> in3 >> in3;
			in3.erase(in3.end() - 1);
			bool ans = false;
			if(in2 == "child" && ma[in1] == in3)
				ans = true;
			else if(in2 == "parent" && ma[in3] == in1)
				ans = true;
			else if(in2 == "sibling" && ma[in1] == ma[in3])
				ans = true;
			else if(in2 == "descendant")
				ans = des(in1, in3, ma);
			else if(in2 == "ancestor")
				ans = des(in3, in1, ma);
			cout << (ans ? "True" : "False") << endl;
		}
		cout << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1348
final_rank: 4

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int INF = 1<<28;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	while(cin >> n >> m, n|m) {
		cin.ignore();
		vector<string> v(1000);
		map<string, string> ma;
		REP(i, n) {
			string s;
			getline(cin, s);
			int cnt = 0;
			while(s[cnt] == ' ')
				cnt++;
			s = s.substr(cnt);
			if(!cnt)
				ma[s] = "";
			else
				ma[s] = v[cnt-1];
			v[cnt] = s;
		}
		REP(i, m) {
			string in1, in2, in3;
			cin >> in1;
			cin >> in2 >> in2 >> in2;
			cin >> in3 >> in3;
			in3.erase(in3.end() - 1);
			bool ans = false;
			if(in2 == "child" && ma[in1] == in3)
				ans = true;
			else if(in2 == "parent" && ma[in3] == in1)
				ans = true;
			else if(in2 == "sibling" && ma[in1] == ma[in3])
				ans = true;
			else if(in2 == "descendant") {
				string s = ma[in1];
				while(s != "") {
					if(s == in3)
						ans = true;
					s = ma[s];
				}
			}
			else if(in2 == "ancestor") {
				string s = ma[in3];
				while(s != "") {
					if(s == in1)
						ans = true;
					s = ma[s];
				}
			}
			if(ans)
				cout << "True" << endl;
			else
				cout << "False" << endl;
		}
		cout << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1344
final_rank: 3

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <functional>
using namespace std;
typedef unordered_map<string,string> mss;

unordered_map<string,function<bool(const string&,const string&,mss&)>>T={
	{"child",[](const string &x,const string &y,mss &parent){return parent[x]==y;}},
	{"parent",[](const string &x,const string &y,mss &parent){return parent[y]==x;}},
	{"sibling",[](const string &x,const string &y,mss &parent){return parent[x]==parent[y];}},
	{"descendant",[](const string &x,const string &y,mss &parent){
		string z=parent[x];
		for(;;){
			if(z==y)return true;
			if(parent[z]=="")return false;
			z=parent[z];
		}
	}},
	{"ancestor",[](const string &x,const string &y,mss &parent){
		string z=parent[y];
		for(;;){
			if(z==x)return true;
			if(parent[z]=="")return false;
			z=parent[z];
		}
	}},

};
int main(){
	int N,Q;
	string line;
	for(;cin>>N>>Q,N;){
		getline(cin,line);
		mss parent;
		vector<string>stack;
		getline(cin,line);
		parent[line]="";
		stack.push_back(line);
		for(int i=1;i<N;i++){
			getline(cin,line);
			int idx=0;
			for(;line[idx]==' ';idx++);
			string name=line.substr(idx);
			if(idx>stack.size())return 1;
			for(;idx<stack.size();stack.pop_back());
			parent[name]=stack[stack.size()-1];
			stack.push_back(name);
		}
		for(int i=0;i<Q;i++){
			getline(cin,line);
			istringstream ss(line);
			string x,z,y;
			ss>>x>>y>>y>>z>>y>>y;
			y=y.substr(0,y.size()-1);
			cout<<(T[z](x,y,parent)?"True":"False")<<endl;
		}
		cout<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1340
final_rank: 2

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define MAX_N 1010

struct P{
  int p,l;
};

P p[MAX_N];
string s[MAX_N];
bool used[MAX_N];
int N;

void init(int N){
  for(int i = 0 ; i < N ; i++){
    p[i].p = i;
  }
}

bool find(int x,int target){
  if(p[x].p == target){
    return true;
  }
  if(p[x].p == x){ return false; }
  return find(p[x].p,target);
}

void make(int n,int parent,int root,int depth){
  set<int> children;
  if(used[n]){ return; }
  used[n] = true;
  p[n].p = parent; p[n].l = depth;
  for(int i = n+1 ; i < N ; i++){
    int len = s[i].size();
    if(depth+1 >= len){ continue; }
    if(s[i][depth] != '.'){ break; }
    if(s[i][depth] == '.' && s[i][depth+1] != '.'){
      children.insert(i);
      make(i,n,root,depth+1);
    }
  }
}

int main(){
  int M;
  while(cin >> N >> M, N){
    map<string,int> mp;
    int cur = 0;
    init(N); cin.ignore();
    for(int i = 0 ; i < N ; i++){
      getline(cin,s[i]);
      for(int j = 0 ; j < (int)s[i].size() ; j++){
        if(s[i][j] != ' '){
          mp[s[i].substr(j)] = cur++;
          break;
        }
        s[i][j] = '.';
      }
      used[i] = false;
    }
    for(int i = 0 ; i < N ; i++){
      if(used[i]){ continue; }
      if(s[i][0] != '.'){
        make(i,i,i,0);
      }
    }
    string ord,a,b,c;
    for(int i = 0 ; i < M ; i++){
      getline(cin,ord);
      stringstream ss(ord);
      ss >> a;
      ss >> ord; ss >> ord;
      ss >> c;
      ss >> ord;
      ss >> b;
      b.resize(b.size()-1);
      bool ans = false;
      if(c == "parent"){
        if(p[mp[b]].p == mp[a]){
          ans = true;
        }
      }else if(c == "child"){
        if(p[mp[a]].p == mp[b]){
          ans = true;
        }
      }else if(c == "ancestor"){
        if(p[mp[a]].l < p[mp[b]].l && find(mp[b],mp[a])){
          ans = true;
        }
      }else if(c == "descendant"){
        if(p[mp[a]].l > p[mp[b]].l && find(mp[a],mp[b])){
          ans = true;
        }
      }else{
        if(p[mp[a]].l == p[mp[b]].l && p[mp[a]].p == p[mp[b]].p){
          ans = true;
        }
      }
      cout << (ans ? "True" : "False") << endl;
    }
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1440
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;

struct node{
  string name;
  int parent;
  vector<int> child;

  node(string s=""):name(s){ parent = -1; child.clear(); }

  void set_par(int a){ parent = a; }
  void add_child(int a){ child.push_back(a); }
};

vector<node> tree;
map<string,int> id;

inline bool is_par(string my, string your){
  int par = tree[id[my]].parent;
  if(par<0)return false;
  return tree[par].name == your;
}

inline bool is_child(string my, string your){
  for(int c : tree[ id[my] ].child){
    if(tree[c].name == your)return true;
  }
  return false;
}

inline bool is_sib(string my, string your){
  int par = tree[id[my]].parent;
  if(par<0)return false;
  return is_child(tree[par].name, your);
}

inline bool is_anc(string my, string your){
  if(my == your)return true;
  int par = tree[id[my]].parent;
  if(par<0)return false;
  return is_anc(tree[par].name, your);
}

inline bool is_des(string my, string your){
  if(my == your)return true;
  for(int c : tree[ id[my] ].child){
    if(is_des(tree[c].name,your))return true;
  }
  return false;  
}

int main(){
  int n,m;
  while(cin >> n >> m, n){
    stack< pair<int,int> > s;
    id.clear();
    tree.resize(n);

    cin.ignore();
    for(int i=0;i<n;i++){
      string name;
      getline(cin,name);
      //cout << name << endl;
      int indent = 0;
      while(name[indent]==' ')indent++;
      name = name.substr(indent);
      tree[i] = node(name);
      id[name] = i;

      while(s.size() && s.top().second+1 != indent)s.pop();
      if(s.size()){
	tree[i].set_par( s.top().first );
	tree[s.top().first].add_child(i);
      }
      s.push( make_pair(i,indent) );
    }

    /*
    for(int i=0;i<n;i++){
      cout << tree[i].name << endl;
      cout << tree[i].parent << endl;
      for(int c : tree[i].child)cout << c << " "; cout << endl;
    }
    */

    for(int i=0;i<m;i++){
      string my,your,query,dummy;
      cin >> your >> dummy >> dummy >> query >> dummy >> my;
      my = my.substr(0,my.size()-1);

      //cout << my << " " << your << " " << query << endl;

      bool f = false;
      if(query == "child")f = is_child(my,your);
      else if(query == "parent")f = is_par(my,your);
      else if(query == "sibling")f = is_sib(my,your);
      else if(query == "ancestor")f = is_anc(my,your);
      else f = is_des(my,your);

      if(f)cout << "True" << endl;
      else cout << "False" << endl;
    }
    cout << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1420
final_rank: 9

code_snippet: #include <algorithm>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef long long Long;
#define whole(xs) xs.begin(), xs.end()

struct Q {
    string Relation, Name1, Name2;
    Q(const string& Relation, const string& Name1, const string& Name2) 
        : Relation(Relation), Name1(Name1), Name2(Name2) {}
};
ostream& operator<<(ostream& os, const Q& q) {
    os << "(" << q.Relation << ", " << q.Name1 << ", " << q.Name2 << ")";
    return os;
}

int N, M;
vector<int> Parent;
vector<Q> Queries;
map<string, int> Id;
vector<int> Prev;

int CountPrefixSpaces(const string& s) {
    int i;
    for (i = 0; i < s.size() && s[i] == ' '; i++) ;
    return i;
}

bool input() {
    string l;
    getline(cin, l);
    istringstream is; is.str(l);
    is >> N >> M;
    is.clear();
    if (N == 0 && M == 0) return false;
    Parent.resize(N, -1);
    Queries.clear();
    Id.clear();
    Prev.resize(N, -1);
    for (int i = 0; i < N; i++) {
        getline(cin, l);
        is.str(l);
        string Name;
        is >> Name;
        is.clear();
        Id[Name] = i;
        int C = CountPrefixSpaces(l);
        Prev[C] = i;
        if (C != 0) {
            Parent[i] = Prev[C - 1];
        }
    }
    for (int i = 0; i < M; i++) {
        getline(cin, l);
        is.str(l);
        string Name1, Relation, Name2;
        string _;
        is >> Name1;
        for (int k = 0; k < 2; k++) is >> _;
        is >> Relation;
        is >> _;
        is >> Name2;
        Name2 = Name2.substr(0, Name2.size() - 1);
        is.clear();
        Queries.push_back(Q(Relation, Name1, Name2));
    }
    return true;
}

void solve() {
    for (int i = 0; i < M; i++) {
        string& Relation = Queries[i].Relation,
                Name1 = Queries[i].Name1,
                Name2 = Queries[i].Name2;
        int x = Id[Name1], y = Id[Name2];
        if (Relation == "child") {
            cout << (y == Parent[x] ? "True" : "False") << endl;
        } else if (Relation == "parent") {
            cout << (x == Parent[y] ? "True" : "False") << endl;
        } else if (Relation == "sibling") {
            cout << (Parent[x] == Parent[y] ? "True" : "False") << endl;
        } else if (Relation == "descendant") {
            while (x >= 0) {
                if (y == x) {
                    cout << "True" << endl;
                    goto fin1;
                }
                x = Parent[x];
            }
            cout << "False" << endl;
            fin1:;
        } else if (Relation == "ancestor") {
            while (y >= 0) {
                if (y == x) {
                    cout << "True" << endl;
                    goto fin2;
                }
                y = Parent[y];
            }
            cout << "False" << endl;
            fin2:;
        } else {
            assert(0);
        }
    }
    cout << endl;
}

int main() {
    while (input()) solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1528
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int par[1010];
int last[100];
map<string, int> name;

bool check_ancestor(int a, int d){
	if (a < 0 || d < 0) return false;
	if (a == par[d]) return true;
	return check_ancestor(a, par[d]);
}

int main(){
	int n, q;
	while (cin >> n >> q, n|q){
		name.clear();
		MEMSET(last, -1);
		MEMSET(par, -1);
		cin.ignore();
		rep(i, n){
			string s;
			getline(cin, s);
			int cnt = count(ALL(s), ' ');
			last[cnt] = i;
			name[s.substr(cnt)] = i;
			if (!cnt) continue;
			par[i] = last[cnt - 1];
		}
		while (q--){
			string a, b, c, d, e, f;
			cin >> a >> b >> c >> d >> e >> f;
			//cout << f.substr(0, f.size() - 1) << endl;
			int x = name[a], y = name[f.substr(0, f.size() - 1)];
			bool ok;
			switch (d[0]){
			case 'c':
				ok = par[x] == y;
				break;
			case 'a':
				ok = check_ancestor(x, y);
				break;
			case 's':
				ok = par[x] == par[y];
				break;
			case 'p':
				ok = x == par[y];
				break;
			case 'd':
				ok = check_ancestor(y, x);
			}
			cout << (ok ? "True" : "False") << endl;
		}
		cout << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1320
final_rank: 1

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>

using namespace std;

typedef pair<int,string> P;

// family tree
struct Node{
  vector<Node*> child;
  Node* parent;
  string name;
  Node():name(""){}
  Node(vector<Node*> c, Node* p, string s):child(c),parent(p),name(s){}

  void init(){
    for(int i = 0; i < (int)child.size(); i++)
      child[i]->init();
    child.clear();
  }

  bool isSibling(const string& A, const string& B){
    return find(A)->parent==find(B)->parent;
  }

  bool isChild(const string& A){
    for(int i = 0; i < (int)child.size(); i++)
      if(child[i]->name == A) return true;
    return false;
  }

  bool isDecendant(const string& A){
    for(int i = 0; i < (int)child.size(); i++)
      if(child[i]->name == A) return true;
    
    bool res = false;
    for(int i = 0; i < (int)child.size(); i++)
      res = res||child[i]->isDecendant(A);
    return res;
  }

  Node* find(const string& name){
    for(int i = 0; i < (int)child.size(); i++)
      if(child[i]->name == name) return child[i];

    for(int i = 0; i < (int)child.size(); i++){
      Node* tmp = child[i]->find(name);
      if(tmp != NULL) return tmp;
    }
    return NULL;
  }
};

int N,M;
Node* top = new Node(vector<Node*>(), top, "");

P devide(const string& s){
  int blank = 0;
  for(int i = 0; i < (int)s.length(); i++) if(s[i] == ' ') blank++;
  return P(blank,s.substr(blank));
}

void input(){
  cin.ignore();
  
  Node* now = top;
  int depth = -1;

  for(int i = 0; i < N; i++){
    string s;
    getline(cin,s);
    P p = devide(s);
    
    if(p.first == depth){
      Node* nex = new Node(vector<Node*>(), now->parent, p.second);
      ((now->parent)->child).push_back(nex);
      now = nex;
    }

    if(p.first < depth){
      // go up
      int diff = depth-p.first+1;
      for(int i = 0; i < diff; i++) now = now->parent;
      Node* nex = new Node(vector<Node*>(), now, p.second);
      now->child.push_back(nex);
      now = nex;
      depth = p.first;
    }
    
    if(p.first > depth){
      // go down
      Node* nex = new Node(vector<Node*>(), now, p.second);
      now->child.push_back(nex);
      now = nex;
      depth = p.first;
    }
  }
}

void solve(){

  for(int i = 0; i < M; i++){
    string X,Y,Q,trash;
    cin >> X >> trash >> trash >> Q >> trash >> Y;
    bool ans = false;
    Y = Y.substr(0,Y.length()-1);

    if(Q[0]=='c') ans = (top->find(Y))->isChild(X);
    if(Q[0]=='p') ans = (top->find(X))->isChild(Y);
    if(Q[0]=='s') ans = top->isSibling(X,Y);
    if(Q[0]=='d') ans = (top->find(Y))->isDecendant(X);
    if(Q[0]=='a') ans = (top->find(X))->isDecendant(Y);
    
    cout << (ans?"True":"False") << endl;
  }
  cout << endl;
}

int main(){
  
  while(cin >> N >> M && N+M){
    top->init();
    input();
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1360
final_rank: 5

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

typedef pair<int, string> P;

map<string, P> fam;

vector<string> s;
vector<string> name;
int N, Q;

void output(){
  string op;
  for(int i = 0 ; i < Q ; i++){
    string relation, X, Y;
    for(int j = 0 ; j < 6 ; j++){
      cin >> op;
      if(j == 0) X = op;
      if(j == 3) relation = op;
      if(j == 5) Y = op;
    }
    Y.erase(Y.end()-1);
    
    if(relation == "parent"){
      relation = "child";
      swap(X, Y);
    }
    if(relation == "descendant"){
      relation = "ancestor";
      swap(X, Y);
    }
    
    bool ans = false;
    
    if(relation == "child"){
      ans = fam[X].second == Y;
    }
    else if(relation == "ancestor"){
      while(fam[Y].first != 0){
	if(fam[Y].second == X){
	  ans = true;
	  break;
	}
	Y = fam[Y].second;
      }
    }
    else if(relation == "sibling"){
      int gen = fam[Y].first;
      string par = fam[Y].second;
      for(map<string, P>::iterator it = fam.begin() ; it != fam.end() ; it++){
	if(it->first == X){
	  if(it->second.first == gen && it->second.second == par){
	    ans = true;
	    break;
	  }
	}
      }
    }
    if(ans) cout << "True" << endl;
    else cout << "False" << endl;
  }
}

int main(){
  while(cin >> N >> Q, N|Q){
    fam.clear();
    s.clear();
    name.clear();
    cin.ignore();
    string tmp;
    for(int i = 0 ; i < N ; i++){
      getline(cin, tmp);
      s.push_back(tmp);
    }
    
    name = s;
    
    for(int i = 0 ; i < N ; i++){
      while(name[i][0] == ' ') name[i].erase(name[i].begin());
    }
    
    for(int i = 0 ; i < N ; i++){
      int gen = 0;
      for(int j = 0 ; j < (int)s[i].size() ; j++){
	if(s[i][j] != ' ') break;
	gen++;
      }
      for(int j = i ; j >= 0 ; j--){
	if(s[j][gen-1] != ' '){
	  fam[ name[i] ] = P(gen, name[j]);
	  break;
	}
      }
    }
    
    output();
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1536
final_rank: 15

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

typedef pair<int, string> P;

map<string, P> fam;

vector<string> s;
vector<string> name;
int N, Q;

void output(){
  string op;
  for(int i = 0 ; i < Q ; i++){
    string relation, X, Y;
    for(int j = 0 ; j < 6 ; j++){
      cin >> op;
      if(j == 0) X = op;
      if(j == 3) relation = op;
      if(j == 5) Y = op;
    }
    Y.erase(Y.end()-1);
    
    if(relation == "parent"){
      relation = "child";
      swap(X, Y);
    }
    if(relation == "descendant"){
      relation = "ancestor";
      swap(X, Y);
    }
    
    bool ans = false;
    
    if(relation == "child"){
      ans = fam[X].second == Y;
    }
    else if(relation == "ancestor"){
      while(fam[Y].first != 0){
	if(fam[Y].second == X){
	  ans = true;
	  break;
	}
	Y = fam[Y].second;
      }
    }
    else if(relation == "sibling"){
      int gen = fam[Y].first;
      string par = fam[Y].second;
      for(map<string, P>::iterator it = fam.begin() ; it != fam.end() ; it++){
	if(it->first == X){
	  if(it->second.first == gen && it->second.second == par){
	    ans = true;
	    break;
	  }
	}
      }
    }
    if(ans) cout << "True" << endl;
    else cout << "False" << endl;
  }
}

int main(){
  while(cin >> N >> Q, N|Q){
    fam.clear();
    s.clear();
    name.clear();
    cin.ignore();
    string tmp;
    for(int i = 0 ; i < N ; i++){
      getline(cin, tmp);
      s.push_back(tmp);
    }
    
    name = s;
    
    for(int i = 0 ; i < N ; i++){
      while(name[i][0] == ' ') name[i].erase(name[i].begin());
      /*
      for(int j = 0 ; j < (int)name[i].size() ; j++){
	if(name[i][j] == ' ') name[i].erase(name[i].begin()+j);
      }
      */
    }
    
    for(int i = 0 ; i < N ; i++){
      int gen = 0;
      for(int j = 0 ; j < (int)s[i].size() ; j++){
	if(s[i][j] != ' ') break;
	gen++;
      }
      for(int j = i ; j >= 0 ; j--){
	if(s[j][gen-1] != ' '){
	  fam[ name[i] ] = P(gen, name[j]);
	  break;
	}
      }
    }
    
    output();
    cout << endl;
    /*
    for(map<string, P>::iterator it = fam.begin() ; it != fam.end() ; it++){
      cout << it->first << ' ' << it->second.first << ' ' << it->second.second << endl;
    }
    */
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1536
final_rank: 15

code_snippet: #include<map>
#include<sstream>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cassert>
#define F first
#define S second
#define pb push_back
#define all(n) n.begin(),n.end()
using namespace std;
typedef pair<string,int> SI;
 
int oldest,youngest;
 
void split(vector<SI>& vec,string line)
{
  int space_cnt = 0;
  for(int i=0;line[i] == ' ';i++,space_cnt++){}
 
  vec.push_back(SI(line.substr(space_cnt,line.size()-space_cnt),space_cnt));
  oldest = min(oldest,space_cnt);
  youngest = max(youngest,space_cnt);
}
 
void ParseAndSolve(vector<SI>& vec,map<string,int>& index_tree,string line)
{
  vector<string> info;
  stringstream ss(line);
  string mid;
  vector<string> ope; 
  
 
  for(int i=0;i<ss.str().size();i++)
    {
      if((ss >> mid).fail())
    goto skip;
       
      ope.push_back(mid);
       
    }
 
 skip:
 
  info.push_back(ope[0]);
  for(int i=1;i<ope.size()-1;i++)
    {
      if(ope[i] == "child" || ope[i] == "parent" || ope[i] == "sibling" || ope[i] == "descendant" || ope[i] == "ancestor")
    {
      info.push_back(ope[i]);
      break;
    }
    }
  info.push_back(ope[ope.size()-1].substr(0,ope[ope.size()-1].size()));
 
 
  assert(info.size() == 3);
 
  string X,Y;
  X = info[0],Y = info[2];
 
  if(info[1][0] == 'c')
    {
      int Yindex = index_tree[Y];     
      int Xindex = index_tree[X];
 
      if(vec[Xindex].S <= vec[Yindex].S || Yindex+1 >= vec.size() || vec[Yindex+1].S != vec[Xindex].S)
    {
      cout << "False" << endl;
      return;
    }
 
    
      int parent_level = vec[Yindex].S;
 
      for(int i=Yindex+1;parent_level < vec[i].S && i < vec.size();i++)
    {
 
      if(vec[i].F == X)
        {
          cout << "True" << endl;
          return;
        }
    }
 
      cout << "False" << endl;
      return;
    }
  else if(info[1][0] == 'p')
    {
 
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];
 
      if(vec[Xindex].S >= vec[Yindex].S || Xindex+1 >= vec.size() || vec[Xindex+1].S != vec[Yindex].S )
    {
      cout << "False" << endl;
      return;
    }
 
 
      int parent_level = vec[Xindex].S;
 
      for(int i=Xindex+1;parent_level < vec[i].S && i < vec.size();i++ )
    {
      if(vec[i].F == Y)
        {
          cout << "True" << endl;
          return;
        }
    }
 
      cout << "False" << endl;
      return;
 
 
    }
  else if(info[1][0] == 's')
    {
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];
 
      if(vec[Xindex].S != vec[Yindex].S || min(Xindex,Yindex)+1 >= vec.size())
    {
      cout << "False" << endl;
      return;
    }
 
      int sibling_level = vec[Xindex].S;
 
      int Maxindex,Minindex;
      string Maxname,Minname;
 
      if(Xindex > Yindex)
    {
      Maxindex = Xindex,Minindex = Yindex;
      Maxname = X,Minname = Y;
    }
      else
    {
      Maxindex = Yindex,Minindex = Xindex;
      Maxname = Y,Minname = X;
    }
 
      for(int i=Minindex+1;i<vec.size() && sibling_level <= vec[i].S;i++)
    {
      if(Maxname == vec[i].F)
        {
          cout << "True" << endl;
          return;
        }
    }
      cout << "False" << endl;
      return;
 
    }
  else if(info[1][0] == 'd')
    {
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];
 
      if(Yindex+1 >= vec.size() || vec[Xindex].S <= vec[Yindex].S)
    {
      cout << "False" << endl;
      return;
    }
 
      for(int i=Yindex+1;i<vec.size() && vec[i].S > vec[Yindex].S;i++)
    {
      if(vec[i].F == vec[Xindex].F)
        {
          cout << "True" << endl;
          return;
        }
    }
 
      cout << "False" << endl;
      return;
    }
  else if(info[1][0] == 'a')
    {
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];
      if(Xindex+1 >= vec.size()  || vec[Xindex].S >= vec[Yindex].S)
    {
      cout << "False" << endl;
      return;
    }
 
      for(int i=Xindex+1;i < vec.size() && vec[i].S > vec[Xindex].S;i++)
    {
      if(vec[i].F == vec[Yindex].F)
        {
          cout << "True" << endl;
          return;
        }
    }
 
      cout << "False" << endl;
      return;
    }
  else
    assert(false);
   
 
}
 
int main()
{
  int n,m;
  while(true)
    {
      cin >> n >> m;
    
      if(!(n+m))
    break;
      oldest = (1<<28);
      youngest = -1;
      cin.ignore();
      string line;
      vector<SI> vec;
      for(int i=0;i<n;i++)
    {
      getline(cin,line);
     
      split(vec,line);
    }
 
      map<string,int> index_tree;
      for(int i=0;i<n;i++)
    index_tree[vec[i].F] = i;
 
      for(int i=0;i<m;i++)
    {
      getline(cin,line);
      
      ParseAndSolve(vec,index_tree,line.substr(0,line.size()-1));
      
    }
 
      cout << endl;
    }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1360
final_rank: 5

code_snippet: #include<map>
#include<sstream>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cassert>
#define F first
#define S second
#define pb push_back
#define all(n) n.begin(),n.end()
using namespace std;
typedef pair<string,int> SI;

int oldest,youngest;

void split(vector<SI>& vec,string line)
{
  int space_cnt = 0;
  for(int i=0;line[i] == ' ';i++,space_cnt++){}

  vec.push_back(SI(line.substr(space_cnt,line.size()-space_cnt),space_cnt));
  oldest = min(oldest,space_cnt);
  youngest = max(youngest,space_cnt);
}

void ParseAndSolve(vector<SI>& vec,map<string,int>& index_tree,string line)
{
  vector<string> info;
  stringstream ss(line);
  string mid;
  vector<string> ope; 
 

  for(int i=0;i<ss.str().size();i++)
    {
      if((ss >> mid).fail())
	goto skip;
      
      ope.push_back(mid);
      
    }

 skip:

  info.push_back(ope[0]);
  for(int i=1;i<ope.size()-1;i++)
    {
      if(ope[i] == "child" || ope[i] == "parent" || ope[i] == "sibling" || ope[i] == "descendant" || ope[i] == "ancestor")
	{
	  info.push_back(ope[i]);
	  break;
	}
    }
  info.push_back(ope[ope.size()-1].substr(0,ope[ope.size()-1].size()));


  assert(info.size() == 3);

  string X,Y;
  X = info[0],Y = info[2];

  if(info[1][0] == 'c')
    {
      int Yindex = index_tree[Y];     
      int Xindex = index_tree[X];

      if(vec[Xindex].S <= vec[Yindex].S || Yindex+1 >= vec.size() || vec[Yindex+1].S != vec[Xindex].S)
	{
	  cout << "False" << endl;
	  return;
	}

   
      int parent_level = vec[Yindex].S;

      for(int i=Yindex+1;parent_level < vec[i].S && i < vec.size();i++)
	{

	  if(vec[i].F == X)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}

      cout << "False" << endl;
      return;
    }
  else if(info[1][0] == 'p')
    {

      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];

      if(vec[Xindex].S >= vec[Yindex].S || Xindex+1 >= vec.size() || vec[Xindex+1].S != vec[Yindex].S )
	{
	  cout << "False" << endl;
	  return;
	}


      int parent_level = vec[Xindex].S;

      for(int i=Xindex+1;parent_level < vec[i].S && i < vec.size();i++ )
	{
	  if(vec[i].F == Y)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}

      cout << "False" << endl;
      return;


    }
  else if(info[1][0] == 's')
    {
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];

      if(vec[Xindex].S != vec[Yindex].S || min(Xindex,Yindex)+1 >= vec.size())
	{
	  cout << "False" << endl;
	  return;
	}

      int sibling_level = vec[Xindex].S;

      int Maxindex,Minindex;
      string Maxname,Minname;

      if(Xindex > Yindex)
	{
	  Maxindex = Xindex,Minindex = Yindex;
	  Maxname = X,Minname = Y;
	}
      else 
	{
	  Maxindex = Yindex,Minindex = Xindex;
	  Maxname = Y,Minname = X;
	}

      for(int i=Minindex+1;i<vec.size() && sibling_level <= vec[i].S;i++)
	{
	  if(Maxname == vec[i].F)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}
      cout << "False" << endl;
      return;

    }
  else if(info[1][0] == 'd')
    {
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];

      if(Yindex+1 >= vec.size() || vec[Xindex].S <= vec[Yindex].S)
	{
	  cout << "False" << endl;
	  return;
	}

      for(int i=Yindex+1;i<vec.size() && vec[i].S > vec[Yindex].S;i++)
	{
	  if(vec[i].F == vec[Xindex].F)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}

      cout << "False" << endl;
      return;
    }
  else if(info[1][0] == 'a')
    {
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];
      if(Xindex+1 >= vec.size()  || vec[Xindex].S >= vec[Yindex].S)
	{
	  cout << "False" << endl;
	  return;
	}

      for(int i=Xindex+1;i < vec.size() && vec[i].S > vec[Xindex].S;i++)
	{
	  if(vec[i].F == vec[Yindex].F)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}

      cout << "False" << endl;
      return;
    }
  else 
    assert(false);
  

}

int main()
{
  int n,m;
  while(true)
    {
      cin >> n >> m;
   
      if(!(n+m))
	break;
      oldest = (1<<28);
      youngest = -1;
      cin.ignore();
      string line;
      vector<SI> vec;
      for(int i=0;i<n;i++)
	{
	  getline(cin,line);
	
	  split(vec,line);
	}

      map<string,int> index_tree;
      for(int i=0;i<n;i++)
	index_tree[vec[i].F] = i;

      for(int i=0;i<m;i++)
	{
	  getline(cin,line);
	 
	  ParseAndSolve(vec,index_tree,line.substr(0,line.size()-1));
	 
	}

      cout << endl;
    }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1360
final_rank: 5

code_snippet: #include<map>
#include<sstream>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cassert>
#define F first
#define S second
#define pb push_back
#define all(n) n.begin(),n.end()
using namespace std;
typedef pair<string,int> SI;

int oldest,youngest;

void split(vector<SI>& vec,string line)
{
  int space_cnt = 0;
  for(int i=0;line[i] == ' ';i++,space_cnt++){}

  vec.push_back(SI(line.substr(space_cnt,line.size()-space_cnt),space_cnt));
  oldest = min(oldest,space_cnt);
  youngest = max(youngest,space_cnt);
}

void ParseAndSolve(vector<SI>& vec,map<string,int>& index_tree,map<int,vector<string> >& space_tree,map<string,int>& level_tree,string line)
{
  vector<string> info;
  stringstream ss(line);
  string mid;
  vector<string> ope; 
 

  for(int i=0;i<ss.str().size();i++)
    {
      if((ss >> mid).fail())
	goto skip;
      
      ope.push_back(mid);
      
    }

 skip:

  info.push_back(ope[0]);
  for(int i=1;i<ope.size()-1;i++)
    {
      if(ope[i] == "child" || ope[i] == "parent" || ope[i] == "sibling" || ope[i] == "descendant" || ope[i] == "ancestor")
	{
	  info.push_back(ope[i]);
	  break;
	}
    }
  info.push_back(ope[ope.size()-1].substr(0,ope[ope.size()-1].size()));


  assert(info.size() == 3);

  string X,Y;
  X = info[0],Y = info[2];

  if(info[1][0] == 'c')
    {
      int Yindex = index_tree[Y];     
      int Xindex = index_tree[X];

      if(level_tree[X] <= level_tree[Y] || Yindex+1 >= vec.size() || vec[Yindex+1].S != vec[Xindex].S)
	{
	  cout << "False" << endl;
	  return;
	}

   
      int parent_level = vec[Yindex].S;

      for(int i=Yindex+1;parent_level < vec[i].S && i < vec.size();i++)
	{

	  if(vec[i].F == X)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}

      cout << "False" << endl;
      return;
    }
  else if(info[1][0] == 'p')
    {

      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];

      if(level_tree[X] >= level_tree[Y] || Xindex+1 >= vec.size() || vec[Xindex+1].S != vec[Yindex].S )
	{
	  cout << "False" << endl;
	  return;
	}


      int parent_level = vec[Xindex].S;

      for(int i=Xindex+1;parent_level < vec[i].S && i < vec.size();i++ )
	{
	  if(vec[i].F == Y)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}

      cout << "False" << endl;
      return;


    }
  else if(info[1][0] == 's')
    {
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];

      if(vec[Xindex].S != vec[Yindex].S || min(Xindex,Yindex)+1 >= vec.size())
	{
	  cout << "False" << endl;
	  return;
	}

      int sibling_level = vec[Xindex].S;

      int Maxindex,Minindex;
      string Maxname,Minname;

      if(Xindex > Yindex)
	{
	  Maxindex = Xindex,Minindex = Yindex;
	  Maxname = X,Minname = Y;
	}
      else 
	{
	  Maxindex = Yindex,Minindex = Xindex;
	  Maxname = Y,Minname = X;
	}

      for(int i=Minindex+1;i<vec.size() && sibling_level <= vec[i].S;i++)
	{
	  if(Maxname == vec[i].F)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}
      cout << "False" << endl;
      return;

    }
  else if(info[1][0] == 'd')
    {
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];

      if(Yindex+1 >= vec.size() || vec[Xindex].S <= vec[Yindex].S)
	{
	  cout << "False" << endl;
	  return;
	}

      for(int i=Yindex+1;i<vec.size() && vec[i].S > vec[Yindex].S;i++)
	{
	  if(vec[i].F == vec[Xindex].F)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}

      cout << "False" << endl;
      return;
    }
  else if(info[1][0] == 'a')
    {
      int Xindex = index_tree[X];
      int Yindex = index_tree[Y];
      if(Xindex+1 >= vec.size()  || vec[Xindex].S >= vec[Yindex].S)
	{
	  cout << "False" << endl;
	  return;
	}

      for(int i=Xindex+1;i < vec.size() && vec[i].S > vec[Xindex].S;i++)
	{
	  if(vec[i].F == vec[Yindex].F)
	    {
	      cout << "True" << endl;
	      return;
	    }
	}

      cout << "False" << endl;
      return;
    }
  else 
    assert(false);
  

}

int main()
{
  int n,m;
  while(true)
    {
      cin >> n >> m;
      //cerr << n << " " << m << endl;
      if(!(n+m))
	break;
      oldest = (1<<28);
      youngest = -1;
      cin.ignore();
      string line;
      vector<SI> vec;
      for(int i=0;i<n;i++)
	{
	  getline(cin,line);
	  //cerr << line << endl;
	  split(vec,line);
	}

      map<int,vector<string> > space_tree;
      map<string,int> level_tree;
      map<string,int> index_tree;
      for(int i=0;i<n;i++)
	{
	  index_tree[vec[i].F] = i;
	  level_tree[vec[i].F] = vec[i].S;
	  space_tree[vec[i].S].pb(vec[i].F);
	}

      for(int i=0;i<m;i++)
	{
	  getline(cin,line);
	  //cerr << line << endl;
	  ParseAndSolve(vec,index_tree,space_tree,level_tree,line.substr(0,line.size()-1));
	 
	}

      cout << endl;
    }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1428
final_rank: 10

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;


struct Person {
  string name;
  Person *parent;
};

const int MAXN = 1005;
int N, M;
map<string, Person*> conv;
Person root, person[MAXN];

void init() {
  conv.clear();
  root.name = "root";
  root.parent = NULL;
}

bool child(Person *a, Person *b) { // a is child of b
  return a->parent == b;
}

bool sibling(Person *a, Person *b) {
  return a->parent == b->parent;
}

bool descendant(Person *a, Person *b) { // a is descendant of b
  while(a != NULL) {
    if(a->parent == b) return true;
    a = a->parent;
  }
  return false;
}

int main() {
  while(cin >> N >> M && (N|M)) {
    cin.ignore();
    init();
    Person *cp = &root;
    int depth = -1;
    for(int i = 0; i < N; ++i) {
      string line;
      getline(cin, line);
      int sp = 0;
      for(int j = 0; j < line.size() && line[j] == ' '; ++j, ++sp);

      Person &c = person[i];
      c.name = line.substr(sp);
      conv[c.name] = &person[i];

      if(sp > depth) {
        c.parent = cp;
      } else {
        for(int j = 0; j < depth-sp; ++j) {
          cp = cp->parent;
        }
        c.parent = cp->parent;
      }
      cp = &c;
      depth = sp;
    }

    while(M--) {
      string x, y, type;
      string dumy;
      cin >> x >> dumy >> dumy >> type >> dumy >> y;
      y = y.substr(0, y.size()-1);
      bool output = false;
      if(0) {
      } else if(type == "child") {
        output = child(conv[x], conv[y]);
      } else if(type == "parent") {
        output = child(conv[y], conv[x]);
      } else if(type == "sibling") {
        output = sibling(conv[x], conv[y]);
      } else if(type == "descendant") {
        output = descendant(conv[x], conv[y]);
      } else if(type == "ancestor") {
        output = descendant(conv[y], conv[x]);
      }
      cout << (output ? "True" : "False") << endl;
    }
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1344
final_rank: 14

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

struct person{
	vector<person> children;
	string name;
	person(string name):name(name){}
	int construct(vs &names,int indent,int index){
		FOR(i,index,names.size()){
			int next_indent=count(ALL(names[i]),' ');
			if(next_indent==indent+1){
				string next_name=names[i];
				next_name.erase(next_name.begin(),next_name.begin()+next_indent);
				children.push_back(person(next_name));
			}else if(next_indent>indent+1){
				i=children[children.size()-1].construct(names,indent+1,i)-1;
			}else{
				return i;
			}
		}
		return names.size();
	}
	bool is_child(string a,string b){
		if(name==b){
			REP(i,children.size()){
				if(children[i].name==a){
					return true;
				}
			}
			return false;
		}
		REP(i,children.size()){
			if(children[i].is_child(a,b)){
				return true;
			}
		}
		return false;
	}
	bool is_descendant(string a,string b,bool flag=false){
		if(flag&&name==a){
			return true;
		}
		if(name==b||flag){
			REP(i,children.size()){
				if(children[i].is_descendant(a,b,true)){
					return true;
				}
			}
			return false;
		}
		REP(i,children.size()){
			if(children[i].is_descendant(a,b)){
				return true;
			}
		}
		return false;
	}
	bool is_parent(string a,string b){
		if(name==a){
			REP(i,children.size()){
				if(children[i].name==b){
					return true;
				}
			}
			return false;
		}
		REP(i,children.size()){
			if(children[i].is_parent(a,b)){
				return true;
			}
		}
		return false;
	}
	bool is_ancestor(string a,string b,bool flag=false){
		if(flag&&name==b){
			return true;
		}
		if(name==a||flag){
			REP(i,children.size()){
				if(children[i].is_ancestor(a,b,true)){
					return true;
				}
			}
			return false;
		}
		REP(i,children.size()){
			if(children[i].is_ancestor(a,b)){
				return true;
			}
		}
		return false;
	}
	bool is_sibling(string a,string b){
		bool flag=false;
		REP(i,children.size()){
			if(children[i].name==a||children[i].name==b){
				if(flag){
					return true;
				}
				flag=true;
			}
		}
		REP(i,children.size()){
			if(children[i].is_sibling(a,b)){
				return true;
			}
		}
		return false;
	}
};

int main(){
	int n,m;
	while(cin>>n>>m,n&&m){
		string name;
		getline(cin,name);
		getline(cin,name);
		person root=person(name);
		vs names;
		REP(i,n-1){
			getline(cin,name);
			names.push_back(name);
		}
		int index=0;
		root.construct(names,0,index);
		REP(i,m){
			string a,b,rel,trash;
			cin>>a>>trash>>trash>>rel>>trash>>b;
			bool ok;
			b.erase(b.end()-1,b.end());
			if(rel=="child"){
				ok=root.is_child(a,b);
			}else if(rel=="parent"){
				ok=root.is_parent(a,b);
			}else if(rel=="ancestor"){
				ok=root.is_ancestor(a,b);
			}else if(rel=="descendant"){
				ok=root.is_descendant(a,b);
			}else if(rel=="sibling"){
				ok=root.is_sibling(a,b);
			}else{
				assert(0);
			}
			cout<<(ok?"True":"False")<<endl;
		}
		cout<<endl;

	}
}
accuracy: 1
time_ms: 30
memory_kb: 1460
final_rank: 17

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, string>
const int INF = INT_MAX / 2;
const int MAX_M = 1010;

P cntSpace(string str){
    int cnt = 0;
    while(str[cnt] == ' ') cnt++;
    return P(cnt, str.substr(cnt));
}

bool isParent(int x, int y, vector<int> parent){
    if(parent[y] == x) return true;
    else return false;
}

bool checkQ(string str, map<string, int> list_i, vector<int> parent){
    stringstream ss(str);
    vector<string> vec;
    string tmp;
    while(ss >>tmp) vec.push_back(tmp);

    int x = list_i[vec[0]];
    int y = list_i[vec[5].erase(vec[5].size() - 1)];

    if(vec[3] == "child") return isParent(y, x, parent);
    if(vec[3] == "parent") return isParent(x, y, parent);
    if(vec[3] == "sibling") return parent[x] == parent[y];
    if(vec[3] == "descendant"){
        int now_x = x;
        while(parent[now_x] != -1){
            if(isParent(y, now_x, parent) == true) return true;
            now_x = parent[now_x];
        }
        return false;
    }
    if(vec[3] == "ancestor"){
        int now_y = y;
        while(parent[now_y] != -1){
            if(isParent(x, now_y, parent) == true) return true;
            now_y = parent[now_y];
        }
        return false;
    }
}



int main(){
    int m, n;
    while(cin >>m >>n && (n && m)){
        string name_line;
        getline(cin, name_line);

        vector<int> parent(m);
        int current_parent_i[MAX_M];
        map<string, int> list_i;

        parent[0] = -1;

        REP(i, m){
            getline(cin, name_line);

            P rank_and_name = cntSpace(name_line);
            current_parent_i[rank_and_name.first + 1] = i;
            list_i[rank_and_name.second] = i;

            if(i != 0){
                int now_parent = current_parent_i[rank_and_name.first];
                parent[i] = now_parent;
            }
        }

        REP(i, n){
            string q;
            getline(cin, q);
            bool ans = checkQ(q, list_i, parent);
            if(ans) cout <<"True" <<endl;
            else cout <<"False" <<endl;
        }
        cout <<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 1432
final_rank: 19

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, string>
const int INF = INT_MAX / 2;
const int MAX_M = 1010;

P cntSpace(string str){
    int cnt = 0;
    while(str[cnt] == ' ') cnt++;
    return P(cnt, str.substr(cnt));
}

bool isParent(int x, int y, vector<int> parent){
    if(parent[y] == x) return true;
    else return false;
}

bool checkQ(string str, map<string, int> list_i, vector<int> parent){
    stringstream ss(str);
    vector<string> vec;
    string tmp;
    while(ss >>tmp) vec.push_back(tmp);

    int x = list_i[vec[0]];
    int y = list_i[vec[5].erase(vec[5].size() - 1)];

    if(vec[3] == "child") return isParent(y, x, parent);
    if(vec[3] == "parent") return isParent(x, y, parent);
    if(vec[3] == "sibling") return parent[x] == parent[y];
    if(vec[3] == "descendant"){
        int now_x = x;
        while(parent[now_x] != -1){
            if(isParent(y, now_x, parent) == true) return true;
            now_x = parent[now_x];
        }
        return false;
    }
    if(vec[3] == "ancestor"){
        int now_y = y;
        while(parent[now_y] != -1){
            if(isParent(x, now_y, parent) == true) return true;
            now_y = parent[now_y];
        }
        return false;
    }
}



int main(){
    while(true){
        string str_m_n;
        getline(cin, str_m_n);
        stringstream ss(str_m_n);
        int m, n;
        ss >>m >>n;
        if(!m && !n) break;

        vector<int> parent(m);
        int current_parent_i[MAX_M];
        map<string, int> list_i;

        parent[0] = -1;

        REP(i, m){
            string name_line;
            getline(cin, name_line);
            stringstream ss(name_line);
            P rank_and_name = cntSpace(name_line);
            current_parent_i[rank_and_name.first + 1] = i;
            list_i[rank_and_name.second] = i;
            if(i != 0){
                int now_parent = current_parent_i[rank_and_name.first];
                parent[i] = now_parent;
            }
        }

        REP(i, n){
            string q;
            getline(cin, q);
            bool ans = checkQ(q, list_i, parent);
            if(ans) cout <<"True" <<endl;
            else cout <<"False" <<endl;
        }
        cout <<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 1432
final_rank: 19