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

#include <bits/stdc++.h>
#define N 2010
using namespace std;
const int INF = 1e9;
int K, C[N][N], NG[N][N];
vector<int> G[N];
int Max(int &a, int b) { return a = max(a, b); }

int visited[N];
vector<int> g;
void dfs(int pos, int pre) {
  g.push_back(pos);
  if (visited[pos]++)
    return;
  for (int nx : G[pos])
    if (nx != pre) {
      dfs(nx, pos);
      break;
    }
}

int dp1[2][2][N][N]; // flg,used,pos,cnt
void update(int flg, int i, int cnt) {
  int pos = g[i];
  int pre = g[i - 1];

  // pos???????????????
  if (!(pos == g[0] &&
        flg ==
            1)) { //??????????????§????????????????????¨??????????????????????????£?????\\???
    Max(dp1[flg][0][i][cnt],
        dp1[flg][0][i - 1][cnt]); //??????????????£?????????
    Max(dp1[flg][0][i][cnt], dp1[flg][1][i - 1][cnt]); //??????????????£???
  }

  if (flg == 0 && pos == g[0])
    return; // start??§?????????start????????£??????????????????????????¨?????????????????????
  int f = !(
      pos == g[0] &&
      flg ==
          1); // ??????????????§???????????????????????§????????´???(flg==1)???cost???0?????????

  // pos????????????
  Max(dp1[flg][1][i][cnt + f],
      dp1[flg][0][i - 1][cnt]); //??????????????£?????????
  if (NG[pre][pos] == 0)
    Max(dp1[flg][1][i][cnt + f],
        dp1[flg][1][i - 1][cnt] + C[pre][pos]); //???????????£???
}

vector<vector<int>> D;
void DP1() {

  int n = g.size();
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k <= n; k++)
        for (int l = 0; l <= n; l++)
          dp1[i][j][k][l] = -INF;

  dp1[0][0][0][0] = 0;
  dp1[1][1][0][1] = 0;

  for (int i = 1; i < n; i++)
    for (int j = 0; j <= n; j++)
      update(0, i, j), update(1, i, j);

  vector<int> tmp;
  for (int i = 0; i <= n; i++) {
    int a = max(dp1[0][0][n - 1][i], dp1[0][1][n - 1][i]);
    int b = max(dp1[1][0][n - 1][i], dp1[1][1][n - 1][i]);
    tmp.push_back(max(a, b));
  }
  D.push_back(tmp);
}

int DP2() {
  int dp2[N]; // 1??????????????§????????????????????????
  for (int i = 0; i <= K; i++)
    dp2[i] = -INF; //?????????

  int n = D.size();
  dp2[0] = 0;
  for (int i = 0; i < n; i++) //??£??????????????????????????§?????????
    for (int k = K, m = D[i].size(); k >= 0; k--) //???????????????????????????
      for (int j = 0; j < m && k + j <= K; j++)   //?¬?????????????????????????
        Max(dp2[k + j], dp2[k] + D[i][j]);

  return dp2[K];
}

int main() {
  int n, m;
  cin >> n >> m >> K;

  for (int i = 0, a, b, c; i < m; i++) {
    cin >> a >> b >> c;
    a--, b--;
    C[a][b] = C[b][a] = c;
    if (c == 0)
      NG[a][b] = NG[b][a] = 1;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  //?¬???°????????\\???????????????????????????
  for (int i = 0; i < n; i++)
    if (G[i].size() <= 1 && visited[i] == 0)
      g.clear(), dfs(i, -1), DP1();

  //?¬???°????????????????????????
  for (int i = 0; i < n; i++)
    if (visited[i] == 0)
      g.clear(), dfs(i, -1), DP1();

  int ans = DP2();
  if (ans < -1e8)
    cout << "Impossible" << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 2001
using namespace std;
const int INF = 1e9;
int K, C[N][N], NG[N][N];
vector<int> G[N];
int Max(int &a, int b) { return a = max(a, b); }

int visited[N];
vector<int> g;
void dfs(int pos, int pre) {
  g.push_back(pos);
  if (visited[pos]++)
    return;
  for (int nx : G[pos])
    if (nx != pre) {
      dfs(nx, pos);
      break;
    }
}

int dp1[2][2][N][N]; // flg,used,pos,cnt
void update(int flg, int i, int cnt) {
  int pos = g[i];
  int pre = g[i - 1];

  // pos???????????????
  if (!(pos == g[0] &&
        flg ==
            1)) { //??????????????§????????????????????¨??????????????????????????£?????\\???
    Max(dp1[flg][0][i][cnt],
        dp1[flg][0][i - 1][cnt]); //??????????????£?????????
    Max(dp1[flg][0][i][cnt], dp1[flg][1][i - 1][cnt]); //??????????????£???
  }

  if (flg == 0 && pos == g[0])
    return; // start??§?????????start????????£??????????????????????????¨?????????????????????
  int f = !(
      pos == g[0] &&
      flg ==
          1); // ??????????????§???????????????????????§????????´???(flg==1)???cost???0?????????

  // pos????????????
  Max(dp1[flg][1][i][cnt + f],
      dp1[flg][0][i - 1][cnt]); //??????????????£?????????
  if (NG[pre][pos] == 0)
    Max(dp1[flg][1][i][cnt + f],
        dp1[flg][1][i - 1][cnt] + C[pre][pos]); //???????????£???
}

vector<vector<int>> D;
void DP1() {

  int n = g.size();
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k <= n; k++)
        for (int l = 0; l <= n; l++)
          dp1[i][j][k][l] = -INF;

  dp1[0][0][0][0] = 0;
  dp1[1][1][0][1] = 0;

  for (int i = 1; i < n; i++)
    for (int j = 0; j <= n; j++)
      update(0, i, j), update(1, i, j);

  vector<int> tmp;
  for (int i = 0; i <= n; i++) {
    int a = max(dp1[0][0][n - 1][i], dp1[0][1][n - 1][i]);
    int b = max(dp1[1][0][n - 1][i], dp1[1][1][n - 1][i]);
    tmp.push_back(max(a, b));
  }
  D.push_back(tmp);
}

int DP2() {
  int dp2[N]; // 1??????????????§????????????????????????
  for (int i = 0; i <= K; i++)
    dp2[i] = -INF; //?????????

  int n = D.size();
  dp2[0] = 0;
  for (int i = 0; i < n; i++) //??£??????????????????????????§?????????
    for (int k = K, m = D[i].size(); k >= 0; k--) //???????????????????????????
      for (int j = 0; j < m && k + j <= K; j++)   //?¬?????????????????????????
        Max(dp2[k + j], dp2[k] + D[i][j]);

  return dp2[K];
}

int main() {
  int n, m;
  cin >> n >> m >> K;

  for (int i = 0, a, b, c; i < m; i++) {
    cin >> a >> b >> c;
    a--, b--;
    C[a][b] = C[b][a] = c;
    if (c == 0)
      NG[a][b] = NG[b][a] = 1;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  //?¬???°????????\\???????????????????????????
  for (int i = 0; i < n; i++)
    if (G[i].size() <= 1 && visited[i] == 0)
      g.clear(), dfs(i, -1), DP1();

  //?¬???°????????????????????????
  for (int i = 0; i < n; i++)
    if (visited[i] == 0)
      g.clear(), dfs(i, -1), DP1();

  int ans = DP2();
  if (ans < -1e7)
    cout << "Impossible" << endl;
  else
    cout << ans << endl;
  return 0;
}