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

#include <bits/stdc++.h>
#define MAX_V 110
#define N 110
using namespace std;

/* ?????£?????????????§£ O(|V|+|E|) */
int V;                 //????????°
vector<int> G[MAX_V];  //??°???????????£??\\??????????????¨???
vector<int> rG[MAX_V]; //?????????????????????????????°??????
vector<int> vs;        //??°?????????????????????
bool used[MAX_V];      //?????§???????????????
int cmp[MAX_V];        //?±????????????£?????????????????????????????????

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++)
    if (!used[G[v][i]])
      dfs(G[v][i]);
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++)
    if (!used[rG[v][i]])
      rdfs(rG[v][i], k);
}

int scc() {
  memset(used, 0, sizeof(used));
  vs.clear();
  for (int v = 0; v < V; v++)
    if (!used[v])
      dfs(v);
  memset(used, 0, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--)
    if (!used[vs[i]])
      rdfs(vs[i], k++);
  return k;
}

int n, v[N], w[N], K[N], T;
int Max(int &a, int b) { return a = max(a, b); }

set<int> g[N];
vector<int> DP[N];
void update(vector<int> I, vector<int> &dp) {
  int col = cmp[I[0]];
  for (int i : I) {
    int num = g[cmp[i]].count(cmp[i]) ? K[i] : 1;
    for (int j = 1; num; j = min(num, j * 2)) { //???????????°
      num -= j;
      for (int k = T; k >= j * w[i]; k--) //?????¨?????????
        Max(dp[k], dp[k - j * w[i]] + j * v[i]);
    }
  }

  for (int nx : g[col])
    for (int j = 0; j <= T; j++)
      Max(DP[nx][j], dp[j]);
}

void init() {
  for (int i = 0; i < N; i++) {
    cmp[i] = 0;
    G[i].clear();
    rG[i].clear();
    g[i].clear();
    DP[i].clear();
    DP[i].resize(T + 1, 0);
  }
}

int main() {
  while (1) {
    int m;
    cin >> n >> m >> T;
    if (!n && !m && !T)
      break;
    init();

    for (int i = 0; i < n; i++)
      cin >> v[i] >> w[i] >> K[i];
    for (int i = 0, a, b; i < m; i++)
      cin >> a >> b, add_edge(a - 1, b - 1);
    V = n;
    V = scc();
    // for(int i=0;i<n;i++)cout<<cmp[i]<<" ";cout<<endl;

    for (int i = 0; i < n; i++)
      for (int j : G[i])
        g[cmp[i]].insert(cmp[j]);

    int ans = 0;
    for (int i = 0; i < V; i++) {
      vector<int> I;
      for (int j = 0; j < n; j++)
        if (cmp[j] == i)
          I.push_back(j);
      update(I, DP[i]);
    }
    for (int i = 0; i < V; i++)
      for (int j = 0; j <= T; j++)
        Max(ans, DP[i][j]);

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MAX_V 110
#define N 110
using namespace std;

/* ?????£?????????????§£ O(|V|+|E|) */
int V;                 //????????°
vector<int> G[MAX_V];  //??°???????????£??\\??????????????¨???
vector<int> rG[MAX_V]; //?????????????????????????????°??????
vector<int> vs;        //??°?????????????????????
bool used[MAX_V];      //?????§???????????????
int cmp[MAX_V];        //?±????????????£?????????????????????????????????

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++)
    if (!used[G[v][i]])
      dfs(G[v][i]);
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++)
    if (!used[rG[v][i]])
      rdfs(rG[v][i], k);
}

int scc() {
  memset(used, 0, sizeof(used));
  vs.clear();
  for (int v = 0; v < V; v++)
    if (!used[v])
      dfs(v);
  memset(used, 0, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--)
    if (!used[vs[i]])
      rdfs(vs[i], k++);
  return k;
}

int n, v[N], w[N], K[N], T;
int Max(int &a, int b) { return a = max(a, b); }

set<int> Idx;
set<int> g[N];
int calc() {
  int dp[20010] = {};

  for (int i : Idx) { //??????
    int num = g[cmp[i]].count(cmp[i]) ? K[i] : 1;
    for (int j = 1; num; j = min(num, j * 2)) { //???????????°
      num -= j;
      for (int k = T; k >= j * w[i]; k--) //?????¨?????????
        Max(dp[k], dp[k - j * w[i]] + j * v[i]);
    }
  }
  int res = 0;
  for (int i = 0; i <= T; i++)
    Max(res, dp[i]);
  return res;
}

int calc(int col) {
  int res = 0, flg = 1;
  for (int i = 0; i < n; i++)
    if (cmp[i] == col)
      Idx.insert(i);

  for (int nx : g[col])
    if (nx != col)
      res = max(res, calc(nx)), flg = 0;
  if (flg)
    res = calc();
  for (int i = 0; i < n; i++)
    if (cmp[i] == col)
      Idx.erase(i);
  return res;
}

int main() {
  while (1) {
    int m;
    cin >> n >> m >> T;
    if (!n && !m && !T)
      break;
    memset(cmp, 0, sizeof(cmp));
    for (int i = 0; i < N; i++)
      G[i].clear(), rG[i].clear(), Idx.clear(), g[i].clear();
    for (int i = 0; i < n; i++)
      cin >> v[i] >> w[i] >> K[i];
    for (int i = 0, a, b; i < m; i++)
      cin >> a >> b, add_edge(a - 1, b - 1);
    V = n;
    V = scc();

    bool start[N] = {};
    for (int i = 0; i < n; i++)
      for (int j : G[i])
        g[cmp[i]].insert(cmp[j]), start[cmp[j]] |= cmp[i] != cmp[j];

    int ans = 0;
    memset(used, 0, sizeof(used));
    for (int i = 0; i < n; i++) {
      if (used[cmp[i]] || start[cmp[i]])
        continue;
      used[cmp[i]] = 1;
      Max(ans, calc(cmp[i]));
    }
    cout << ans << endl;
  }
  return 0;
}