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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(n) rep(i, n)
#define all(n) n.begin(), n.end()
#define foreach(it, c)                                                         \\
  for (typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
typedef pair<string, int> pp;
const int MAXN = 1000;
map<string, int> m;
int en[MAXN + 10] = {};
int vec[MAXN + 10][MAXN + 10] = {};
int used[MAXN + 10] = {};

int solve(int n) {
  int ans = 0;
  if (used[n])
    return 0;
  used[n] = 1;
  if (en[n] == 0)
    return 1;

  for (int i = 0; i < en[n]; i++)
    ans += solve(vec[n][i]);

  return ans;
}

int main() {
  int n;
  while (cin >> n && n) {
    m.clear();
    memset(en, 0, sizeof(en));
    memset(vec, 0, sizeof(vec));
    memset(used, 0, sizeof(used));

    string s, oya, ko;
    int nnum = 0;
    REP(n) {
      cin >> s;
      int f = 0, e = 0;
      for (f = 0; f < s.size(); f++)
        if (s[f] == ':') {
          oya = s.substr(0, f);
          f += 1;
          break;
        }
      if (m.find(oya) == m.end()) {
        m.insert(pp(oya, nnum));
        nnum++;
      }
      // cout << oya << " ";
      while (f < s.size())
        for (e = f + 1; e < s.size(); e++)
          if (s[e] == ',' || s[e] == '.') {
            ko = s.substr(f, e - f);
            if (m.find(ko) == m.end()) {
              m.insert(pp(ko, nnum));
              nnum++;
              vec[m[oya]][en[m[oya]]] = m[ko];
              en[m[oya]]++;
            } else {
              vec[m[oya]][en[m[oya]]] = m[ko];
              en[m[oya]]++;
            }
            f = e + 1;
            // cout << ko   << " ";
          }
      // cout << endl;
    }
    cout << solve(0) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(n) rep(i, n)
#define all(n) n.begin(), n.end()
#define foreach(it, c)                                                         \\
  for (typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
typedef pair<string, int> pp;
const int MAXN = 100;
map<string, int> m;
int en[MAXN + 10] = {};
int vec[MAXN + 10][MAXN + 10] = {};
int used[MAXN + 10] = {};
int solve(int n) {
  int ans = 0;
  if (used[n])
    return 0;
  used[n] = 1;
  if (en[n] == 0)
    return 1;

  for (int i = 0; i < en[n]; i++)
    ans += solve(vec[n][i]);

  return ans;
}

int main() {
  int n;
  while (cin >> n && n) {
    m.clear();
    memset(en, 0, sizeof(en));
    memset(vec, 0, sizeof(vec));
    memset(used, 0, sizeof(used));

    string s, oya, ko;
    int nnum = 0;
    REP(n) {
      cin >> s;
      int f = 0, e = 0;
      for (f = 0; f < s.size(); f++)
        if (s[f] == ':') {
          oya = s.substr(0, f);
          f += 1;
          break;
        }
      if (m.find(oya) == m.end()) {
        m.insert(pp(oya, nnum));
        nnum++;
      }
      // cout << oya << " ";
      while (f < s.size())
        for (e = f + 1; e < s.size(); e++)
          if (s[e] == ',' || s[e] == '.') {
            ko = s.substr(f, e - f);
            if (m.find(ko) == m.end()) {
              m.insert(pp(ko, nnum));
              nnum++;
              vec[m[oya]][en[m[oya]]] = m[ko];
              en[m[oya]]++;
            } else {
              vec[m[oya]][en[m[oya]]] = m[ko];
              en[m[oya]]++;
            }
            f = e + 1;
            // cout << ko   << " ";
          }
      // cout << endl;
    }
    cout << solve(0) << endl;
  }

  return 0;
}