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

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

typedef vector<double> vec;
typedef vector<vec> mat;

const double EPS = 1e-8;

vec gauss_jordan(const mat &A, const vec &b, int k) {
  int n = A.size();
  mat B(n, vec(n + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j];
  for (int i = 0; i < n; i++)
    B[i][n] = b[i];

  for (int i = 0; i < k; i++) {
    int pivot = i;
    for (int j = i + 1; j < n; j++)
      if (fabs(B[j][i]) > fabs(B[pivot][i]))
        pivot = j;
    swap(B[i], B[pivot]);

    // ティツァツ」テ」ツ?古」ツ?ェテ」ツ??」ツ?凝」ツ??」ツ?づ」ツ?」テ」ツ?ヲテ」ツつづ、ツクツ?ヲツ?湘」ツ?ァテ」ツ?ッテ」ツ?ェテ」ツ??」ツ??
    if (fabs(B[i][i]) < EPS)
      return vec();

    for (int j = i + 1; j <= n; j++)
      B[i][j] /= B[i][i];
    for (int j = 0; j < n; j++)
      if (i != j)
        for (int k = i + 1; k <= n; k++)
          B[j][k] -= B[j][i] * B[i][k];
  }
  vec x(n);
  for (int i = 0; i < n; i++)
    x[i] = B[i][n];
  return x;
}

int X[111][111];
int C[111][111];
int P, S;

bool check(const vec &v) {
  for (int i = 0; i <= P; i++)
    for (int j = 0; j <= S; j++)
      if (C[i][j] != -1)
        X[i][j] = (int)round(v[C[i][j]]);
  for (int i = 0; i <= P; i++) {
    int sum = 0;
    for (int j = 0; j < S; j++)
      sum += X[i][j];
    if (sum != X[i][S])
      return false;
  }
  for (int j = 0; j <= S; j++) {
    int sum = 0;
    for (int i = 0; i < P; i++)
      sum += X[i][j];
    if (sum != X[P][j])
      return false;
  }
  return true;
}

void view(vec &v) {
  for (auto a : v)
    cout << a << " ";
  cout << endl;
}

const int INF = (1 << 29);
int main() {

  while (cin >> P && P) {
    cin >> S;
    memset(C, -1, sizeof(C));
    memset(X, 0, sizeof(X));
    int cnt = 0;
    for (int i = 0; i <= P; i++) {
      for (int j = 0; j <= S; j++) {
        string s;
        cin >> s;
        if (s == "?")
          C[i][j] = cnt++;
        else
          X[i][j] = stoi(s);
      }
    }

    static int ttt = 0;
    if (ttt++)
      cout << endl;

    int n = P + S + 1;

    if (cnt > n) {
      cout << "NO" << endl;
      continue;
    }

    mat M = mat(n, vec(n));
    vec V = vec(n);
    set<vec> F;
    int k = 0;
    for (int i = 0; i < P; i++) {
      V[k] = 0;
      for (int j = 0; j < S; j++) {
        if (C[i][j] != -1)
          M[k][C[i][j]] = 1;
        else
          V[k] -= X[i][j];
      }
      V[k] += X[i][S];
      k++;
    }

    for (int j = 0; j < S; j++) {
      V[k] = 0;
      for (int i = 0; i < P; i++) {
        if (C[i][j] != -1)
          M[k][C[i][j]] = 1;
        else
          V[k] -= X[i][j];
      }
      V[k] += X[P][j];
      k++;
    }

    for (int i = 0; i < P; i++)
      for (int j = 0; j < S; j++) {
        if (C[i][j] != -1)
          M[k][C[i][j]] = 1;
        else
          V[k] -= X[i][j];
      }
    V[k] += X[P][S];
    k++;

    vec res = gauss_jordan(M, V, cnt);
    if (res.empty())
      cout << "NO" << endl;
    else {
      if (check(res)) {
        for (int i = 0; i < cnt; i++)
          cout << (int)round(res[i]) << endl;
      } else
        cout << "NO" << endl;
    }
  }
}

Error: -6
ba286ef7-9ee4-4907-9268-ac2cf01d1f18.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00698/C++/s246821118.cpp:97: int main(): Assertion `!(!f && !V[k])' failed.


Fixed submission:

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

typedef vector<double> vec;
typedef vector<vec> mat;

const double EPS = 1e-9;

vec gauss_jordan(const mat &A, const vec &b) {
  int n = A.size();
  mat B(n, vec(n + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j];
  for (int i = 0; i < n; i++)
    B[i][n] = b[i];

  for (int i = 0; i < n; i++) {
    int pivot = i;
    for (int j = i + 1; j < n; j++)
      if (fabs(B[j][i]) > fabs(B[pivot][i]))
        pivot = j;
    swap(B[i], B[pivot]);

    // ティツァツ」テ」ツ?古」ツ?ェテ」ツ??」ツ?凝」ツ??」ツ?づ」ツ?」テ」ツ?ヲテ」ツつづ、ツクツ?ヲツ?湘」ツ?ァテ」ツ?ッテ」ツ?ェテ」ツ??」ツ??
    if (fabs(B[i][i]) < EPS)
      return vec();

    for (int j = i + 1; j <= n; j++)
      B[i][j] /= B[i][i];
    for (int j = 0; j < n; j++)
      if (i != j)
        for (int k = i + 1; k <= n; k++)
          B[j][k] -= B[j][i] * B[i][k];
  }
  vec x(n);
  for (int i = 0; i < n; i++)
    x[i] = B[i][n];
  return x;
}

int X[111][111];
int C[111][111];
int P, S;

bool check(const vec &v) {

  for (int i = 0; i <= P; i++)
    for (int j = 0; j <= S; j++)
      if (C[i][j] != -1)
        X[i][j] = (int)round(v[C[i][j]]);

  for (int i = 0; i <= P; i++) {
    int sum = 0;
    for (int j = 0; j < S; j++)
      sum += X[i][j];
    if (sum != X[i][S])
      return false;
  }
  for (int j = 0; j <= S; j++) {
    int sum = 0;
    for (int i = 0; i < P; i++)
      sum += X[i][j];
    if (sum != X[P][j])
      return false;
  }
  return true;
}

void view(vec &v) {
  for (auto a : v)
    cout << a << " ";
  cout << endl;
}

const int INF = (1 << 29);
int main() {

  while (cin >> P && P) {
    cin >> S;
    memset(C, -1, sizeof(C));
    memset(X, 0, sizeof(X));
    int cnt = 0;
    for (int i = 0; i <= P; i++) {
      for (int j = 0; j <= S; j++) {
        string s;
        cin >> s;
        if (s == "?")
          C[i][j] = cnt++;
        else
          X[i][j] = stoi(s);
      }
    }

    mat M = mat(cnt, vec(cnt));
    vec V = vec(cnt);
    set<vec> F;
    int k = 0;
    for (int i = 0; i <= P; i++) {
      if (k == cnt)
        break;
      bool f = false;
      V[k] = 0;
      M[k] = vec(cnt, 0);
      for (int j = 0; j < S; j++) {
        if (C[i][j] != -1)
          M[k][C[i][j]] = f = 1;
        else
          V[k] -= X[i][j];
      }
      if (C[i][S] != -1)
        M[k][C[i][S]] = f = -1;
      else
        V[k] += X[i][S];
      assert(!(!f && !V[k]));
      if (f && F.count(M[k]) == 0)
        F.insert(M[k++]);
    }

    for (int j = 0; j <= S; j++) {
      if (k == cnt)
        break;
      bool f = false;
      V[k] = 0;
      M[k] = vec(cnt, 0);
      for (int i = 0; i < P; i++) {
        if (C[i][j] != -1)
          M[k][C[i][j]] = f = 1;
        else
          V[k] -= X[i][j];
      }
      if (C[P][j] != -1)
        M[k][C[P][j]] = f = -1;
      else
        V[k] += X[P][j];

      assert(!(!f && !V[k]));

      if (f && F.count(M[k]) == 0)
        F.insert(M[k++]);
    }

    static int ttt = 0;
    if (ttt++)
      cout << endl;
    vec res;
    if (k == cnt)
      res = gauss_jordan(M, V);
    if (res.empty())
      cout << "NO" << endl;
    else {
      if (check(res)) {
        for (double v : res) {
          cout << (int)round(v) << endl;
        }
      } else
        cout << "NO" << endl;
    }
  }
}