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

#include "bits/stdc++.h"
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)

#define MAX_V 200100
vector<int> G[MAX_V];
vector<int> rG[MAX_V];
vector<int> each_scc[MAX_V];

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

void dfs(int v, bool used[], vector<int> &vs) {
  used[v] = true;
  rep(i, G[v].size()) {
    if (!used[G[v][i]])
      dfs(G[v][i], used, vs);
  }
  vs.push_back(v);
}

void rdfs(int v, int k, vector<int> &sccs, vector<int> &cmp, bool used[]) {
  used[v] = true;
  sccs.push_back(v);
  cmp[v] = k;
  rep(i, rG[v].size()) {
    if (!used[rG[v][i]])
      rdfs(rG[v][i], k, sccs, cmp, used);
  }
}

int scc(int V, vector<int> &cmp) {
  vector<int> vs;
  bool used[MAX_V];
  fill(used, used + MAX_V, false);
  vs.clear();
  rep(v, V) {
    if (!used[v])
      dfs(v, used, vs);
  }
  fill(used, used + MAX_V, false);
  int k = 0;
  int cnt = 0;
  for (int i = vs.size() - 1; i >= 0; i--) {
    vector<int> sccs;
    if (!used[vs[i]]) {
      rdfs(vs[i], k++, sccs, cmp, used);
      each_scc[cnt++] = sccs;
    }
  }
  return k;
}

map<ll, ll> memo;
vector<int> nG[MAX_V];
int dfs(int cur, int prev, vector<int> &cost) {
  int ret = cost[cur];
  int add = 0;
  if (memo.count(cur) > 0)
    return memo[cur];
  for (auto &e : nG[cur]) {
    if (e == prev)
      continue;
    add = max(add, dfs(e, cur, cost));
  }
  return memo[cur] = ret + add;
}

int f(vector<int> &list, vector<int> &cost, int v) {
  int ret = 0;
  for (auto &e : list) {
    if (e >= v)
      continue;
    ret += cost[e];
  }
  return ret;
}

int main() {
  int v, e;
  cin >> v >> e;
  vector<int> c(v);
  rep(i, v) cin >> c[i];
  rep(i, e) {
    int a, b;
    cin >> a >> b;
    add_edge(a, v + b);
    add_edge(v + a, b);
  }

  vector<int> cmp(v * 2);
  int k = scc(2 * v, cmp);

  //    set<pii> st;

  rep(i, k) {
    set<int> st;
    for (auto &e : each_scc[i]) {
      for (auto &to : G[e]) {
        if (st.count(cmp[to]) != 0)
          continue;
        if (i == cmp[to])
          continue;
        st.insert(cmp[to]);
        nG[i].pb(cmp[to]);
      }
    }
  }

  vector<int> nc(k);
  rep(i, k) { nc[i] = f(each_scc[i], c, v); }
  cout << dfs(cmp[0], -1, nc) << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)

#define MAX_V 200100
vector<int> G[MAX_V];
vector<int> rG[MAX_V];
vector<int> each_scc[MAX_V];

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

void dfs(int v, bool used[], vector<int> &vs) {
  used[v] = true;
  rep(i, G[v].size()) {
    if (!used[G[v][i]])
      dfs(G[v][i], used, vs);
  }
  vs.push_back(v);
}

void rdfs(int v, int k, vector<int> &sccs, vector<int> &cmp, bool used[]) {
  used[v] = true;
  sccs.push_back(v);
  cmp[v] = k;
  rep(i, rG[v].size()) {
    if (!used[rG[v][i]])
      rdfs(rG[v][i], k, sccs, cmp, used);
  }
}

int scc(int V, vector<int> &cmp) {
  vector<int> vs;
  bool used[MAX_V];
  fill(used, used + MAX_V, false);
  vs.clear();
  rep(v, V) {
    if (!used[v])
      dfs(v, used, vs);
  }
  fill(used, used + MAX_V, false);
  int k = 0;
  int cnt = 0;
  for (int i = vs.size() - 1; i >= 0; i--) {
    vector<int> sccs;
    if (!used[vs[i]]) {
      rdfs(vs[i], k++, sccs, cmp, used);
      each_scc[cnt++] = sccs;
    }
  }
  return k;
}

vector<int> nG[MAX_V];

bool Used[MAX_V];
int dfs(int cur, int prev, vector<int> &cost) {
  assert(!Used[cur]);
  Used[cur] = true;
  int ret = cost[cur];
  int add = 0;
  for (auto &e : nG[cur]) {
    if (e == prev)
      continue;
    add = max(add, dfs(e, cur, cost));
  }
  return ret + add;
}

int f(vector<int> &list, vector<int> &cost, int v) {
  int ret = 0;
  for (auto &e : list) {
    if (e >= v)
      continue;
    ret += cost[e];
  }
  return ret;
}

int main() {
  int v, e;
  cin >> v >> e;
  vector<int> c(v);
  rep(i, v) cin >> c[i];
  rep(i, e) {
    int a, b;
    cin >> a >> b;
    add_edge(a, v + b);
    add_edge(v + a, b);
  }

  vector<int> cmp(v * 2);
  int k = scc(2 * v, cmp);

  //    set<pii> st;

  rep(i, k) {
    set<int> st;
    for (auto &e : each_scc[i]) {
      for (auto &to : G[e]) {
        if (st.count(cmp[to]) != 0)
          continue;
        if (i == cmp[to])
          continue;
        st.insert(cmp[to]);
        nG[i].pb(cmp[to]);
      }
    }
  }

  vector<int> nc(k);
  rep(i, k) { nc[i] = f(each_scc[i], c, v); }
  cout << dfs(cmp[0], -1, nc) << endl;
}