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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
template <class T> inline bool rd(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T> inline void pt(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    pt(x / 10);
  putchar(x % 10 + '0');
}
using namespace std;
const int N = 3000 + 105;

int n, m;
vector<string> myset[105];
set<string> ans;
map<string, int> mp;
string s;
bool vis[105];
void dfs(int x) {
  vis[x] = 1;
  for (int i = 0; i < myset[x].size(); i++) {
    if (mp.count(myset[x][i])) {
      if (!vis[mp[myset[x][i]]])
        dfs(mp[myset[x][i]]);
    } else
      ans.insert(myset[x][i]);
  }
}
string str;
int main() {
  while (cin >> n, n) {
    memset(vis, 0, sizeof vis);
    mp.clear();
    for (int j = 1; j <= n; j++) {
      cin >> str;
      str[str.length() - 1] = ',';
      int pos = str.find(':');
      string temp = str.substr(0, pos);
      mp[temp] = j;
      while (1) {
        str = str.substr(pos + 1);
        pos = str.find(',');
        if (pos == -1)
          break;
        temp = str.substr(0, pos);
        myset[j].push_back(temp);
      }
      sort(myset[j].begin(), myset[j].end());
      myset[j].erase(unique(myset[j].begin(), myset[j].end()), myset[j].end());
    }
    ans.clear();
    dfs(1);
    pt((int)ans.size());
    puts("");
    for (int j = 1; j <= n; j++)
      myset[j].clear();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
template <class T> inline bool rd(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T> inline void pt(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    pt(x / 10);
  putchar(x % 10 + '0');
}
using namespace std;
typedef long long ll;
const int N = 1000 + 50;

int n, m;
vector<string> myset[105];
map<string, int> mp;
string s;
char st[N];

void dfs(int x) {
  for (int i = 0; i < myset[x].size(); i++) {
    if (mp.count(myset[x][i]))
      dfs(mp[myset[x][i]]);
    else
      myset[0].push_back(myset[x][i]);
  }
}
int main() {
  while (~scanf("%d", &n), n) {
    mp.clear();
    for (int j = 1; j <= n; j++) {
      scanf("%s", st);
      int len = strlen(st);
      st[len - 1] = ',';
      int i = 0;
      s = "";
      while (st[i] != ':') {
        s += st[i];
        i++;
      }
      mp[s] = j;
      s = "";
      myset[j].clear();
      for (i++; i < len; i++) {
        if (st[i] == ',') {
          myset[j].push_back(s);
          s = "";
        } else
          s += st[i];
      }

      sort(myset[j].begin(), myset[j].end());
      myset[j].erase(unique(myset[j].begin(), myset[j].end()), myset[j].end());
    }
    myset[0].clear();
    dfs(1);
    sort(myset[0].begin(), myset[0].end());
    myset[0].erase(unique(myset[0].begin(), myset[0].end()), myset[0].end());
    printf("%d\\n", myset[0].size());
  }
  return 0;
}