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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

void count(int idx, map<int, vector<int>> &m, set<int> &s, set<int> &f) {
  if (f.count(idx) != 0)
    return;
  f.insert(idx);
  if (m.count(idx) == 0)
    s.insert(idx);
  else {
    vector<int> &v = m[idx];
    FOR(it, v) count(*it, m, s, f);
  }
}

inline int n2i(const string &str, map<string, int> &m) {
  if (m.count(str) == 0) {
    int next = m.size();
    return m[str] = next;
  } else
    return m[str];
}

int main() {
  int n;
  char table[128];
  REP(i, 128) table[i] = i;
  table[':'] = table[','] = table['.'] = ' ';

  while (cin >> n, n) {
    string str;
    int start;
    map<string, int> m;
    map<int, vector<int>> vv;
    REP(i, n) {
      cin >> str;
      FOR(it, str) *it = table[*it];

      stringstream ss(str);
      string tmp;
      int idx;

      ss >> tmp;
      if (i == 0)
        start = idx = n2i(tmp, m);
      else
        idx = n2i(tmp, m);

      vector<int> &v = vv[idx];

      while (ss >> tmp)
        v.push_back(n2i(tmp, m));
    }

    set<int> s, f;
    count(start, vv, s, f);
    cout << s.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

void count(int idx, map<int, vector<int>> &m, set<int> &s) {
  if (m.count(idx) == 0)
    s.insert(idx);
  else {
    vector<int> &v = m[idx];
    FOR(it, v) count(*it, m, s);
  }
}

inline int n2i(const string &str, map<string, int> &m) {
  if (m.count(str) == 0) {
    int next = m.size();
    return m[str] = next;
  } else
    return m[str];
}

int main() {
  int n;
  char table[128];
  REP(i, 128) table[i] = i;
  table[':'] = table[','] = table['.'] = ' ';

  while (cin >> n, n) {
    string str;
    int start;
    map<string, int> m;
    map<int, vector<int>> vv;
    REP(i, n) {
      cin >> str;
      FOR(it, str) *it = table[*it];

      stringstream ss(str);
      string tmp;
      int idx;

      ss >> tmp;
      if (i == 0)
        start = idx = n2i(tmp, m);
      else
        idx = n2i(tmp, m);

      vector<int> &v = vv[idx];

      while (ss >> tmp)
        v.push_back(n2i(tmp, m));
    }

    set<int> s;
    count(start, vv, s);
    cout << s.size() << endl;
  }
  return 0;
}