Problem p03411 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

bool augment(const Graph &g, int u, vector<int> &matchTo,
             vector<bool> &visited) {
  if (u < 0)
    return true;
  REP(i, g[u].size()) {
    auto e = g[u][i];
    if (visited[e.dst])
      continue;
    visited[e.dst] = true;
    if (augment(g, matchTo[e.dst], matchTo, visited)) {
      matchTo[e.src] = e.dst;
      matchTo[e.dst] = e.src;
      return true;
    }
  }
  return false;
}

int bipartiteMatching(const Graph &g, int L, Edges &matching) {
  const int n = g.size();
  vector<int> matchTo(n, -1);
  int match = 0;
  REP(u, L) {
    vector<bool> visited(n);
    if (augment(g, u, matchTo, visited))
      ++match;
  }
  REP(u, L)
  if (matchTo[u] >= 0) // make explicit matching
      matching.push_back(Edge(u, matchTo[u], 1));
  return match;
}

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

int main() {
  int n;
  cin >> n;
  REP(i, n) scanf("%d%d", a + i, b + i);
  REP(i, n) scanf("%d%d", c + i, d + i);
  Graph g(2 * n);
  REP(i, n) {
    REP(j, n) {
      if (a[i] < c[j] && b[i] < d[j]) {
        g[i + 1].push_back(Edge(i + 1, n + j + 1, 1));
      }
    }
  }
  Edges es;
  cout << bipartiteMatching(g, n, es) << endl;
  return 0;
}

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

bool augment(const Graph &g, int u, vector<int> &matchTo,
             vector<bool> &visited) {
  if (u < 0)
    return true;
  REP(i, g[u].size()) {
    auto e = g[u][i];
    if (visited[e.dst])
      continue;
    visited[e.dst] = true;
    if (augment(g, matchTo[e.dst], matchTo, visited)) {
      matchTo[e.src] = e.dst;
      matchTo[e.dst] = e.src;
      return true;
    }
  }
  return false;
}

int bipartiteMatching(const Graph &g, int L, Edges &matching) {
  const int n = g.size();
  vector<int> matchTo(n, -1);
  int match = 0;
  REP(u, L) {
    vector<bool> visited(n);
    if (augment(g, u, matchTo, visited))
      ++match;
  }
  REP(u, L)
  if (matchTo[u] >= 0) // make explicit matching
      matching.push_back(Edge(u, matchTo[u], 1));
  return match;
}

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

int main() {
  int n;
  cin >> n;
  REP(i, n) scanf("%d%d", a + i, b + i);
  REP(i, n) scanf("%d%d", c + i, d + i);
  Graph g(2 * n);
  REP(i, n) {
    REP(j, n) {
      if (a[i] < c[j] && b[i] < d[j]) {
        g[i].push_back(Edge(i, n + j, 1));
      }
    }
  }
  Edges es;
  cout << bipartiteMatching(g, n, es) << endl;
  return 0;
}