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

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;

typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

typedef vector<double> VD;
typedef vector<VD> VVD;

typedef vector<string> VS;
typedef vector<VS> VVS;

typedef vector<bool> VB;
typedef vector<VB> VVB;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(xx, y, mx, my) (0 <= (x) && 0 <= (y) && (x) < (mx) && y < (my))
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)

const int INF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int N;
map<string, VS> gs;
map<string, VS> memo;

VS dfs(string &key0) {
  if (memo.count(key0)) {
    return memo[key0];
  }

  if (!gs.count(key0)) {
    return memo[key0] = VS({key0});
  }

  VS res;
  for (auto &key : gs[key0]) {
    VS res2 = dfs(key);
    VS t(100000);
    auto it = set_union(ALL(res), ALL(res2), t.begin());
    t.resize(it - t.begin());
    res = t;
  }

  sort(ALL(res));

  return memo[key0] = res;
}

int solve(string &key) {
  memo.clear();
  VS res = dfs(key);
  return res.size();
}

int main(void) {
  while (cin >> N, N) {
    string first_gname;
    gs.clear();
    REP(i, 0, N) {
      string s;
      cin >> s;

      int start;
      string gname;
      {
        auto pos = s.find(':');
        start = pos + 1;
        gname = s.substr(0, pos);
      }

      if (first_gname == "") {
        first_gname = gname;
      }

      VS ms;
      while (true) {
        auto pos = s.find_first_of(",.", start);
        string member = s.substr(start, pos - start);
        ms.PB(member);
        start = pos + 1;
        if (s[pos] == '.')
          break;
      }

      gs[gname] = ms;
    }

    cout << solve(first_gname) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;

typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

typedef vector<double> VD;
typedef vector<VD> VVD;

typedef vector<string> VS;
typedef vector<VS> VVS;

typedef vector<bool> VB;
typedef vector<VB> VVB;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(xx, y, mx, my) (0 <= (x) && 0 <= (y) && (x) < (mx) && y < (my))
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)

const int INF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int N;
map<string, VS> gs;

set<string> names;

void dfs(string &key0) {
  if (!gs.count(key0)) {
    names.insert(key0);
    return;
  }

  for (auto &key : gs[key0]) {
    dfs(key);
  }
}

int solve(string &key) {
  names.clear();
  dfs(key);
  return names.size();
}

int main(void) {
  while (cin >> N, N) {
    string first_gname;
    gs.clear();
    REP(i, 0, N) {
      string s;
      cin >> s;

      int start;
      string gname;
      {
        auto pos = s.find(':');
        start = pos + 1;
        gname = s.substr(0, pos);
      }

      if (first_gname == "") {
        first_gname = gname;
      }

      VS ms;
      while (true) {
        auto pos = s.find_first_of(",.", start);
        string member = s.substr(start, pos - start);
        ms.PB(member);
        start = pos + 1;
        if (s[pos] == '.')
          break;
      }

      gs[gname] = ms;
    }

    cout << solve(first_gname) << endl;
  }

  return 0;
}