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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
vector<string> ans;
vector<string> m[150];
map<string, int> id;
int vis[1500];
int n;
char s[150000];
char tmp[1500];
void gao(int now) {
  vis[now] = 1;
  for (int i = 0; i < m[now].size(); i++) {
    string haha = m[now][i];
    // printf ("%d\\n",id[haha]);
    if (id.count(haha)) {
      if (vis[id[haha]])
        continue;
      else
        gao(id[haha]);
    } else {
      ans.push_back(haha);
    }
  }
}
int main() {
  while (scanf("%d", &n), n) {
    id.clear();
    ans.clear();
    for (int i = 0; i <= n; i++)
      m[i].clear();
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++) {
      scanf("%s", s + 1);
      int len = strlen(s + 1);
      int now = 0;
      for (int j = 1; j <= len; j++) {
        if (s[j] == ':') {
          tmp[now] = 0;
          now = 0;
          id[string(tmp)] = i;
          // printf ("%d",id[string(tmp)]);
          // cout<<string(tmp)<<endl;
          // cout<<string(tmp)<<endl;
        } else if (s[j] == ',' || s[j] == '.') {
          tmp[now] = 0;
          now = 0;
          m[i].push_back(string(tmp));

        } else {
          tmp[now++] = s[j];
        }
      }
    }
    gao(1);
    sort(ans.begin(), ans.end());
    ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
    printf("%d\\n", ans.size());
    // for(int i=0;i<ans.size();i++)
    // cout<<ans[i]<<endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000007
#define MP make_pair
const int maxn = 100010;
const double eps = 1e-6, pi = acos(-1.0);
using namespace std;
typedef pair<int, int> P;
typedef long long LL;
int n;
vector<string> v[110];
void get(vector<string> &w) {
  char c = getchar();
  while (c == ' ' || c == '\\n')
    c = getchar();
  char s[100];
  int cnt = 0;
  do {
    s[cnt++] = c;
    c = getchar();
  } while (c != ':');
  s[cnt] = '\\0';
  w.push_back(string(s));
  do {
    c = getchar();
    cnt = 0;
    do {
      s[cnt++] = c;
      c = getchar();
    } while (c != ',' && c != '.');
    s[cnt] = '\\0';
    w.push_back(string(s));
  } while (c != '.');
}
set<string> ans;
set<int> tmp;
map<string, int> group;
bool vis[110];
void solve(int g) {
  int j;
  for (int i = 1; i < v[g].size(); i++)
    if (group.find(v[g][i]) == group.end())
      ans.insert(v[g][i]);
    else {
      j = group[v[g][i]];
      if (!vis[j]) {
        tmp.insert(j);
        vis[j] = 1;
      }
    }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
  int i, j;
  while (~scanf("%d", &n) && n) {
    for (i = 0; i < 110; i++)
      v[i].clear();
    ans.clear();
    tmp.clear();
    group.clear();
    memset(vis, 0, sizeof(vis));
    for (i = 0; i < n; i++) {
      get(v[i]);
      group[v[i][0]] = i;
    }
    vis[0] = 1;
    tmp.insert(0);
    while (!tmp.empty()) {
      i = *(tmp.begin());
      solve(i);
      tmp.erase(i);
    }
    printf("%d\\n", (int)ans.size());
  }
  return 0;
}