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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type {
  Call,
  Called,
  Lonely,
};

int N, memo[5001][3];

vector<int> V[5001];

int recursive(int node_id, Type type) {

  if (memo[node_id][type] != -1)
    return memo[node_id][type];

  int ret = 0, tmp, sum;

  switch (type) {
  case Call:
    sum = 0;
    for (int i = 0; i < V[node_id].size(); i++) {
      sum +=
          max(recursive(V[node_id][i], Call), recursive(V[node_id][i], Lonely));
    }
    for (int i = 0; i < V[node_id].size(); i++) {
      ret = max(
          ret, sum -
                   max(memo[V[node_id][i]][Call], memo[V[node_id][i]][Lonely]) +
                   recursive(V[node_id][i], Called) + 1);
    }
    break;
  case Called:
    for (int i = 0; i < V[node_id].size(); i++) {
      ret +=
          max(recursive(V[node_id][i], Call), recursive(V[node_id][i], Lonely));
    }
    break;
  case Lonely:
    for (int i = 0; i < V[node_id].size(); i++) {
      ret +=
          max(recursive(V[node_id][i], Call), recursive(V[node_id][i], Lonely));
    }
    break;
  }

  return memo[node_id][type] = ret;
}

int func() { return max(recursive(0, Call), recursive(0, Lonely)); }

int main(void) {

  scanf("%d", &N);
  int tmp;

  for (int day = 1; day <= N; day++) {

    scanf("%d", &tmp);
    V[tmp].push_back(day);
    for (int i = 0; i <= day; i++) {
      for (int k = 0; k < 3; k++)
        memo[i][k] = -1;
    }

    printf("%d\\n", func());
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type {
  Call,
  Called,
  Lonely,
};

int N, memo[5001][3];

vector<int> V[5001];

int recursive(int node_id, Type type) {

  if (memo[node_id][type] != -1)
    return memo[node_id][type];

  int ret = 0, tmp;

  switch (type) {
  case Call:
    for (int i = 0; i < V[node_id].size(); i++) {
      tmp = recursive(V[node_id][i], Called) + 1;
      for (int k = 0; k < V[node_id].size(); k++) {
        if (i == k)
          continue;
        tmp += max(recursive(V[node_id][k], Call),
                   recursive(V[node_id][k], Lonely));
      }
      ret = max(ret, tmp);
    }
    break;
  case Called:
    for (int i = 0; i < V[node_id].size(); i++) {
      ret +=
          max(recursive(V[node_id][i], Call), recursive(V[node_id][i], Lonely));
    }
    break;
  case Lonely:
    for (int i = 0; i < V[node_id].size(); i++) {
      ret +=
          max(recursive(V[node_id][i], Call), recursive(V[node_id][i], Lonely));
    }
    break;
  }

  return memo[node_id][type] = ret;
}

int func() { return max(recursive(0, Call), recursive(0, Lonely)); }

int main(void) {

  scanf("%d", &N);
  int tmp;

  for (int day = 1; day <= N; day++) {

    scanf("%d", &tmp);
    V[tmp].push_back(day);
    for (int i = 0; i <= day; i++) {
      for (int k = 0; k < 3; k++)
        memo[i][k] = -1;
    }

    printf("%d\\n", func());
  }
  return 0;
}