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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define rep(i, n) for (int i = 0; i < n; i++)
#define SORT(x) sort((x).begin(), (x).end())
#define clr(a, b) memset((a), (b), sizeof(a))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<P> Vp;
typedef priority_queue<P, vector<P>, greater<P>> PQ;

int n;

void solve() {
  set<string> g[100], ans;
  vector<string> s;
  set<string>::iterator ite;
  rep(i, n) {
    string str;
    cin >> str;
    int st;
    rep(j, str.size()) {
      if (str[j] == ':') {
        s.pb(str.substr(0, j));
        st = j;
        break;
      }
    }
    for (int j = st + 1; j < str.size(); j++) {
      if (str[j] == ',') {
        g[i].insert(str.substr(st + 1, j - st - 1));
        st = j;
      }
      if (str[j] == '.') {
        g[i].insert(str.substr(st + 1, j - st - 1));
        break;
      }
    }
  }
  queue<string> que;
  set<string> used;
  for (ite = g[0].begin(); ite != g[0].end(); ite++) {
    que.push(*ite);
    used.insert(*ite);
  }
  while (true) {
    if (que.size() == 0)
      break;
    string str = que.front();
    que.pop();
    bool check = false;
    for (int i = 1; i < n; i++) {
      if (str == s[i]) {
        check = true;
        for (ite = g[i].begin(); ite != g[i].end(); ite++) {
          if (used.find(*ite) == used.end()) {
            que.push(*ite);
            used.insert(*ite);
          }
        }
        break;
      }
    }
    if (!check)
      ans.insert(str);
  }
  cout << ans.size() << endl;
  /*
  for(ite = ans.begin(); ite != ans.end(); ite++){
          cout << *ite << " ";
  }
  cout << endl;
  */
}

int main() {
  while (scanf("%d", &n), n)
    solve();
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define rep(i, n) for (int i = 0; i < n; i++)
#define SORT(x) sort((x).begin(), (x).end())
#define clr(a, b) memset((a), (b), sizeof(a))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<P> Vp;
typedef priority_queue<P, vector<P>, greater<P>> PQ;

int n;

void solve() {
  set<string> g[100], ans;
  vector<string> s;
  set<string>::iterator ite, it;
  rep(i, n) {
    string str;
    cin >> str;
    int st;
    rep(j, str.size()) {
      if (str[j] == ':') {
        s.pb(str.substr(0, j));
        st = j;
      }
    }
    for (int j = st + 1; j < str.size(); j++) {
      if (str[j] == ',') {
        g[i].insert(str.substr(st + 1, j - st - 1));
        st = j;
      }
      if (str[j] == '.') {
        g[i].insert(str.substr(st + 1, j - st - 1));
        break;
      }
    }
  }
  queue<string> que;
  for (ite = g[0].begin(); ite != g[0].end(); ite++)
    que.push(*ite);
  while (true) {
    if (que.size() == 0)
      break;
    string str = que.front();
    que.pop();
    bool check = false;
    for (int i = 1; i < n; i++) {
      if (str == s[i]) {
        check = true;
        for (it = g[i].begin(); it != g[i].end(); it++)
          que.push(*it);
        break;
      }
    }
    if (!check)
      ans.insert(str);
  }
  cout << ans.size() << endl;
  /*
  for(ite = ans.begin(); ite != ans.end(); ite++){
          cout << *ite << " ";
  }
  cout << endl;
  */
}

int main() {
  while (scanf("%d", &n), n)
    solve();
}