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

#include <bits/stdc++.h>

#define DEBUG(C) cerr << #C << " = " << C << endl

using namespace std;
vector<string> split(const string &S, const char sep) {
  vector<string> res;
  string buf = "";
  for (int i = 0; i < S.length(); i++) {
    if (S[i] == sep) {
      res.emplace_back(buf);
      buf.clear();
    } else {
      buf += S[i];
    }
  }
  if (!buf.empty()) {
    res.emplace_back(buf);
  }
  return res;
}

pair<string, vector<string>> parse(const string &S) {
  pair<string, vector<string>> res;
  int now = 0;
  string buf = "";
  for (; now < S.length(); now++) {
    if (S[now] == ':') {
      now++;
      break;
    } else {
      buf += S[now];
    }
  }
  res.first = buf;
  buf.clear();
  res.second = split(S.substr(now), ',');
  if (!res.second.empty()) {
    res.second.back().pop_back();
  }
  sort(res.second.begin(), res.second.end());
  res.second.erase(unique(res.second.begin(), res.second.end()),
                   res.second.end());
  return res;
}

vector<pair<string, vector<string>>> input;
map<int, set<string>> se;

void dfs(int idx) {
  if (se.count(idx)) {
    return;
  }
  for (int i = 0; i < input[idx].second.size(); i++) {
    bool f = true;
    for (int j = 0; j < input.size(); j++) {
      if (input[idx].second[i] == input[j].first) {
        // DEBUG(idx);DEBUG(i);DEBUG(j);
        // DEBUG(input[idx].second[i]);
        f = false;
        dfs(j);
        se[idx].insert(se[j].begin(), se[j].end());
      }
    }
    if (f) {
      se[idx].insert(input[idx].second[i]);
    }
  }
  return;
}

void solve(int n) {
  se.clear();
  input.clear();
  input.resize(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    input[i] = parse(s);
    // if (89 <= i && i <= 91) DEBUG(s);
  }
  dfs(0);
  cout << se[0].size() << endl;
}

int main() {
  while (true) {
    int n;
    scanf("%d%d", &n);
    if (n == 0) {
      break;
    }
    solve(n);
  }
  return 0;
}

Error: 0
S.substr(now) = alice,bob,design,eve.
S.substr(now) = carol,alice.
alice bob carol eve 
S.substr(now) = another.
S.substr(now) = yetanother.
S.substr(now) = dave.
dave 
S.substr(now) = alice,bob,bestfriends,carol,fran,badcompany.
S.substr(now) = eve,alice.
S.substr(now) = dave,carol.
alice bob carol dave eve fran 
S.substr(now) = b,c,d,e.
S.substr(now) = c,d,e,f.
S.substr(now) = d,e,f,g.
S.substr(now) = e,f,g,h.
S.substr(now) = f,g,h,i.
f g h i 
S.substr(now) = bb.
S.substr(now) = dd,ee.
S.substr(now) = gg.
S.substr(now) = cc.
dd ee 


Fixed submission:

#include <bits/stdc++.h>

#define DEBUG(C) cerr << #C << " = " << C << endl

using namespace std;
vector<string> split(const string &S, const char sep) {
  vector<string> res;
  string buf = "";
  for (int i = 0; i < S.length(); i++) {
    if (S[i] == sep) {
      res.emplace_back(buf);
      buf.clear();
    } else {
      buf += S[i];
    }
  }
  if (!buf.empty()) {
    res.emplace_back(buf);
  }
  return res;
}

pair<string, vector<string>> parse(const string &S) {
  pair<string, vector<string>> res;
  int now = 0;
  string buf = "";
  for (; now < S.length(); now++) {
    if (S[now] == ':') {
      now++;
      break;
    } else {
      buf += S[now];
    }
  }
  res.first = buf;
  buf.clear();
  DEBUG(S.substr(now));
  res.second = split(S.substr(now), ',');
  if (!res.second.empty()) {
    res.second.back().pop_back();
  }
  return res;
}

vector<pair<string, vector<string>>> input;
set<string> se;

void dfs(const string &name) {
  // DEBUG(name);
  bool f = true;
  for (int i = 0; i < input.size(); i++) {
    if (input[i].first == name) {
      f = false;
      for (int j = 0; j < input[i].second.size(); j++) {
        dfs(input[i].second[j]);
      }
    }
  }
  if (f) {
    se.insert(name);
  }
  return;
}

void solve(int n) {
  se.clear();
  input.clear();
  input.resize(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    input[i] = parse(s);
  }
  for (int i = 0; i < input.front().second.size(); i++) {
    dfs(input.front().second[i]);
  }
  for (auto e : se)
    cerr << e << " ";
  cerr << endl;
  cout << se.size() << endl;
}

int main() {
  while (true) {
    int n;
    scanf("%d%d", &n);
    if (n == 0) {
      break;
    }
    solve(n);
  }
  return 0;
}