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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
        for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
                if (i) os << " ";
                os << get<0>(t);
        }
        return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

struct Law {
  int G;
  vector<int> S;
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N, E, T;
  cin >> N >> E >> T;
  --T;
  vector<int> W(N);
  cin >> W;
  vector<Law> law;
  REP(i, E) {
    int G, C;
    cin >> G >> C;
    --G;
    vector<int> S(C);
    cin >> S;
    REP(i, C) S[i]--;
    law.pb((Law){G, S});
  }
  vector<int> dp(N, inf);
  REP(i, N) {
    if (W[i] == 1) {
      dp[i] = 1;
    }
  }
  REP(i, E) REP(j, E) {
    int M = 0, m = inf;
    vector<int> a(law[j].S.size());
    REP(k, law[j].S.size()) {
      int id = law[j].S[k];
      a[k] = dp[id];
    }
    sort(ALL(a), greater<int>());
    int c = 1;
    REP(i, a.size()) { c = max(c, a[i] + (int)i); }
    if (c < dp[law[j].G]) {
      dp[law[j].G] = c;
    }
  }
  if (dp[T] == inf)
    cout << -1 << endl;
  else
    cout << dp[T] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
        for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
                if (i) os << " ";
                os << get<0>(t);
        }
        return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

struct Law {
  int G;
  vector<int> S;
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N, E, T;
  cin >> N >> E >> T;
  --T;
  vector<int> W(N);
  cin >> W;
  vector<Law> law;
  REP(i, E) {
    int G, C;
    cin >> G >> C;
    --G;
    vector<int> S(C);
    cin >> S;
    REP(i, C) S[i]--;
    law.pb((Law){G, S});
  }
  vector<int> dp(N, inf);
  REP(i, N) {
    if (W[i] == 1) {
      dp[i] = 1;
    }
  }
  REP(t, 100) REP(i, N) REP(j, E) {
    int M = 0, m = inf;
    vector<int> a(law[j].S.size());
    REP(k, law[j].S.size()) {
      int id = law[j].S[k];
      a[k] = dp[id];
    }
    sort(ALL(a), greater<int>());
    int c = 1;
    REP(i, a.size()) { c = max(c, a[i] + (int)i); }
    if (c < dp[law[j].G]) {
      dp[law[j].G] = c;
    }
  }
  if (dp[T] == inf)
    cout << -1 << endl;
  else
    cout << dp[T] << endl;
}