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

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 100000;

int N, M;
int C[MAX_N];
vector<int> g[MAX_N], r[MAX_N];

bool v[MAX_N], bipartite[MAX_N];
int comp[MAX_N], sum[2][MAX_N], cc[MAX_N];
vector<int> order;
vector<int> decomp[MAX_N];

int dp[2][MAX_N];

void dfs(int idx) {
  if (v[idx])
    return;
  v[idx] = true;
  for (auto &to : g[idx])
    dfs(to);
  order.push_back(idx);
}

void rdfs(int idx, int ptr) {
  if (~comp[idx])
    return;
  comp[idx] = ptr;
  decomp[ptr].push_back(idx);
  for (auto &to : r[idx])
    rdfs(to, ptr);
}

bool dfs2(int idx, bool color, int ptr) {
  if (~cc[idx])
    return (cc[idx] != color);
  cc[idx] = color;
  sum[color][ptr] += C[idx];
  bool flag = false;
  for (auto &to : g[idx]) {
    if (comp[to] == comp[idx])
      flag |= dfs2(to, !color, ptr);
  }
  return (flag);
}

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    scanf("%d", &C[i]);
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    g[a].push_back(b);
    r[b].push_back(a);
  }

  for (int i = 0; i < N; i++)
    dfs(i);
  reverse(begin(order), end(order));
  int ptr = 0;
  memset(comp, -1, sizeof(comp));
  for (auto &u : order) {
    if (comp[u] == -1)
      rdfs(u, ptr++);
  }

  memset(cc, -1, sizeof(cc));
  for (int i = 0; i < ptr; i++) {
    bipartite[i] = dfs2(decomp[i][0], false, i);
  }

  memset(dp, -1, sizeof(dp));
  dp[cc[0]][comp[0]] = sum[cc[0]][comp[0]];

  int ret = 0;
  for (int i = 0; i < ptr; i++) {

    if (bipartite[i]) {
      if (dp[0][i] == -1 && dp[1][i] == -1)
        continue;
      int beet = 0;
      if (~dp[0][i])
        beet = max(beet, dp[0][i] + sum[1][i]);
      if (~dp[1][i])
        beet = max(beet, dp[1][i] + sum[0][i]);
      dp[0][i] = dp[1][i] = beet;
    }

    for (auto &curr : decomp[i]) {
      for (int j = 0; j < 2; j++) {
        if (dp[j][i] == -1)
          continue;
        for (auto &next : g[curr]) {
          if (comp[curr] == comp[next])
            continue;
          int real = cc[curr] ^ cc[next] ^ j ^ 1;
          dp[real][comp[next]] =
              max(dp[real][comp[next]], dp[j][i] + sum[real][comp[next]]);
        }
      }
    }

    ret = max(ret, dp[0][i]);
    ret = max(ret, dp[1][i]);
  }

  printf("%d\\n", ret);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 100000;

int N, M;
int C[MAX_N];
vector<int> g[MAX_N], r[MAX_N];

bool v[MAX_N], bipartite[MAX_N];
int comp[MAX_N], sum[2][MAX_N], cc[MAX_N];
vector<int> order;
vector<int> decomp[MAX_N];

int dp[2][MAX_N];

void dfs(int idx) {
  if (v[idx])
    return;
  v[idx] = true;
  for (auto &to : g[idx])
    dfs(to);
  order.push_back(idx);
}

void rdfs(int idx, int ptr) {
  if (~comp[idx])
    return;
  comp[idx] = ptr;
  decomp[ptr].push_back(idx);
  for (auto &to : r[idx])
    rdfs(to, ptr);
}

bool dfs2(int idx, bool color, int ptr) {
  if (~cc[idx])
    return (cc[idx] != color);
  cc[idx] = color;
  sum[color][ptr] += C[idx];
  bool flag = false;
  for (auto &to : g[idx]) {
    if (comp[to] == comp[idx])
      flag |= dfs2(to, !color, ptr);
  }
  return (flag);
}

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    scanf("%d", &C[i]);
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    g[a].push_back(b);
    r[b].push_back(a);
  }

  for (int i = 0; i < N; i++)
    dfs(i);
  reverse(begin(order), end(order));
  int ptr = 0;
  memset(comp, -1, sizeof(comp));
  for (auto &u : order) {
    if (comp[u] == -1)
      rdfs(u, ptr++);
  }

  memset(cc, -1, sizeof(v));
  for (int i = 0; i < ptr; i++) {
    bipartite[i] = dfs2(decomp[i][0], false, i);
  }
  for (int i = 0; i < N; i++) {
    assert(cc[i] >= 0);
  }

  memset(dp, -1, sizeof(dp));
  dp[cc[0]][comp[0]] = sum[cc[0]][comp[0]];

  int ret = 0;
  for (int i = 0; i < ptr; i++) {

    if (bipartite[i]) {
      if (dp[0][i] == -1 && dp[1][i] == -1)
        continue;
      int beet = 0;
      if (~dp[0][i])
        beet = max(beet, dp[0][i] + sum[1][i]);
      if (~dp[1][i])
        beet = max(beet, dp[1][i] + sum[0][i]);
      dp[0][i] = dp[1][i] = beet;
    }

    for (auto &curr : decomp[i]) {
      for (int j = 0; j < 2; j++) {
        if (dp[j][i] == -1)
          continue;
        for (auto &next : g[curr]) {
          if (comp[curr] == comp[next])
            continue;
          if (cc[curr] == -1 || cc[next] == -1)
            continue;
          int real = cc[curr] ^ cc[next] ^ j ^ 1;
          dp[real][comp[next]] =
              max(dp[real][comp[next]], dp[j][i] + sum[real][comp[next]]);
        }
      }
    }

    ret = max(ret, dp[0][i]);
    ret = max(ret, dp[1][i]);
  }

  printf("%d\\n", ret);
}