Problem p03411 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

const int MAXN = 1100;

struct Edge {
  int to, next;
};

Edge Edges[MAXN << 1];
int head[MAXN], cnt, Nx, Ny;

int Mx[MAXN], My[MAXN];
queue<int> Q;
int dx[MAXN], dy[MAXN];
bool vis[MAXN];

bool HopcroftKarpDfs(int u) {
  for (int i = head[u]; ~i; i = Edges[i].next) {
    int v = Edges[i].to;
    if (!vis[v] && dy[v] == dx[u] + 1) {
      vis[v] = true;
      if (!My[v] || HopcroftKarpDfs(My[v])) {
        Mx[u] = v, My[v] = u;
        return true;
      }
    }
  }
  return false;
}

int HopcroftKarp() {
  memset(Mx, 0, sizeof(Mx));
  memset(My, 0, sizeof(My));
  int ans = 0;
  while (true) {
    bool flag = false;
    while (!Q.empty())
      Q.pop();
    memset(dx, 0, sizeof(dx));
    memset(dy, 0, sizeof(dy));
    for (int i = 1; i <= Nx; i++)
      if (!Mx[i])
        Q.push(i);
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      for (int i = head[u]; ~i; i = Edges[i].next) {
        int v = Edges[i].to;
        if (!dy[v]) {
          dy[v] = dx[u] + 1;
          if (My[v]) {
            dx[My[v]] = dy[v] + 1;
            Q.push(My[v]);
          } else
            flag = true;
        }
      }
    }
    if (!flag)
      break;
    memset(vis, false, sizeof(vis));
    for (int i = 1; i <= Nx; i++)
      if (!Mx[i] && HopcroftKarpDfs(i))
        ans++;
  }
  return ans;
}

int a[MAXN], b[MAXN], c[MAXN], d[MAXN];

int main() {
  int N;

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> a[i] >> b[i];
  }

  for (int i = 1; i <= N; i++) {
    cin >> c[i] >> d[i];
  }

  memset(head, -1, sizeof(head));
  cnt = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = N + 1; j <= (N << 1); j++) {
      if (a[i] < c[j - N] && b[i] < d[j - N]) {
        Edges[cnt].to = i, Edges[cnt].next = head[j], head[j] = cnt++;
        Edges[cnt].to = j, Edges[cnt].next = head[i], head[i] = cnt++;
      }
    }
  }

  Nx = Ny = (N << 1);
  int ans = HopcroftKarp();

  cout << ans / 2;

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

const int MAXN = 4000;

struct Edge {
  int to, next;
};

Edge Edges[MAXN << 1];
int head[MAXN], cnt, Nx, Ny;

int Mx[MAXN], My[MAXN];
queue<int> Q;
int dx[MAXN], dy[MAXN];
bool vis[MAXN];

bool HopcroftKarpDfs(int u) {
  for (int i = head[u]; ~i; i = Edges[i].next) {
    int v = Edges[i].to;
    if (!vis[v] && dy[v] == dx[u] + 1) {
      vis[v] = true;
      if (!My[v] || HopcroftKarpDfs(My[v])) {
        Mx[u] = v, My[v] = u;
        return true;
      }
    }
  }
  return false;
}

int HopcroftKarp() {
  memset(Mx, 0, sizeof(Mx));
  memset(My, 0, sizeof(My));
  int ans = 0;
  while (true) {
    bool flag = false;
    while (!Q.empty())
      Q.pop();
    memset(dx, 0, sizeof(dx));
    memset(dy, 0, sizeof(dy));
    for (int i = 1; i <= Nx; i++)
      if (!Mx[i])
        Q.push(i);
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      for (int i = head[u]; ~i; i = Edges[i].next) {
        int v = Edges[i].to;
        if (!dy[v]) {
          dy[v] = dx[u] + 1;
          if (My[v]) {
            dx[My[v]] = dy[v] + 1;
            Q.push(My[v]);
          } else
            flag = true;
        }
      }
    }
    if (!flag)
      break;
    memset(vis, false, sizeof(vis));
    for (int i = 1; i <= Nx; i++)
      if (!Mx[i] && HopcroftKarpDfs(i))
        ans++;
  }
  return ans;
}

int a[MAXN], b[MAXN], c[MAXN], d[MAXN];

int main() {
  int N;

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> a[i] >> b[i];
  }

  for (int i = 1; i <= N; i++) {
    cin >> c[i] >> d[i];
  }

  memset(head, -1, sizeof(head));
  cnt = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = N + 1; j <= (N << 1); j++) {
      if (a[i] < c[j - N] && b[i] < d[j - N]) {
        Edges[cnt].to = i, Edges[cnt].next = head[j], head[j] = cnt++;
        Edges[cnt].to = j, Edges[cnt].next = head[i], head[i] = cnt++;
      }
    }
  }

  Nx = Ny = (N << 1);
  int ans = HopcroftKarp();

  cout << ans / 2;

  return 0;
}