Problem p03411 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

typedef int FLOW;           // フローを表す型、今回は int 型
const int MAX_V = 100;      // グラフの最大ノード数
const FLOW INF = 100000000; // 十分大きい値

// グラフの辺の構造体
struct Edge {
  int rev, from, to;
  FLOW cap, icap;
  Edge(int r, int f, int t, FLOW c) : rev(r), from(f), to(t), cap(c), icap(c) {}
  friend ostream &operator<<(ostream &s, const Edge &E) {
    if (E.cap > 0)
      return s << E.from << "->" << E.to << '(' << E.cap << ')';
    else
      return s;
  }
};

// グラフ構造体
struct Graph {
  int V;
  vector<Edge> list[MAX_V];

  Graph(int n = 0) : V(n) {
    for (int i = 0; i < MAX_V; ++i)
      list[i].clear();
  }
  void init(int n = 0) {
    V = n;
    for (int i = 0; i < MAX_V; ++i)
      list[i].clear();
  }
  void resize(int n = 0) { V = n; }
  void reset() {
    for (int i = 0; i < V; ++i)
      for (int j = 0; j < list[i].size(); ++j)
        list[i][j].cap = list[i][j].icap;
  }
  inline vector<Edge> &operator[](int i) { return list[i]; }

  Edge &redge(Edge e) {
    if (e.from != e.to)
      return list[e.to][e.rev];
    else
      return list[e.to][e.rev + 1];
  }

  void addedge(int from, int to, FLOW cap) {
    list[from].push_back(Edge((int)list[to].size(), from, to, cap));
    list[to].push_back(Edge((int)list[from].size() - 1, to, from, 0));
  }
};

// 最大流を求めるサブルーチンたち
static int level[MAX_V];
static int iter[MAX_V];

void dibfs(Graph &G, int s) {
  for (int i = 0; i < MAX_V; ++i)
    level[i] = -1;
  level[s] = 0;
  queue<int> que;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < G[v].size(); ++i) {
      Edge &e = G[v][i];
      if (level[e.to] < 0 && e.cap > 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

FLOW didfs(Graph &G, int v, int t, FLOW f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); ++i) {
    Edge &e = G[v][i], &re = G.redge(e);
    if (level[v] < level[e.to] && e.cap > 0) {
      FLOW d = didfs(G, e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        re.cap += d;
        return d;
      }
    }
  }
  return 0;
}

// 最大流を求めるメイン関数
FLOW Dinic(Graph &G, int s, int t) {
  FLOW res = 0;
  while (true) {
    dibfs(G, s);
    if (level[t] < 0)
      return res;
    memset(iter, 0, sizeof(iter));
    FLOW flow;
    while ((flow = didfs(G, s, t, INF)) > 0) {
      res += flow;
    }
  }
}

int main() {

  int n;
  cin >> n;
  pair<int, int> r[n], b[n];
  Graph G(2 * n + 2);
  int S_node = 2 * n;
  int T_node = 2 * n + 1;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    r[i] = make_pair(x, y);
  }
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    b[i] = make_pair(x, y);
  }
  rep(i, n) rep(j, n) {
    if (r[j].first < b[i].first && r[j].second < b[i].second) {
      G.addedge(j, i + n, 1);
    }
  }

  for (int i = 0; i < n; ++i) {
    G.addedge(S_node, i, 1);
  }

  for (int j = 0; j < n; ++j) {
    G.addedge(j + n, T_node, 1);
  }

  // 最大流を求める
  FLOW res = Dinic(G, S_node, T_node);

  // 出力
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

typedef int FLOW;           // フローを表す型、今回は int 型
const int MAX_V = 205;      // グラフの最大ノード数
const FLOW INF = 100000000; // 十分大きい値

// グラフの辺の構造体
struct Edge {
  int rev, from, to;
  FLOW cap, icap;
  Edge(int r, int f, int t, FLOW c) : rev(r), from(f), to(t), cap(c), icap(c) {}
  friend ostream &operator<<(ostream &s, const Edge &E) {
    if (E.cap > 0)
      return s << E.from << "->" << E.to << '(' << E.cap << ')';
    else
      return s;
  }
};

// グラフ構造体
struct Graph {
  int V;
  vector<Edge> list[MAX_V];

  Graph(int n = 0) : V(n) {
    for (int i = 0; i < MAX_V; ++i)
      list[i].clear();
  }
  void init(int n = 0) {
    V = n;
    for (int i = 0; i < MAX_V; ++i)
      list[i].clear();
  }
  void resize(int n = 0) { V = n; }
  void reset() {
    for (int i = 0; i < V; ++i)
      for (int j = 0; j < list[i].size(); ++j)
        list[i][j].cap = list[i][j].icap;
  }
  inline vector<Edge> &operator[](int i) { return list[i]; }

  Edge &redge(Edge e) {
    if (e.from != e.to)
      return list[e.to][e.rev];
    else
      return list[e.to][e.rev + 1];
  }

  void addedge(int from, int to, FLOW cap) {
    list[from].push_back(Edge((int)list[to].size(), from, to, cap));
    list[to].push_back(Edge((int)list[from].size() - 1, to, from, 0));
  }
};

// 最大流を求めるサブルーチンたち
static int level[MAX_V];
static int iter[MAX_V];

void dibfs(Graph &G, int s) {
  for (int i = 0; i < MAX_V; ++i)
    level[i] = -1;
  level[s] = 0;
  queue<int> que;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < G[v].size(); ++i) {
      Edge &e = G[v][i];
      if (level[e.to] < 0 && e.cap > 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

FLOW didfs(Graph &G, int v, int t, FLOW f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); ++i) {
    Edge &e = G[v][i], &re = G.redge(e);
    if (level[v] < level[e.to] && e.cap > 0) {
      FLOW d = didfs(G, e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        re.cap += d;
        return d;
      }
    }
  }
  return 0;
}

// 最大流を求めるメイン関数
FLOW Dinic(Graph &G, int s, int t) {
  FLOW res = 0;
  while (true) {
    dibfs(G, s);
    if (level[t] < 0)
      return res;
    memset(iter, 0, sizeof(iter));
    FLOW flow;
    while ((flow = didfs(G, s, t, INF)) > 0) {
      res += flow;
    }
  }
}

int main() {

  int n;
  cin >> n;
  pair<int, int> r[n], b[n];
  Graph G(2 * n + 2);
  int S_node = 2 * n;
  int T_node = 2 * n + 1;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    r[i] = make_pair(x, y);
  }
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    b[i] = make_pair(x, y);
  }
  rep(i, n) rep(j, n) {
    if (r[j].first < b[i].first && r[j].second < b[i].second) {
      G.addedge(j, i + n, 1);
    }
  }

  for (int i = 0; i < n; ++i) {
    G.addedge(S_node, i, 1);
  }

  for (int j = 0; j < n; ++j) {
    G.addedge(j + n, T_node, 1);
  }

  // 最大流を求める
  FLOW res = Dinic(G, S_node, T_node);

  // 出力
  cout << res << endl;
  return 0;
}