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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RREP(i, x) for (int i = (x); i >= 0; i--)
#define RFOR(i, c)                                                             \\
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)

// FILE *in = freopen("input.txt", "r", stdin);
// FILE *out = freopen("output.txt", "w", stdout);

int n;

struct node {
  string name;
  vector<node *> child;
  bool check;
  node(char *names) {
    name = string(names);
    check = false;
  }
};

map<string, node *> names;
string first;

void input() {
  for (int i = 0; i < n; i++) {
    getc(stdin);
    char c;
    char groupname[100];
    int gc = 0;
    while ((c = getc(stdin)) != ':') {
      groupname[gc++] = c;
    }
    groupname[gc] = 0;
    node *group;
    auto grpfind = names.find(groupname);
    if (grpfind == names.end()) {
      group = new node(groupname);
      names[groupname] = group;
      if (i == 0)
        first = groupname;
    } else {
      group = grpfind->second;
    }
    do {
      char person[100];
      int pc = 0;
      while ((c = getc(stdin)) != ',' && c != '.') {
        person[pc++] = c;
      }
      person[pc] = 0;
      node *pp;
      auto ppp = names.find(person);
      if (ppp == names.end()) {
        pp = new node(person);
        names[person] = pp;
      } else {
        pp = ppp->second;
      }
      group->child.push_back(pp);
    } while (c != '.');
  }
}

int back(string name) {
  node *pp = names[name];
  if (pp->check)
    return 0;
  pp->check = true;
  if (pp->child.size() == 0) {
    return 1;
  }
  int res = 0;
  REP(i, pp->child.size()) { res += back(pp->child[i]->name); }
  return res;
}

void process() {
  int res = back(first);
  printf("%d\\n", res);
}

void output() {}

int main() {
  while (scanf("%d", &n) > 0 && n) {
    names.clear();
    input();
    process();
    output();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RREP(i, x) for (int i = (x); i >= 0; i--)
#define RFOR(i, c)                                                             \\
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)

// FILE *in = freopen("input.txt", "r", stdin);
// FILE *out = freopen("output.txt", "w", stdout);

int n;

struct node {
  string name;
  vector<node *> child;
  bool check;
  node(char *names) {
    name = string(names);
    check = false;
  }
};

map<string, node *> names;
string first;

void input() {
  for (int i = 0; i < n; i++) {
    getc(stdin);
    char c;
    char groupname[100];
    int gc = 0;
    while ((c = getc(stdin)) != ':') {
      groupname[gc++] = c;
    }
    groupname[gc] = 0;
    node *group;
    auto grpfind = names.find(groupname);
    if (grpfind == names.end()) {
      group = new node(groupname);
      names[groupname] = group;
      if (i == 0)
        first = groupname;
    } else {
      group = grpfind->second;
    }
    do {
      char person[100];
      int pc = 0;
      while ((c = getc(stdin)) != ',' && c != '.') {
        person[pc++] = c;
      }
      person[pc] = 0;
      node *pp;
      auto ppp = names.find(person);
      if (ppp == names.end()) {
        pp = new node(person);
        names[person] = pp;
      } else {
        pp = ppp->second;
      }
      group->child.push_back(pp);
    } while (c != '.');
  }
}

int back(string name) {
  node *pp = names[name];
  if (pp->check)
    return 0;
  if (pp->child.size() == 0) {
    pp->check = true;
    return 1;
  }
  int res = 0;
  REP(i, pp->child.size()) { res += back(pp->child[i]->name); }
  return res;
}

void process() {
  int res = back(first);
  printf("%d\\n", res);
}

void output() {}

int main() {
  while (scanf("%d", &n) > 0 && n) {
    names.clear();
    input();
    process();
    output();
  }
  return 0;
}