Problem p00797 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

typedef pair<string, int> PSI;
typedef stack<PSI> Stack;

const int size = 1001;
int n, m;
int D[size];
string N[size];
int T[size][size]; // type
map<string, int> ID;
int IDC;

void addName(string name) {
  if (ID.find(name) != ID.end()) {
    return;
  }
  ID[name] = IDC++;
}

int getIndex(string name) {
  if (ID.find(name) == ID.end()) {
    return -1;
  }
  return ID[name];
}

int getDepth(string line) {
  int depth = 0;
  for (string ::iterator it_i = line.begin(); it_i != line.end(); ++it_i) {
    if (*it_i == ' ') {
      depth += 1;
    }
  }
  return depth;
}

string getName(string line) {
  string name;
  istringstream is(line);
  is >> name;
  return name;
}

string getNameFirst(string line) {
  string name;
  istringstream is(line);
  is >> name;
  return name;
}

string getNameSecond(string line) {
  string name;
  istringstream is(line);
  for (int i = 0; i < 5; i++) {
    is >> name;
  }
  is >> name;
  return name.substr(0, name.size() - 1);
}

int getFindRelation(string line) {
  string relation;
  istringstream is(line);
  for (int i = 0; i < 3; i++) {
    is >> relation;
  }
  is >> relation;
  if (relation == "child") {
    return 1;
  } else if (relation == "parent") {
    return 2;
  } else if (relation == "sibling") {
    return 3;
  } else if (relation == "ancestor") {
    return 4;
  } else if (relation == "descendant") {
    return 5;
  }
  return 0;
}

bool check(int a, int b, int c) { return (T[a][b] & 1 << c) != 0; }

int main() {
  while (cin >> n >> m) {
    cin.clear();
    cin.ignore();
    if (n == 0 && m == 0)
      break;

    // init
    ID.clear();
    IDC = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        T[i][j] = 0;
      }
    }

    // input
    for (int i = 0; i < n; i++) {
      string line;
      getline(cin, line);
      D[i] = getDepth(line);
      N[i] = getName(line);
      addName(N[i]);
    }

    // child
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (D[i] == D[j] - 1) {
          T[i][j] |= 1 << 2;
          T[j][i] |= 1 << 1;
        } else if (D[i] >= D[j]) {
          break;
        }
      }
    }

    // sibling
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (D[i] == D[j]) {
          T[i][j] |= 1 << 3;
          T[j][i] |= 1 << 3;
        } else if (D[i] > D[j]) {
          break;
        }
      }
    }

    // descendant
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (D[i] < D[j]) {
          T[i][j] |= 1 << 4;
          T[j][i] |= 1 << 5;
        } else {
          break;
        }
      }
    }

    // questions
    for (int i = 0; i < m; i++) {
      string line;
      getline(cin, line);
      string A = getNameFirst(line);
      string B = getNameSecond(line);
      int a = getIndex(A);
      int b = getIndex(B);
      int c = getFindRelation(line);
      bool flag = true;
      if (A == B) {
        flag = false;
      }
      if (a == -1 || b == -1) {
        flag = false;
      }
      cout << (flag && check(a, b, c) ? "True" : "False") << endl;
    }
    cout << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

typedef pair<string, int> PSI;
typedef stack<PSI> Stack;

const int size = 1001;
int n, m;
int D[size];
string N[size];

int getDepth(string line) {
  int depth = 0;
  for (string ::iterator it_i = line.begin(); it_i != line.end(); ++it_i) {
    if (*it_i == ' ') {
      depth += 1;
    }
  }
  return depth;
}

string getName(string line) {
  string name;
  istringstream is(line);
  is >> name;
  return name;
}

int main() {
  while (cin >> n >> m) {
    cin.clear();
    cin.ignore();
    if (n == 0 && m == 0)
      break;

    // input
    for (int i = 0; i < n; i++) {
      string line;
      getline(cin, line);
      D[i] = getDepth(line);
      N[i] = getName(line);
    }

    set<string> S;
    for (int i = 0; i < n; i++) {
      int L = i + 1;
      while (L < n) {
        if (D[i] == D[L])
          break;
        L++;
      }
      for (int j = i + 1; j < L; j++) {
        ostringstream os;
        if (D[i] + 1 == D[j]) {
          os << N[i] << " is the parent of " << N[j] << ".";
          S.insert(os.str());
          os.str("");
          os << N[j] << " is a child of " << N[i] << ".";
          S.insert(os.str());
        } else if (D[i] == D[j]) {
          os << N[i] << " is a sibling of " << N[j] << ".";
          S.insert(os.str());
          os.str("");
          os << N[j] << " is a sibling of " << N[i] << ".";
          S.insert(os.str());
        } else if (D[i] - 1 == D[j]) {
          os << N[i] << " is a child of " << N[j] << ".";
          S.insert(os.str());
          os.str("");
          os << N[j] << " is a parent of " << N[i] << ".";
          S.insert(os.str());
        } else if (D[i] + 1 < D[j]) {
          os << N[i] << " is an ancestor of " << N[j] << ".";
          S.insert(os.str());
          os.str("");
          os << N[j] << " is a descendant of " << N[i] << ".";
          S.insert(os.str());
        } else if (D[i] + 1 > D[j]) {
          os << N[i] << " is a descendant of " << N[j] << ".";
          S.insert(os.str());
          os.str("");
          os << N[j] << " is an ancestor of " << N[i] << ".";
          S.insert(os.str());
        }
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        ostringstream os;
        if (D[i] == D[j]) {
          os << N[i] << " is a sibling of " << N[j] << ".";
          S.insert(os.str());
          os.str("");
          os << N[j] << " is a sibling of " << N[i] << ".";
          S.insert(os.str());
        } else if (D[i] + 1 < D[j]) {
          os << N[i] << " is an ancestor of " << N[j] << ".";
          S.insert(os.str());
          os.str("");
          os << N[j] << " is a descendant of " << N[i] << ".";
          S.insert(os.str());
        } else if (D[i] + 1 > D[j]) {
          os << N[i] << " is a descendant of " << N[j] << ".";
          S.insert(os.str());
          os.str("");
          os << N[j] << " is an ancestor of " << N[i] << ".";
          S.insert(os.str());
        }
      }
    }

    // questions
    for (int i = 0; i < m; i++) {
      string line;
      getline(cin, line);
      cout << (S.count(line) ? "True" : "False") << endl;
    }
    cout << endl;
  }
  return 0;
}
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;