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

#include <algorithm>
#include <cassert>
#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 FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N, E, T;
vector<int> W;
vector<int> G;
vector<int> S[1000];
int memo[300];
int dfs(int K, vector<bool> used) {
  if (memo[K] != -1 && memo[K] < INF)
    return memo[K];
  if (W[K])
    return 1;
  int res = INF;
  used[K] = true;
  REP(i, E) if (G[i] == K) {
    vector<int> need;
    REP(j, S[i].size()) {
      if (used[S[i][j]])
        need.push_back(INF);
      else
        need.push_back(dfs(S[i][j], used));
    }
    sort(need.begin(), need.end());
    reverse(need.begin(), need.end());
    int ans = 0;
    REP(i, need.size()) ans = max(ans, need[i] + i);
    res = min(res, ans);
  }
  return memo[K] = res;
}
int main() {
  while (cin >> N >> E >> T) {
    T--;
    W.assign(N, 0);
    REP(i, N) cin >> W[i];
    G.assign(E, 0);
    REP(i, E) {
      cin >> G[i];
      G[i]--;
      int C;
      cin >> C;
      S[i].assign(C, 0);
      REP(j, C) cin >> S[i][j];
      REP(j, C) S[i][j]--;
    }
    /*
    vector<bool> used(N, false);
    memset(memo, -1, sizeof memo);
    int ans = dfs(T, used);
    if(ans == INF) ans = -1;
    cout << ans << endl;
    */
    bool update;
    vector<int> dist(N, INF);
    REP(i, N) if (W[i]) dist[i] = 1;
    while (true) {
      update = false;
      REP(i, E) {
        vector<int> need;
        REP(j, S[i].size()) { need.push_back(dist[S[i][j]]); }
        sort(need.begin(), need.end());
        reverse(need.begin(), need.end());
        int res = 0;
        REP(j, need.size()) res = max(res, need[j] + j);
        if (dist[G[i]] > res) {
          update = true;
          dist[G[i]] = res;
        }
      }
      if (!update)
        break;
    }
    if (dist[T] == INF)
      dist[T] = -1;
    cout << dist[T] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#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 FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N, E, T;
vector<int> W;
vector<int> G;
vector<int> S[1000];
int memo[300];
int dfs(int K, vector<bool> used) {
  if (memo[K] != -1 && memo[K] < INF)
    return memo[K];
  if (W[K])
    return 1;
  int res = INF;
  used[K] = true;
  REP(i, E) if (G[i] == K) {
    vector<int> need;
    REP(j, S[i].size()) {
      if (used[S[i][j]])
        need.push_back(INF);
      else
        need.push_back(dfs(S[i][j], used));
    }
    sort(need.begin(), need.end());
    reverse(need.begin(), need.end());
    int ans = 0;
    REP(i, need.size()) ans = max(ans, need[i] + i);
    res = min(res, ans);
  }
  return memo[K] = res;
}
int main() {
  while (cin >> N >> E >> T) {
    T--;
    W.assign(N, 0);
    REP(i, N) cin >> W[i];
    G.assign(E, 0);
    REP(i, E) {
      cin >> G[i];
      G[i]--;
      int C;
      cin >> C;
      S[i].assign(C, 0);
      REP(j, C) cin >> S[i][j];
      REP(j, C) S[i][j]--;
    }
    vector<bool> used(N, false);
    memset(memo, -1, sizeof memo);
    int ans = dfs(T, used);
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}