Problem p03691 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int(i) = 0; (i) < (ll)(n); ++(i))
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) (x = min(x, y))
#define chmax(x, y) (x = max(x, y))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
#define N 401
#define M 100010
#define N2 (N / 64 + 1)
ll n, m, x[M], y[M], k[N][M];
ull k2[N][N2];
int main() {
  cin >> n >> m;
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  reverse(x, x + m);
  reverse(y, y + m);
  rep(i, n) {
    k[i][i] = 1;
    rep(j, m) {
      if (k[i][x[j]] && k[i][y[j]]) {
        k[i][i] = 2;
        break;
      } else if (k[i][x[j]])
        k[i][y[j]] = 1;
      else if (k[i][y[j]])
        k[i][x[j]] = 1;
    }
    if (k[i][i] == 2) {
      mset(k2[i], -1);
      continue;
    }
    rep(j, n) if (k[i][j]) k2[i][j / 64] |= 1ull << (j % 64);
  }
  ll res = 0;
  rep(i, n) rep(j, i) {
    int r = 1;
    rep(l, n / 64 + 1) {
      if (k2[i][l] & k2[j][l]) {
        r = 0;
        break;
      }
    }
    res += r;
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int(i) = 0; (i) < (ll)(n); ++(i))
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) (x = min(x, y))
#define chmax(x, y) (x = max(x, y))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
#define N 401
#define M 100010
#define N2 (N / 64 + 1)
ll n, m, x[M], y[M], k[N][N];
ull k2[N][N2];
int main() {
  cin >> n >> m;
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  reverse(x, x + m);
  reverse(y, y + m);
  rep(i, n) {
    k[i][i] = 1;
    rep(j, m) {
      if (k[i][x[j]] && k[i][y[j]]) {
        k[i][i] = 2;
        break;
      } else if (k[i][x[j]])
        k[i][y[j]] = 1;
      else if (k[i][y[j]])
        k[i][x[j]] = 1;
    }
    if (k[i][i] == 2) {
      mset(k2[i], -1);
      continue;
    }
    rep(j, n) if (k[i][j]) k2[i][j / 64] |= 1ull << (j % 64);
  }
  ll res = 0;
  rep(i, n) rep(j, i) {
    int r = 1;
    rep(l, n / 64 + 1) {
      if (k2[i][l] & k2[j][l]) {
        r = 0;
        break;
      }
    }
    res += r;
  }
  cout << res << endl;
  return 0;
}