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

#include <bits/stdc++.h>
using namespace std;
typedef vector<double> vec;
typedef vector<vec> mat;
double eps = 1e-8;

void gauss_jordan(mat &A) {
  int n = A.size();
  int m = A[0].size();

  int rank = 0;

  vector<bool> flg(n, false);

  for (int i = 0; i < n; i++) {
    int pivot = i;
    for (int j = 0; j < n; j++) {
      if (flg[j])
        continue;
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    }

    swap(A[pivot], A[i]);

    if (abs(A[i][i]) < eps)
      continue;

    rank++;
    flg[i] = true;

    double val = A[i][i];
    for (int j = 0; j < m; j++)
      A[i][j] /= val;

    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      double va = A[j][i];

      for (int k = 0; k < m; k++) {
        A[j][k] -= A[i][k] * va;
      }
    }
  }
  /*
  cout<<"rank = "<<rank<<endl;
  cout<<" n = "<<n<<endl;
  cout<<" m = "<<m<<endl;

  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      printf("%3d",(int)A[i][j]);
    }
    cout<<endl;
  }

  */
  cout << m - rank << endl;
}

int N, M;
int G[500][500];

int main() {
  memset(G, -1, sizeof(G));
  mat A;
  cin >> N >> M;
  if (M == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a][b] = i;
  }

  for (int i = 0; i < N; i++) {
    bool flg = false;
    vec B(M, 0);
    for (int j = 0; j < N; j++) {
      if (G[i][j] == -1)
        continue;
      int id = G[i][j];
      B[id] = 1;
      flg = true;
    }
    for (int j = 0; j < N; j++) {
      if (G[j][i] == -1)
        continue;
      int id = G[j][i];
      B[id] = -1;
      flg = true;
    }
    if (flg)
      A.push_back(B);
  }

  while (A.size() < A[0].size()) {
    vec B(M, 0);
    A.push_back(B);
  }

  gauss_jordan(A);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<double> vec;
typedef vector<vec> mat;
double eps = 1e-8;

void gauss_jordan(mat &A) {
  int n = A.size();
  int m = A[0].size();

  int rank = 0;

  vector<bool> flg(n, false);

  for (int i = 0; i < n; i++) {
    int pivot = i;
    for (int j = 0; j < n; j++) {
      if (flg[j])
        continue;
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    }

    swap(A[pivot], A[i]);

    if (abs(A[i][i]) < eps)
      continue;

    rank++;
    flg[i] = true;

    double val = A[i][i];
    for (int j = 0; j < m; j++)
      A[i][j] /= val;

    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      double va = A[j][i];
      for (int k = 0; k < m; k++)
        A[j][k] -= A[i][k] * va;
    }
  }
  /*
  cout<<"rank = "<<rank<<endl;
  cout<<" n = "<<n<<endl;
  cout<<" m = "<<m<<endl;

  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      printf("%3d",(int)A[i][j]);
    }
    cout<<endl;
  }

  */
  cout << m - rank << endl;
}

int N, M;
int G[500][500];

int main() {
  memset(G, -1, sizeof(G));
  mat A;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a][b] = i;
  }

  for (int i = 0; i < N; i++) {
    vec B(M, 0);
    for (int j = 0; j < N; j++) {
      if (G[i][j] == -1)
        continue;
      int id = G[i][j];
      B[id] = 1;
    }
    for (int j = 0; j < N; j++) {
      if (G[j][i] == -1)
        continue;
      int id = G[j][i];
      B[id] = -1;
    }
    A.push_back(B);
  }

  while (A.size() < A[0].size()) {
    vec B(M, 0);
    A.push_back(B);
  }
  gauss_jordan(A);
  return 0;
}