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

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

map<string, set<string>> mp;
vector<string> tt;

int solve(string str) {
  // cout << str << endl;
  auto itr = mp.find(str);
  list<set<string>::iterator> ers;
  list<string> psh;
  for (auto s = itr->second.begin(); s != itr->second.end(); s++) {
    auto itr2 = mp.find(*s);
    if (itr2 != mp.end()) {
      solve(itr2->first);
      for (auto t = itr2->second.begin(); t != itr2->second.end(); t++) {
        psh.push_back(*t);
      }
      ers.push_back(s);
    }
  }
  for (auto s : ers) {
    itr->second.erase(s);
  }
  for (auto s : psh) {
    itr->second.insert(s);
  }
  return itr->second.size();
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;
    tt = vector<string>(n);
    mp = map<string, set<string>>();
    string s, t;
    int j, pre;
    for (int i = 0; i < n; i++) {
      cin >> s;
      j = 0;
      for (; j < s.length(); j++) {
        if (s[j] == ':') {
          t = s.substr(0, j);
          j++;
          pre = j;
          break;
        }
      }
      // cout << t << endl;
      mp[t] = set<string>();
      tt[i] = t;
      for (; j < s.length(); j++) {
        if (s[j] == ',' || s[j] == '.') {
          mp[t].insert(s.substr(pre, j - pre));
          // cout << s.substr(pre,j-pre) << endl;
          j++;
          pre = j;
        }
      }
    }
    cout << solve(tt[0]) << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

map<string, set<string>> mp;
vector<string> tt;

int solve(string str) {
  // cout << str << endl;
  auto itr = mp.find(str);
  list<set<string>::iterator> ers;
  list<string> psh;
  for (auto s = itr->second.begin(); s != itr->second.end(); s++) {
    auto itr2 = mp.find(*s);
    if (itr2 != mp.end()) {
      solve(itr2->first);
      for (auto t = itr2->second.begin(); t != itr2->second.end(); t++) {
        psh.push_back(*t);
      }
      ers.push_back(s);
    }
  }
  for (auto s : ers) {
    itr->second.erase(s);
  }
  for (auto s : psh) {
    itr->second.insert(s);
  }
  return itr->second.size();
}

int main() {
  while (1) {
    int n;
    cin >> n;
    tt = vector<string>(n);
    mp = map<string, set<string>>();
    string s, t;
    int j, pre;
    for (int i = 0; i < n; i++) {
      cin >> s;
      j = 0;
      for (; j < s.length(); j++) {
        if (s[j] == ':') {
          t = s.substr(0, j);
          j++;
          pre = j;
          break;
        }
      }
      // cout << t << endl;
      mp[t] = set<string>();
      tt[i] = t;
      for (; j < s.length(); j++) {
        if (s[j] == ',' || s[j] == '.') {
          mp[t].insert(s.substr(pre, j - pre));
          // cout << s.substr(pre,j-pre) << endl;
          j++;
          pre = j;
        }
      }
    }
    cout << solve(tt[0]) << endl;
  }
  return 0;
}