Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// C++11
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
map<string, set<string>> memo;
set<string> rec(string name, vector<string> &commands, map<string, int> &id) {
  if (memo.count(name))
    return memo[name];
  set<string> res;
  if (id.count(name)) {
    string s = commands[id[name]];
    REP(i, s.size()) if (s[i] == ':') {
      s = s.substr(i + 1);
      break;
    }
    REP(i, s.size()) if (s[i] == ',' || s[i] == '.') s[i] = ' ';
    stringstream ss(s);
    string member;
    while (ss >> member) {
      set<string> d = rec(member, commands, id);
      FORIT(it, d) res.insert(*it);
    }
  } else {
    res.insert(name);
  }
  return memo[name] = res;
}

int main() {
  int N;
  while (cin >> N && N) {
    memo.clear();
    vector<string> commands(N);
    map<string, int> id;
    REP(i, N) cin >> commands[i];
    string first;
    REP(i, N) {
      string s = commands[i];
      string group;
      REP(j, s.size()) if (s[j] == ':') group = s.substr(0, j);
      id[group] = i;
      if (i == 0)
        first = group;
    }
    set<string> ans = rec(first, commands, id);
    // FORIT(it, ans) cout<<*it<<endl;
    cout << ans.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// C++11
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
set<string> rec(string name, vector<string> &commands, map<string, int> &id) {
  set<string> res;
  if (id.count(name)) {
    string s = commands[id[name]];
    REP(i, s.size()) if (s[i] == ':') {
      s = s.substr(i + 1);
      break;
    }
    REP(i, s.size()) if (s[i] == ',' || s[i] == '.') s[i] = ' ';
    stringstream ss(s);
    string member;
    while (ss >> member) {
      set<string> d = rec(member, commands, id);
      FORIT(it, d) res.insert(*it);
    }
  } else {
    res.insert(name);
  }
  return res;
}

int main() {
  int N;
  while (cin >> N && N) {
    vector<string> commands(N);
    map<string, int> id;
    REP(i, N) cin >> commands[i];
    string first;
    REP(i, N) {
      string s = commands[i];
      string group;
      REP(j, s.size()) if (s[j] == ':') group = s.substr(0, j);
      id[group] = i;
      if (i == 0)
        first = group;
    }
    set<string> ans = rec(first, commands, id);
    // FORIT(it, ans) cout<<*it<<endl;
    cout << ans.size() << endl;
  }
  return 0;
}