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

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

vector<int> G[1024];
set<int> s;
int vis[1024];

void dfs(int cur) {
  //  cout << cur << endl;
  if (vis[cur])
    return;
  vis[cur] = 1;
  if (G[cur].size() == 0) {
    s.insert(cur);
    return;
  }

  rep(i, G[cur].size()) dfs(G[cur][i]);
}

int main() {
  int N;
  while (cin >> N && N) {
    rep(i, 1024) G[i].clear();
    map<string, int> m;
    rep(i, N) {
      string s;
      cin >> s;
      replace(s.begin(), s.end(), ',', ' ');
      replace(s.begin(), s.end(), '.', ' ');
      replace(s.begin(), s.end(), ':', ' ');
      stringstream buf(s);
      string cur, tar;
      buf >> cur;
      if (m.find(cur) == m.end())
        m[cur] = m.size();
      while (!buf.eof()) {
        buf >> tar;
        if (m.find(tar) == m.end())
          m[tar] = m.size();
        G[m[cur]].push_back(m[tar]);
      }
    }
    //    for(map<string,int>::iterator it = m.begin();it!=m.end();it++) cout <<
    //    it->first << " " << it->second << endl;
    memset(vis, 0, sizeof(vis));
    s.clear();
    dfs(1);
    cout << s.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

vector<int> G[1024];
set<int> s;

void dfs(int cur) {
  if (G[cur].size() == 0) {
    s.insert(cur);
    return;
  }

  rep(i, G[cur].size()) dfs(G[cur][i]);
}

int main() {
  int N;
  while (cin >> N && N) {
    rep(i, 1024) G[i].clear();
    map<string, int> m;
    rep(i, N) {
      string s;
      cin >> s;
      replace(s.begin(), s.end(), ',', ' ');
      replace(s.begin(), s.end(), '.', ' ');
      replace(s.begin(), s.end(), ':', ' ');
      stringstream buf(s);
      string cur, tar;
      buf >> cur;
      if (m.find(cur) == m.end())
        m[cur] = m.size();
      while (!buf.eof()) {
        buf >> tar;
        if (m.find(tar) == m.end())
          m[tar] = m.size();
        G[m[cur]].push_back(m[tar]);
      }
    }
    int res = 0;
    rep(i, m.size()) {
      s.clear();
      dfs(i);
      res = max(res, (int)s.size());
    }
    cout << res << endl;
  }
  return 0;
}