Problem p01710 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

namespace SCC {
void visit(const vector<vector<int>> &G, vector<int> &vs, vector<int> &used,
           int v) {
  used[v] = true;
  for (auto u : G[v]) {
    if (!used[u])
      visit(G, vs, used, u);
  }
  vs.push_back(v);
}

void visit2(const vector<vector<int>> &T, vector<int> &used, vector<int> &comp,
            vector<int> &vec, int k, int v) {
  comp[v] = k;
  used[v] = true;
  vec.push_back(v);

  for (auto u : T[v]) {
    if (!used[u])
      visit2(T, used, comp, vec, k, u);
  }
}

void decompose(const vector<vector<int>> &G, vector<vector<int>> &H,
               vector<int> &comp) {
  vector<vector<int>> T(G.size());
  for (int i = 0; i < G.size(); i++) {
    for (auto v : G[i]) {
      T[v].push_back(i);
    }
  }
  comp.resize(G.size());

  vector<int> vs(G.size());
  vector<int> used(G.size());
  for (int i = 0; i < G.size(); i++) {
    if (!used[i])
      visit(G, vs, used, i);
  }
  reverse(vs.begin(), vs.end());
  fill(used.begin(), used.end(), 0);

  int K = 0;
  vector<vector<int>> S;
  for (auto v : vs) {
    if (!used[v]) {
      S.push_back(vector<int>());
      visit2(T, used, comp, S.back(), K++, v);
    }
  }

  H.resize(K);
  fill(used.begin(), used.end(), 0);
  for (int i = 0; i < K; i++) {
    for (auto v : S[i]) {
      for (auto u : G[v]) {
        if (used[comp[u]] || comp[v] == comp[u])
          continue;
        used[comp[u]] = true;
        H[comp[v]].push_back(comp[u]);
      }
    }
    for (auto v : H[i])
      used[v] = false;
  }
}
} // namespace SCC

int N, M, T;

void solve() {
  vint p(N), t(N), k(N);
  rep(i, N) cin >> p[i] >> t[i] >> k[i];

  vector<vint> G(N);
  vector<int> sel(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
    if (a == b)
      sel[a] = true;
  }

  vector<vint> H;
  vint comp;

  SCC::decompose(G, H, comp);

  vector<vpint> lis(H.size());

  vint cnt(H.size());
  for (int i = 0; i < N; i++)
    cnt[comp[i]]++;

  for (int i = 0; i < N; i++) {
    if (cnt[comp[i]] == 1 && !sel[i]) {
      lis[comp[i]].pb({p[i], t[i]});
    } else {
      for (int j = 1;; j <<= 1) {
        if (k[i] >= j) {
          lis[comp[i]].pb({p[i] * j, t[i] * j});
          k[i] -= j;
        } else {
          if (k[i])
            lis[comp[i]].pb({p[i] * k[i], t[i] * k[i]});
          break;
        }
      }
    }
  }

  vector<vint> dp(H.size(), vint(T + 1));

  for (int i = 0; i < H.size(); i++) {
    for (auto &w : lis[i]) {
      for (int j = T; j >= 0; j--) {
        if (j + w.se <= T)
          chmax(dp[i][j + w.se], dp[i][j] + w.fi);
      }
    }
    for (auto &u : H[i])
      rep(j, T + 1) chmax(dp[u][j], dp[i][j]);
  }

  int ans = 0;
  rep(i, H.size()) rep(j, T + 1) chmax(ans, dp[i][j]);
  cout << ans << endl;
}

signed main() {
  while (cin >> N >> M >> T, N || M || T)
    solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

namespace SCC {
void visit(const vector<vector<int>> &G, vector<int> &vs, vector<int> &used,
           int v) {
  used[v] = true;
  for (auto u : G[v]) {
    if (!used[u])
      visit(G, vs, used, u);
  }
  vs.push_back(v);
}

void visit2(const vector<vector<int>> &T, vector<int> &used, vector<int> &comp,
            vector<int> &vec, int k, int v) {
  comp[v] = k;
  used[v] = true;
  vec.push_back(v);

  for (auto u : T[v]) {
    if (!used[u])
      visit2(T, used, comp, vec, k, u);
  }
}

void decompose(const vector<vector<int>> &G, vector<vector<int>> &H,
               vector<int> &comp) {
  vector<vector<int>> T(G.size());
  for (int i = 0; i < G.size(); i++) {
    for (auto v : G[i]) {
      T[v].push_back(i);
    }
  }
  comp.resize(G.size());

  vector<int> vs(G.size());
  vector<int> used(G.size());
  for (int i = 0; i < G.size(); i++) {
    if (!used[i])
      visit(G, vs, used, i);
  }
  reverse(vs.begin(), vs.end());
  fill(used.begin(), used.end(), 0);

  int K = 0;
  vector<vector<int>> S;
  for (auto v : vs) {
    if (!used[v]) {
      S.push_back(vector<int>());
      visit2(T, used, comp, S.back(), K++, v);
    }
  }

  H.resize(K);
  fill(used.begin(), used.end(), 0);
  for (int i = 0; i < K; i++) {
    for (auto v : S[i]) {
      for (auto u : G[v]) {
        if (used[comp[u]] || comp[v] == comp[u])
          continue;
        used[comp[u]] = true;
        H[comp[v]].push_back(comp[u]);
      }
    }
    for (auto v : H[i])
      used[v] = false;
  }
}
} // namespace SCC

int N, M, T;

void solve() {
  vint p(N), t(N), k(N);
  rep(i, N) cin >> p[i] >> t[i] >> k[i];

  vector<vint> G(N);
  vector<int> sel(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
    if (a == b)
      sel[i] = true;
  }

  vector<vint> H;
  vint comp;

  SCC::decompose(G, H, comp);

  vector<vpint> lis(H.size());

  vint cnt(H.size());
  for (int i = 0; i < N; i++)
    cnt[comp[i]]++;

  for (int i = 0; i < N; i++) {
    if (cnt[comp[i]] == 1 && !sel[i]) {
      lis[comp[i]].pb({p[i], t[i]});
    } else {
      for (int j = 1;; j <<= 1) {
        if (k[i] >= j) {
          lis[comp[i]].pb({p[i] * j, t[i] * j});
          k[i] -= j;
        } else {
          if (k[i])
            lis[comp[i]].pb({p[i] * k[i], t[i] * k[i]});
          break;
        }
      }
    }
  }

  vector<vint> dp(H.size(), vint(T + 1));

  for (int i = 0; i < H.size(); i++) {
    for (auto &w : lis[i]) {
      for (int j = T; j >= 0; j--) {
        if (j + w.se <= T)
          chmax(dp[i][j + w.se], dp[i][j] + w.fi);
      }
    }
    for (auto &u : H[i])
      rep(j, T + 1) chmax(dp[u][j], dp[i][j]);
  }

  int ans = 0;
  rep(i, H.size()) rep(j, T + 1) chmax(ans, dp[i][j]);
  cout << ans << endl;
}

signed main() {
  while (cin >> N >> M >> T, N || M || T)
    solve();
  return 0;
}