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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

int nextInt() {
  int a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}

void inputVector(vector<int> &v, int &n) {
  rep(i, n) { v.push_back(nextInt()); }
}
void inputVector(vector<double> &v, int &n) {
  rep(i, n) { v.push_back(nextDouble()); }
}
void inputVector(vector<string> &v, int &n) {
  rep(i, n) { v.push_back(nextString()); }
}

map<string, vector<string>> infos;
map<string, set<string>> memo;

set<string> solve(string team) {
  if (infos.find(team) == infos.end()) {
    set<string> tmp;
    tmp.insert(team);
    return tmp;
  }

  if (memo.find(team) != memo.end()) {
    return memo[team];
  }

  vector<string> mems = infos[team];
  set<string> ret;
  rep(i, mems.size()) {
    set<string> tmp = solve(mems[i]);
    for (string str : tmp) {
      ret.insert(str);
    }
  }
  memo[team] = ret;
  return ret;
}

signed main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    infos.clear();
    memo.clear();

    string firstTeam;
    rep(i, n) {
      string str;
      cin >> str;

      string team = str.substr(0, str.find(":"));
      if (i == 0)
        firstTeam = team;

      string names = str.substr(str.find(":") + 1);
      names.substr(0, names.size() - 1);

      vector<string> tmp;
      while (names.find(",") != string::npos) {
        tmp.push_back(names.substr(0, names.find(",")));
        names = names.substr(names.find(",") + 1);
      }
      tmp.push_back(names.substr(0, names.size() - 1));

      infos.insert(make_pair(team, tmp));
    }

    cout << solve(firstTeam).size() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

int nextInt() {
  int a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}

void inputVector(vector<int> &v, int &n) {
  rep(i, n) { v.push_back(nextInt()); }
}
void inputVector(vector<double> &v, int &n) {
  rep(i, n) { v.push_back(nextDouble()); }
}
void inputVector(vector<string> &v, int &n) {
  rep(i, n) { v.push_back(nextString()); }
}

set<string> names;
map<string, vector<string>> infos;

void solve(string team) {
  if (infos.find(team) == infos.end()) {
    names.insert(team);
    return;
  }

  vector<string> mems = infos[team];
  rep(i, mems.size()) { solve(mems[i]); }
}

signed main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    names.clear();
    infos.clear();

    string firstTeam;
    rep(i, n) {
      string str;
      cin >> str;

      string team = str.substr(0, str.find(":"));
      if (i == 0)
        firstTeam = team;

      string names = str.substr(str.find(":") + 1);
      names.substr(0, names.size() - 1);

      vector<string> tmp;
      while (names.find(",") != string::npos) {
        tmp.push_back(names.substr(0, names.find(",")));
        names = names.substr(names.find(",") + 1);
      }
      tmp.push_back(names.substr(0, names.size() - 1));

      infos.insert(make_pair(team, tmp));
    }

    solve(firstTeam);

    cout << names.size() << endl;
  }

  return 0;
}