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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

const double PI = 4 * atan(1.0);

void fast_stream() { std::ios_base::sync_with_stdio(0); }

#ifdef RED_FILE
ifstream ifs("input.txt");
ofstream ofs("output.txt");
#define cin ifs
#define cout ofs
#endif

map<string, vector<string>> msn;

map<string, set<string>> dp;

set<string> dfs(string name) {
  if (dp.find(name) != dp.end())
    return dp[name];
  set<string> res;
  if (msn.find(name) == msn.end()) {
    res.insert(name);
    return res;
  }
  for (int i = 0; i < msn[name].size(); i++) {
    set<string> tmp = dfs(msn[name][i]);
    for (set<string>::iterator it = tmp.begin(); it != tmp.end(); it++)
      res.insert(*it);
  }
  return dp[name] = res;
}

int main() {

  int n;
  while (cin >> n && n != 0) {
    dp.clear();
    msn.clear();
    string s;
    vector<string> ns;
    for (int i = 0; i < n; i++) {
      cin >> s;
      string nodeName;
      for (int j = 0; j < s.size(); j++) {
        if (s[j] == ':') {
          nodeName = s.substr(0, j);
          s = s.substr(j + 1);
          break;
        }
      }
      ns.push_back(nodeName);
      int prv = 0;
      vector<string> vs;
      for (int j = 0; j < s.size(); j++) {
        if (s[j] == ',' || s[j] == '.') {
          vs.push_back(s.substr(prv, j - prv));
          prv = j + 1;
        }
      }
      msn[nodeName] = vs;
    }
    int res = dfs(ns[0]).size();
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

typedef complex<double> P;
typedef pair<P, P> Edge;
typedef long long ll;

const double PI = 4 * atan(1.0);

void fast_stream() { std::ios_base::sync_with_stdio(0); }

// #define RED_FILE

#ifdef RED_FILE
ifstream ifs("input.txt");
ofstream ofs("output.txt");
#define cin ifs
#define cout ofs
#endif

map<string, vector<string>> msn;

set<string> dfs(string name) {
  set<string> res;
  if (msn.find(name) == msn.end()) {
    res.insert(name);
    return res;
  }
  for (int i = 0; i < msn[name].size(); i++) {
    set<string> tmp = dfs(msn[name][i]);
    for (set<string>::iterator it = tmp.begin(); it != tmp.end(); it++)
      res.insert(*it);
  }
  return res;
}

int main() {

  int n;
  while (cin >> n && n != 0) {
    msn.clear();
    string s;
    vector<string> ns;
    for (int i = 0; i < n; i++) {
      cin >> s;
      string nodeName;
      for (int j = 0; j < s.size(); j++) {
        if (s[j] == ':') {
          nodeName = s.substr(0, j);
          s = s.substr(j + 1);
          break;
        }
      }
      ns.push_back(nodeName);
      int prv = 0;
      vector<string> vs;
      for (int j = 0; j < s.size(); j++) {
        if (s[j] == ',' || s[j] == '.') {
          vs.push_back(s.substr(prv, j - prv));
          prv = j + 1;
        }
      }
      msn[nodeName] = vs;
    }
    int res = dfs(ns[0]).size();
    cout << res << endl;
  }

  return 0;
}