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

// #ifdef DEBUG
// #define _GLIBCXX_DEBUG
// #endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

// these require C++11
#include <chrono>
#include <random>
#include <thread>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define int long long
#define var auto
#define val const auto

#define all(c) c.begin(), c.end()
#define repeat(i, n) for (int i = 0; i < static_cast<int>(n); i++)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

typedef complex<double> point;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
  os << "{";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "}";
  return os;
}

template <typename T> set<T> operator|(const set<T> &a, const set<T> &b) {
  set<T> s = a;
  s.insert(b.begin(), b.end());
  return s;
}
vector<string> split_with_delim(const string &s, char delim) {
  istringstream ss(s);
  vector<string> ret;
  string tmp;
  while (getline(ss, tmp, delim)) {
    ret.push_back(tmp);
  }
  return ret;
}

set<string> get_member(const string &here,
                       map<string, vector<string>> &children,
                       set<string> &already_added) {
  if (already_added.find(here) != already_added.end())
    return {};
  already_added.insert(here);
  if (children.find(here) == children.end()) {
    return {here};
  } else {
    set<string> ret;
    for (string s : children[here]) {
      ret = ret | get_member(s, children, already_added);
    }
    return ret;
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (true) {
    map<string, vector<string>> children;
    int n;
    cin >> n;
    if (n == 0)
      break;
    cin.ignore();
    string search;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      s = s.substr(0, s.size() - 1); // .
      val v = split_with_delim(s, ':');
      val group = v[0];
      val member = split_with_delim(v[1], ',');
      children[group] = member;
      if (i == 0)
        search = group;
    }

    set<string> checked;
    set<string> member = get_member(search, children, checked);
    cout << member.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// #ifdef DEBUG
// #define _GLIBCXX_DEBUG
// #endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

// these require C++11
#include <chrono>
#include <random>
#include <thread>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define int long long
#define var auto
#define val const auto

#define all(c) c.begin(), c.end()
#define repeat(i, n) for (int i = 0; i < static_cast<int>(n); i++)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

typedef complex<double> point;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
  os << "{";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "}";
  return os;
}

template <typename T> set<T> operator|(const set<T> &a, const set<T> &b) {
  set<T> s = a;
  s.insert(b.begin(), b.end());
  return s;
}
vector<string> split_with_delim(const string &s, char delim) {
  istringstream ss(s);
  vector<string> ret;
  string tmp;
  while (getline(ss, tmp, delim)) {
    ret.push_back(tmp);
  }
  return ret;
}

set<string> get_member(const string &here,
                       map<string, vector<string>> &children) {
  if (children.find(here) == children.end()) {
    return {here};
  } else {
    set<string> ret;
    for (string s : children[here]) {
      ret = ret | get_member(s, children);
    }
    return ret;
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (true) {
    map<string, vector<string>> children;
    int n;
    cin >> n;
    if (n == 0)
      break;
    cin.ignore();
    string search;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      s = s.substr(0, s.size() - 1); // .
      val v = split_with_delim(s, ':');
      val group = v[0];
      val member = split_with_delim(v[1], ',');
      children[group] = member;
      if (i == 0)
        search = group;
    }

    set<string> member = get_member(search, children);
    cout << member.size() << endl;
  }
  return 0;
}