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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

bool b[505][505];
ll lists[505];

/* ---- bitDP part ----*/
void init_bitDP(int n, int m) {
  /* create bit list */
  for (int i = 0; i < n; i++) {
    lists[i] = 0;
    for (int j = 0; j < m; j++) {
      assert(i < 505 && j < 505);
      if (b[i][j])
        lists[i] |= (1 << j);
    }
  }
  // cout<<"end bitDP: "<<endl;
}

int bitDP(int n, int m) {
  vector<vector<int>> dp(n + 1, vector<int>((1 << m), -1));
  // cout<<"dp"<<endl;
  for (int i = 0; i < n; i++) {
    // cout<<"init: "<<i<<endl;
    for (int j = 0; j < (1 << m); j++) {
      // assert(i<n&&j<(1<<m));
      dp[i][j] = -INF;
    }
  }

  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (1 << m); j++) {
      assert(i < n + 5);
      assert(j < (1 << m));
      // cout<<i<<" "<<j<<endl;
      dp[i][j] = max(dp[i - 1][j], dp[i - 1][j ^ lists[i - 1]] + 1);
    }
  }
  return dp[n][0];
}

/* ---- dfs part ---- */
void init_dfs(bool used[]) {
  for (int i = 0; i < 505; i++) {
    used[i] = false;
  }
}

int dfs_calc(int n, int m, bool used[]) {
  int ret = 0;
  for (int i = 0; i < n; i++)
    ret += used[i];
  for (int i = 0; i < m; i++) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      if (used[j])
        sum += b[j][i];
    }
    if (sum % 2)
      return -1;
  }
  // cout<<"  OK! ___ "<<ret<<endl;
  return ret;
}

int dfs(int n, int m, int cnt, bool used[]) {
  int ret = 0;
  if (n == cnt) {
    // cout<<dfs_calc(n,m)<<endl;
    return dfs_calc(n, m, used);
  }

  used[cnt] = true;
  ret = max(ret, dfs(n, m, cnt + 1, used));
  used[cnt] = false;
  ret = max(ret, dfs(n, m, cnt + 1, used));

  return ret;
}

/* ---- main ---- */
void solve(int n, int m) {
  char c;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c;
      b[i][j] = c - '0';
    }
  }

  if (n <= m) {
    bool used[505];
    // cout<<"dfs"<<endl;
    init_dfs(used);
    // cout<<"dfs_in"<<endl;
    cout << dfs(n, m, 0, used) << endl;
  } else {
    // cout<<"bitDP"<<endl;
    init_bitDP(n, m);
    // cout<<"bitDP_in"<<endl;
    cout << bitDP(n, m) << endl;
    ;
  }
  // cout<<"debug; "<<endl;
}

int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n | m) {
    // cout<<n<<" "<<m<<endl;
    solve(n, m);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

bool used[505];
bool b[505][505];
ll lists[505];

/* ---- bitDP part ----*/
void init_bitDP(int n, int m) {
  /* create bit list */
  // if(505<=n)cout<<"n: "<<n<<endl;
  for (int i = 0; i < n; i++) {
    lists[i] = 0;
    for (int j = 0; j < m; j++) {
      if (b[i][j])
        lists[i] |= (1 << j);

      // assert(505<=i);
    }
  }
}

int bitDP(int n, int m) {
  int dp[n + 5][(1 << (m + 1))];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (1 << m); j++) {
      dp[i][j] = -INF;
    }
  }

  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (1 << m); j++) {
      dp[i][j] = max(dp[i - 1][j], dp[i - 1][j ^ lists[i - 1]] + 1);
    }
  }

  return dp[n][0];
}

/* ---- dfs part ---- */
void init_dfs() {
  for (int i = 0; i < 505; i++) {
    used[i] = false;
  }
}

int dfs_calc(int n, int m) {
  int ret = 0;
  for (int i = 0; i < n; i++)
    ret += used[i];

  for (int i = 0; i < m; i++) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += b[j][i];
    }
    if (sum % 2)
      return -1;
  }
  return ret;
}

int dfs(int n, int m, int cnt) {
  int ret = 0;
  if (n == cnt) {
    // cout<<dfs_calc(n,m)<<endl;
    return dfs_calc(n, m);
  }

  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      used[i] = true;
      ret = max(ret, dfs(n, m, cnt + 1));
      used[i] = false;
      // ret=max(ret,dfs(n,m,cnt+1));
    }
  }
  return ret;
}

/* ---- main ---- */
void solve(int n, int m) {
  char c;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c;
      b[i][j] = c - '0';
    }
  }

  if (n <= m) {
    // cout<<"dfs"<<endl;
    init_dfs();
    // cout<<"dfs_in"<<endl;
    cout << dfs(n, m, 0) << endl;
  } else {
    // cout<<"bitDP"<<endl;
    init_bitDP(n, m);
    // cout<<"bitDP_in"<<endl;
    cout << bitDP(n, m) << endl;
    ;
  }
  // cout<<"debug; "<<endl;
}

int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n | m) {
    solve(n, m);
  }
  return 0;
}