Problem p00884 (C++) — buggy submission, verdict: 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-10;
const double PI = acos(-1.0);

set<string> dfs(map<string, set<string>> &groups, string cur,
                map<string, set<string>> &dp) {
  if (EXIST(dp, cur)) {
    return dp[cur];
  }
  map<string, set<string>>::iterator it = groups.find(cur);
  if (it == groups.end()) {
    set<string> ret;
    ret.insert(cur);
    return ret;
  }
  set<string> &group = it->second;
  set<string> ret;
  for (set<string>::iterator it2 = group.begin(); it2 != group.end(); it2++) {
    set<string> s = dfs(groups, *it2, dp);
    if (s.size() > ret.size()) {
      swap(ret, s);
    }
    ret.insert(ALL(s));
  }
  return dp[cur] = ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    map<string, set<string>> groups;
    string first_group;
    REP(i, n) {
      string s;
      cin >> s;
      string tmp;
      string group;
      set<string> tmp_members;
      REP(j, s.size()) {
        if (s[j] == ':') {
          group = tmp;
          tmp.clear();
        } else if (s[j] == ',' || s[j] == '.') {
          tmp_members.insert(tmp);
          tmp.clear();
        } else if (s[j] != '.') {
          tmp += s[j];
        }
      }
      groups[group] = tmp_members;
      if (i == 0) {
        first_group = group;
      }
    }
    bool changed = true;
    map<string, set<string>> dp;
    set<string> s = dfs(groups, first_group, dp);
    cout << s.size() << endl;
  }
}

Error: TLE

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-10;
const double PI = acos(-1.0);

set<string> dfs(map<string, set<string>> &groups, string cur) {
  map<string, set<string>>::iterator it = groups.find(cur);
  if (it == groups.end()) {
    set<string> ret;
    ret.insert(cur);
    return ret;
  }
  set<string> &group = it->second;
  set<string> ret;
  for (set<string>::iterator it2 = group.begin(); it2 != group.end(); it2++) {
    set<string> s = dfs(groups, *it2);
    if (s.size() > ret.size()) {
      swap(ret, s);
    }
    ret.insert(ALL(s));
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    map<string, set<string>> groups;
    string first_group;
    REP(i, n) {
      string s;
      cin >> s;
      string tmp;
      string group;
      set<string> tmp_members;
      REP(j, s.size()) {
        if (s[j] == ':') {
          group = tmp;
          tmp.clear();
        } else if (s[j] == ',' || s[j] == '.') {
          tmp_members.insert(tmp);
          tmp.clear();
        } else if (s[j] != '.') {
          tmp += s[j];
        }
      }
      groups[group] = tmp_members;
      if (i == 0) {
        first_group = group;
      }
    }
    bool changed = true;
    set<string> s = dfs(groups, first_group);
    cout << s.size() << endl;
  }
}