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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vi dp, visit;
vvi tG;
void dfs(int a) {
  if (visit[a])
    return;
  if (tG[a].size() == 0) {
    dp[a] = true;
    return;
  }
  visit[a] = true;
  rep(i, tG[a].size()) dfs(tG[a][i]);
  return;
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<vector<string>> in(n);
    rep(i, n) {
      string s;
      cin >> s;
      string t = "";
      rep(j, s.size()) {
        if (!isalpha(s[j])) {
          in[i].pb(t);
          j++;
          t = "";
        }
        t += s[j];
      }
    }
    vvi G(n);
    map<string, int> m;
    int co = 1;
    rep(i, n) rep(j, in[i].size()) {
      if (m[in[i][j]]) {
        G[i].pb(m[in[i][j]]);
      } else {
        G[i].pb(co);
        m[in[i][j]] = co;
        co++;
      }
    }
    tG = vvi(co);
    rep(i, n) { rep(j, G[i].size() - 1) tG[G[i][0]].pb(G[i][j + 1]); }
    dp = vi(co);
    visit = vi(co);
    dfs(1);
    int out = 0;
    rep(i, co) if (dp[i]) out++;
    cout << out << endl;
    //		cout<<dfs(1)<<endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vi dp, visit;
vvi tG;
void dfs(int a) {
  if (visit[a])
    return;
  if (tG[a].size() == 0) {
    dp[a] = true;
    return;
  }
  rep(i, tG[a].size()) dfs(tG[a][i]);
  return;
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<vector<string>> in(n);
    rep(i, n) {
      string s;
      cin >> s;
      string t = "";
      rep(j, s.size()) {
        if (!isalpha(s[j])) {
          in[i].pb(t);
          j++;
          t = "";
        }
        t += s[j];
      }
    }
    vvi G(n);
    map<string, int> m;
    int co = 1;
    rep(i, n) rep(j, in[i].size()) {
      if (m[in[i][j]]) {
        G[i].pb(m[in[i][j]]);
      } else {
        G[i].pb(co);
        m[in[i][j]] = co;
        co++;
      }
    }
    tG = vvi(co);
    rep(i, n) { rep(j, G[i].size() - 1) tG[G[i][0]].pb(G[i][j + 1]); }
    dp = vi(co);
    visit = vi(co);
    dfs(1);
    int out = 0;
    rep(i, co) if (dp[i]) out++;
    cout << out << endl;
    //		cout<<dfs(1)<<endl;
  }
}