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

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

map<string, set<string>> ma, dp;

set<string> dfs(string s) {
  if (dp.find(s) != dp.end())
    return dp[s];
  // cout << s << endl;
  set<string> ret;
  for (string i : ma[s]) {
    if (ma.find(i) != ma.end()) {
      set<string> tmp = dfs(i);
      for (string j : tmp) {
        ret.insert(j);
      }
    } else {
      ret.insert(i);
    }
  }

  return dp[s] = ret;
}

signed main(void) {
  while (true) {
    int n;
    string a[105];
    cin >> n;
    if (!n)
      break;
    REP(i, n) cin >> a[i];

    // map????????????
    ma.erase(ALL(ma));
    dp.erase(ALL(dp));

    string s, key;
    REP(i, n) {
      string t = "";
      REP(j, a[i].size()) {
        if (a[i][j] == ':') {
          if (i == 0)
            s = t;
          key = t;
          t = "";
        } else if (a[i][j] == ',') {
          ma[key].insert(t);
          t = "";
        } else if (a[i][j] == '.') {
          ma[key].insert(t);
          t = "";
          break;
        } else {
          t += a[i][j];
        }
      }
    }

    /*for(auto itr = ma.begin(); itr != ma.end(); ++itr) {
            cout << itr->first << " ";
            for(string j: itr->second) {
                    cout << j << " ";
            }
            cout << endl;
    }*/

    set<string> ans = dfs(s);
    cout << ans.size() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

map<string, vector<string>> ma, dp;

vector<string> dfs(string s) {
  if (dp.find(s) != dp.end())
    return dp[s];
  // cout << s << endl;
  vector<string> ret;
  for (string i : ma[s]) {
    if (ma.find(i) != ma.end()) {
      vector<string> tmp = dfs(i);
      for (string j : tmp) {
        ret.PB(j);
      }
    } else {
      ret.PB(i);
    }
  }

  return dp[s] = ret;
}

signed main(void) {
  while (true) {
    int n;
    string a[105];
    cin >> n;
    if (!n)
      break;
    REP(i, n) cin >> a[i];

    // map????????????
    ma.erase(ALL(ma));

    string s, key;
    REP(i, n) {
      string t = "";
      REP(j, a[i].size()) {
        if (a[i][j] == ':') {
          if (i == 0)
            s = t;
          key = t;
          t = "";
        } else if (a[i][j] == ',') {
          ma[key].PB(t);
          t = "";
        } else if (a[i][j] == '.') {
          ma[key].PB(t);
          t = "";
          break;
        } else {
          t += a[i][j];
        }
      }
    }

    /*for(auto itr = ma.begin(); itr != ma.end(); ++itr) {
            cout << itr->first << " ";
            for(string j: itr->second) {
                    cout << j << " ";
            }
            cout << endl;
    }*/

    vector<string> ans = dfs(s);
    set<string> ans1;
    for (string i : ans)
      ans1.insert(i);
    cout << ans1.size() << endl;
  }

  return 0;
}