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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

unordered_map<string, bool> visited;

void Merge(const string &grp, map<string, vector<string>> &m,
           unordered_set<string> &members) {
  if (visited[grp])
    return;
  visited[grp] = true;

  const auto &names = m[grp];
  for (auto &name : names) {
    if (m.count(name) == 1)
      Merge(name, m, members);
    else
      members.insert(name);
  }
}

void Solve() {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    map<string, vector<string>> m;
    string first;
    REP(i, 0, n) {
      string s;
      cin >> s;
      int idx = 0;
      string grp;
      REP(j, 0, s.size()) {
        if (s[j] == ':') {
          grp = s.substr(0, j);
          if (i == 0)
            first = grp;
          idx = j + 1;
        }
        if (s[j] == ',' || s[j] == '.') {
          string sub = s.substr(idx, j - idx);
          m[grp].push_back(sub);
          idx = j + 1;
        }
      }
    }

    unordered_set<string> members;
    visited.clear();
    Merge(first, m, members);
    cout << members.size() << endl;
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<string> group_names(n);
    vector<unordered_set<string>> member_names(n);
    REP(i, 0, n) {
      string s;
      cin >> s;
      int idx = 0;
      REP(j, 0, s.size()) {
        if (s[j] == ':') {
          group_names[i] = s.substr(0, j);
          idx = j + 1;
        }
        if (s[j] == ',' || s[j] == '.') {
          string sub = s.substr(idx, j - idx);
          member_names[i].insert(sub);
          idx = j + 1;
        }
      }
    }

    bool flag = false;
    unordered_set<string> done;
    for (auto it = member_names[0].begin(); it != member_names[0].end() || flag;
         ++it) {
      if (flag) {
        it = member_names[0].begin();
        flag = false;
      }
      if (done.count(*it) == 1)
        continue;
      REP(i, 1, n) if (*it == group_names[i]) {
        vector<string> v;
        for (auto it_ = member_names[i].begin(); it_ != member_names[i].end();
             ++it_) {
          v.push_back(*it_);
          bool flag_ = true;
          REP(j, 1, n) if (*it_ == group_names[j]) {
            flag_ = false;
            break;
          }
        }
        done.insert(*it);
        member_names[0].erase(*it);
        REP(j, 0, v.size()) member_names[0].insert(v[j]);
        flag = true;
      }
    }
    cout << member_names[0].size() << endl;
    for (auto it = member_names[0].begin(); it != member_names[0].end(); ++it)
      dump(*it);
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}