Problem p00884 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-7;
const double PI = acos(-1.0);

set<string> dfs(map<string, vs> &m, string &member,
                map<string, set<string>> &dp) {
  if (EXIST(dp, member)) {
    return dp[member];
  }
  if (!EXIST(m, member)) {
    set<string> ret;
    ret.insert(member);
    return ret;
  }
  set<string> ret;
  REP(i, m[member].size()) {
    set<string> s = dfs(m, m[member][i], dp);
    ret.insert(ALL(s));
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    map<string, vs> m;
    string first_group;
    REP(i, n) {
      string s;
      cin >> s;
      string group;
      int pos = 0;
      while (s[pos] != ':') {
        group += s[pos++];
      }
      if (i == 0) {
        first_group = group;
      }
      pos++;
      m[group] = vs();
      do {
        string member;
        while (s[pos] != ',' && s[pos] != '.') {
          member += s[pos++];
        }
        m[group].push_back(member);
      } while (s[pos++] != '.');
    }
    map<string, set<string>> dp;
    set<string> ans;
    REP(i, m[first_group].size()) {
      set<string> r = dfs(m, m[first_group][i], dp);
      ans.insert(ALL(r));
    }
    cout << ans.size() << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-7;
const double PI = acos(-1.0);

set<string> dfs(map<string, vs> &m, string &member,
                map<string, set<string>> &dp) {
  if (EXIST(dp, member)) {
    return dp[member];
  }
  if (!EXIST(m, member)) {
    set<string> ret;
    ret.insert(member);
    return ret;
  }
  set<string> ret;
  REP(i, m[member].size()) {
    set<string> s = dfs(m, m[member][i], dp);
    ret.insert(ALL(s));
  }
  return dp[member] = ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    map<string, vs> m;
    string first_group;
    REP(i, n) {
      string s;
      cin >> s;
      string group;
      int pos = 0;
      while (s[pos] != ':') {
        group += s[pos++];
      }
      if (i == 0) {
        first_group = group;
      }
      pos++;
      m[group] = vs();
      do {
        string member;
        while (s[pos] != ',' && s[pos] != '.') {
          member += s[pos++];
        }
        m[group].push_back(member);
      } while (s[pos++] != '.');
    }
    map<string, set<string>> dp;
    set<string> ans;
    REP(i, m[first_group].size()) {
      set<string> r = dfs(m, m[first_group][i], dp);
      ans.insert(ALL(r));
    }
    cout << ans.size() << endl;
  }
}