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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
typedef long long ll;
#define BR "\\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, l, r) for (int i = (int)l; i < (int)(r); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

// config
// #define NDEBUG

// debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp, act)
#define STOP(e)
#else
#define DUMP(x) cerr << #x << " = " << (x)
#define DUMPLN(x) DUMP(x) << endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x) << endl
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl
#define CHECK(exp, act)                                                        \\
  if (exp != act) {                                                            \\
    DUMPLN(exp);                                                               \\
    DEBUGLN(act);                                                              \\
  }
#define STOP(e)                                                                \\
  CHECK(e, true);                                                              \\
  if (!(e))                                                                    \\
    exit(1);
#endif

stringstream ss;
template <class T> inline string toString(const vector<T> &x) {
  ss.str("");
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
  ss.str("");
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  ss.str("");
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

string BITtoString(int bit) {
  ss.str("");
  while (bit != 0) {
    ss << (bit % 2);
    bit /= 2;
  }
  string res = ss.str();
  reverse(ALL(res));
  return res;
}
char tmp[1000];
#define nextInt(n) scanf("%d", &n)
#define nextLong(n) scanf("%lld", &n)
#define nextDouble(n) scanf("%lf", &n) // I64d
#define nextChar(n) scanf("%c", &n)
#define nextString(n)                                                          \\
  scanf("%s", tmp);                                                            \\
  n = tmp

namespace _double_tmpl {
typedef long double D;
const D EPS = 1e-8, INF = 1e12;

static constexpr D Ae = 0;
D A(D a, D b) { return a + b; }
D Ainv(D a) { return -a; }
D S(D a, D b) { return A(a, Ainv(b)); }

static constexpr D Me = 1;
D M(D a, D b) { return a * b; }
D Minv(D a) { return 1.0 / a; };

template <typename T> int sig(T a, T b = 0) {
  return a < b - EPS ? -1 : a > b + EPS ? 1 : 0;
}
template <typename T> bool eq(T a, T b) { return sig(abs(a - b)) == 0; }
template <typename T> D norm(T a) { return a * a; }
} // namespace _double_tmpl

namespace _mod_tmpl {
typedef ll D;
const D EPS = 1, INF = 1LL << 58, MOD = 1e9 + 7;

D pmod(D v, D _MOD = MOD) { return (v % _MOD + _MOD) % _MOD; }
static constexpr D Ae = 0;
D A(D a, D b) { return pmod(pmod(a) + pmod(b)); }
D Ainv(D a) { return -a; }
D S(D a, D b) { return A(a, Ainv(b)); }
static constexpr D Me = 1;
D M(D a, D b) { return pmod(pmod(a) * pmod(b)); }
D Mpow(D x, D N) {
  D res = 1;
  while (N != 0) {
    if (N % 2 == 1)
      res = M(res, x);
    x = M(x, x);
    N /= 2;
  }
  return res;
}
D Minv(D a) { return Mpow(a, MOD - 2); };
D sig(D a, D b = 0) { return a <= b - EPS ? -1 : a >= b + EPS ? 1 : 0; } //-
bool eq(D a, D b) { return sig(a - b) == 0; }
D norm(D a) { return a * a; }

D fact(const D N) { return N == 0 ? 1 : M(N, fact(N - 1)); }
D nPr(const int N, const int r) { return M(fact(N), Minv(fact(N - r))); }
D nCr(const int N, const int r) { return M(nPr(N, r), Minv(fact(r))); }
} // namespace _mod_tmpl

// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

class Main {
public:
  vector<vector<int>> g;

  vector<bool> passed;
  set<int> res;
  void dfs(int v, int p) {
    if (passed[v])
      return;
    passed[v] = true;
    if (g[v].size() == 0)
      res.insert(v);
    for (int nv : g[v])
      if (nv != p) {
        dfs(nv, v);
      }
  }

  void run() {
    //	ifstream cin("in");
    //	ofstream cout( "out" );
    while (true) {
      res.clear();
      int n;
      cin >> n;
      if (n == 0)
        break;
      map<string, int> idmap;
      int id = 0;
      g = vector<vector<int>>(2000);

      REP(i, n) {
        string str;
        cin >> str;
        string v = "";
        string p = "";
        REP(j, str.size()) {
          if (str[j] == ':') {
            if (!idmap.count(v))
              idmap[v] = id++;
            p = v;
            v = "";
          } else if (str[j] == ',' || str[j] == '.') {
            if (!idmap.count(v))
              idmap[v] = id++;
            g[idmap[p]].push_back(idmap[v]);
            v = "";
          } else {
            v += str[j];
          }
        }
      }

      passed = vector<bool>(2000);
      dfs(0, -1);
      cout << res.size() << endl;
    }
  }
};
int main() {
  ss << fixed << setprecision(20);
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  Main().run();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
typedef long long ll;
#define BR "\\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, l, r) for (int i = (int)l; i < (int)(r); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

// config
// #define NDEBUG

// debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp, act)
#define STOP(e)
#else
#define DUMP(x) cerr << #x << " = " << (x)
#define DUMPLN(x) DUMP(x) << endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x) << endl
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl
#define CHECK(exp, act)                                                        \\
  if (exp != act) {                                                            \\
    DUMPLN(exp);                                                               \\
    DEBUGLN(act);                                                              \\
  }
#define STOP(e)                                                                \\
  CHECK(e, true);                                                              \\
  if (!(e))                                                                    \\
    exit(1);
#endif

stringstream ss;
template <class T> inline string toString(const vector<T> &x) {
  ss.str("");
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
  ss.str("");
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  ss.str("");
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

string BITtoString(int bit) {
  ss.str("");
  while (bit != 0) {
    ss << (bit % 2);
    bit /= 2;
  }
  string res = ss.str();
  reverse(ALL(res));
  return res;
}
char tmp[1000];
#define nextInt(n) scanf("%d", &n)
#define nextLong(n) scanf("%lld", &n)
#define nextDouble(n) scanf("%lf", &n) // I64d
#define nextChar(n) scanf("%c", &n)
#define nextString(n)                                                          \\
  scanf("%s", tmp);                                                            \\
  n = tmp

namespace _double_tmpl {
typedef long double D;
const D EPS = 1e-8, INF = 1e12;

static constexpr D Ae = 0;
D A(D a, D b) { return a + b; }
D Ainv(D a) { return -a; }
D S(D a, D b) { return A(a, Ainv(b)); }

static constexpr D Me = 1;
D M(D a, D b) { return a * b; }
D Minv(D a) { return 1.0 / a; };

template <typename T> int sig(T a, T b = 0) {
  return a < b - EPS ? -1 : a > b + EPS ? 1 : 0;
}
template <typename T> bool eq(T a, T b) { return sig(abs(a - b)) == 0; }
template <typename T> D norm(T a) { return a * a; }
} // namespace _double_tmpl

namespace _mod_tmpl {
typedef ll D;
const D EPS = 1, INF = 1LL << 58, MOD = 1e9 + 7;

D pmod(D v, D _MOD = MOD) { return (v % _MOD + _MOD) % _MOD; }
static constexpr D Ae = 0;
D A(D a, D b) { return pmod(pmod(a) + pmod(b)); }
D Ainv(D a) { return -a; }
D S(D a, D b) { return A(a, Ainv(b)); }
static constexpr D Me = 1;
D M(D a, D b) { return pmod(pmod(a) * pmod(b)); }
D Mpow(D x, D N) {
  D res = 1;
  while (N != 0) {
    if (N % 2 == 1)
      res = M(res, x);
    x = M(x, x);
    N /= 2;
  }
  return res;
}
D Minv(D a) { return Mpow(a, MOD - 2); };
D sig(D a, D b = 0) { return a <= b - EPS ? -1 : a >= b + EPS ? 1 : 0; } //-
bool eq(D a, D b) { return sig(a - b) == 0; }
D norm(D a) { return a * a; }

D fact(const D N) { return N == 0 ? 1 : M(N, fact(N - 1)); }
D nPr(const int N, const int r) { return M(fact(N), Minv(fact(N - r))); }
D nCr(const int N, const int r) { return M(nPr(N, r), Minv(fact(r))); }
} // namespace _mod_tmpl

// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

class Main {
public:
  vector<vector<int>> g;

  set<int> res;
  void dfs(int v, int p) {
    if (g[v].size() == 0)
      res.insert(v);
    for (int nv : g[v])
      if (nv != p) {
        dfs(nv, v);
      }
  }

  void run() {
    //	ifstream cin("in");
    //	ofstream cout( "out" );
    while (true) {
      res.clear();
      int n;
      cin >> n;
      if (n == 0)
        break;
      map<string, int> idmap;
      int id = 0;
      g = vector<vector<int>>(2000);

      REP(i, n) {
        string str;
        cin >> str;
        string v = "";
        string p = "";
        REP(j, str.size()) {
          if (str[j] == ':') {
            if (!idmap.count(v))
              idmap[v] = id++;
            p = v;
            v = "";
          } else if (str[j] == ',' || str[j] == '.') {
            if (!idmap.count(v))
              idmap[v] = id++;
            g[idmap[p]].push_back(idmap[v]);
            v = "";
          } else {
            v += str[j];
          }
        }
      }

      dfs(0, -1);
      cout << res.size() << endl;
    }
  }
};
int main() {
  ss << fixed << setprecision(20);
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  Main().run();
  return 0;
}