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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

vs teams;
unordered_map<string, int> mp;
set<string> usedt, usedm;
vector<vs> v;

void solve(string s) {
  int n = mp[s];
  REP(i, v[n].size()) {
    string tmp = v[n][i];
    if (mp.find(tmp) == mp.end())
      usedm.insert(tmp);
    else if (usedt.find(tmp) == usedt.end()) {
      usedt.insert(tmp);
      solve(tmp);
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    v.clear();
    v.resize(n);
    teams.clear();
    usedt.clear();
    usedm.clear();
    mp.clear();
    REP(i, n) {
      string s;
      cin >> s;
      int p = s.find(':');
      string team = s.substr(0, p);
      teams.push_back(team);
      mp[team] = i;
      s = s.substr(p + 1);
      while (s.find(',') != string::npos) {
        p = s.find(',');
        v[i].push_back(s.substr(0, p));
        s = s.substr(p + 1);
      }
      p = s.find('.');
      v[i].push_back(s.substr(0, p));
    }
    solve(teams[0]);
    cout << usedm.size() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  int n;
  while (cin >> n, n) {
    map<string, int> team;
    set<string> member;
    vector<vs> v(n);
    REP(i, n) {
      string s;
      cin >> s;
      int tmp = s.find(":");
      string t = s.substr(0, tmp);
      s = s.substr(tmp + 1);
      team[t] = team.size();
      bool f = true;
      while (f) {
        tmp = s.find(',');
        if (tmp != string::npos) {
          v[i].push_back(s.substr(0, tmp));
          s = s.substr(tmp + 1);
        } else {
          f = false;
          v[i].push_back(s.substr(0, s.size() - 1));
        }
      }
    }
    REP(i, v.size()) {
      REP(j, v[i].size()) {
        if (team.find(v[i][j]) == team.end())
          member.insert(v[i][j]);
      }
    }
    vector<bool> usedt(team.size());
    set<string> ans;
    queue<int> que;
    que.push(0);
    while (que.size()) {
      int t = que.front();
      que.pop();
      if (usedt[t])
        continue;
      usedt[t] = true;
      REP(i, v[t].size()) {
        if (member.find(v[t][i]) != member.end())
          ans.insert(v[t][i]);
        else {
          que.push(team[v[t][i]]);
        }
      }
    }
    cout << ans.size() << endl;
  }
  return 0;
}