Problem p00797 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
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;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
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>
#define Pint pair<int, int>
const int INF = INT_MAX / 2;
const int MAX_M = 2000;

string human[MAX_M][MAX_M];
map<string, Pint> human_map;

bool tmpCheck(int y, int x) {
  if (y >= MAX_M)
    return false;
  if (x >= MAX_M)
    return false;
  if (y < 0)
    return false;
  if (x < 0)
    return false;
  return true;
}

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

bool checkChild(string x, string y) {
  int now_y = human_map[y].first + 1, now_x = human_map[y].second + 1;
  bool ret = false;
  if (!tmpCheck(now_y, now_x))
    return false;
  while (human[now_y][now_x] != "") {
    if (human[now_y][now_x] == x)
      ret = true;
    now_y++;
    if (!tmpCheck(now_y, now_x))
      break;
  }
  return ret;
}

bool checkParent(string x, string y) {
  int now_y = human_map[y].first - 1, now_x = human_map[y].second - 1;
  if (!tmpCheck(now_y, now_x))
    return false;
  while (human[now_y][now_x] == "") {
    now_y++;
    if (!tmpCheck(now_y, now_x))
      break;
  }
  bool ret = (human[now_y][now_x] == x) ? true : false;
  return ret;
}

bool checkSibling(string x, string y) {
  int now_y = human_map[y].first, now_x = human_map[y].second;
  int m = 1;
  bool skip = false;
  if (!tmpCheck(now_y + m, now_x))
    skip = true;
  bool ret = false;
  while (human[now_y + m][now_x] != "" && !skip) {
    if (human[now_y + m][now_x] == x)
      ret = true;
    m++;
    if (!tmpCheck(now_y + m, now_x))
      break;
  }
  now_y = human_map[y].first;
  skip = false;
  m = 1;
  if (!tmpCheck(now_y - m, now_x))
    skip = true;
  while (human[now_y - m][now_x] != "" && !skip) {
    if (human[now_y - m][now_x] == x)
      ret = true;
    m++;
    if (!tmpCheck(now_y - m, now_x))
      break;
  }
  return ret;
}

bool checkDescendant(string x, string y) {
  bool ret = false;
  REP(i, human_map[x].first) {
    REP(j, human_map[x].second) {
      if (!tmpCheck(i, j))
        break;
      if (human[i][j] == y)
        ret = true;
    }
  }
  return ret;
}

bool checkAncestor(string x, string y) {
  bool ret = false;
  FOR(i, human_map[x].first + 1, MAX_M) {
    FOR(j, human_map[x].second + 1, MAX_M) {
      if (!tmpCheck(i, j))
        break;
      if (human[i][j] == y)
        ret = true;
    }
  }
  return ret;
}

bool checkQ(string str) {
  stringstream ss(str);
  vector<string> vec;
  string tmp;
  while (ss >> tmp)
    vec.push_back(tmp);

  string y = vec[5].erase(vec[5].size() - 1);
  if (vec[3] == "child")
    return checkChild(vec[0], y);
  if (vec[3] == "parent")
    return checkParent(vec[0], y);
  if (vec[3] == "sibling")
    return checkSibling(vec[0], y);
  if (vec[3] == "descendant")
    return checkDescendant(vec[0], y);
  if (vec[3] == "ancestor")
    return checkAncestor(vec[0], y);
}

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

    REP(i, MAX_M) REP(j, MAX_M) human[i][j] = "";
    human_map.clear();

    REP(i, m) {
      string str;
      getline(cin, str);
      P tmp = cntSpace(str);
      human[i][tmp.first] = tmp.second;
      human_map[tmp.second] = Pint(i, tmp.first);
    }

    REP(i, n) {
      string str;
      getline(cin, str);
      if (checkQ(str) == true)
        cout << "True" << endl;
      else
        cout << "False" << endl;
    }
    cout << endl;
  }
  return 0;
}