Problem p03409 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "cstring"
#include "iostream"
#include "math.h"
#include "queue"
#include "stack"
#include "string"
#include "vector"

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int INF = 10000;

// 行先　容量　逆辺
struct edge {
  int to, cap, rev;
};

int MAX_V = 200;
vector<vector<edge>> G(MAX_V); // グラフの隣接リスト表現
vector<bool> used(MAX_V);      // DFSですでに調べたかのフラグ

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge{to, cap, (int)G[to].size()});
  G[to].push_back(edge{from, 0, (int)G[from].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t) {
    return f;
  }

  used[v] = true;

  int Gsize = G[v].size();
  for (int i = 0; i < Gsize; i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

// ｓからｔへの最大流を求める
int max_flow(int s, int t) {
  int flow = 0;
  for (int i = 0; i < MAX_V; i++) {
    used[i] = false;
  }
  for (;;) {
    int f = dfs(s, t, INF);
    if (f == 0) {
      return flow;
    }
    flow += f;
  }
}

int n, k;
bool can[101][101] = {{false}};

void solve() {
  int s = n + k, t = s + 1;
  int ans = 0;

  for (int i = 0; i < n; i++) {
    add_edge(s, i, 1);
  }

  for (int i = 0; i < n; i++) {
    add_edge(n + i, t, 1);
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      if (can[i][j]) {
        add_edge(i, n + j, 1);
      }
    }
    // cout<< max_flow(s, t);
    ans += max_flow(s, t);
  }
  cout << ans;
}

int main() {

  cin >> n;
  k = n;
  vector<pii> a(n), b(k);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  for (int i = 0; i < k; i++) {
    cin >> b[i].first >> b[i].second;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      if (a[i].first < b[j].first && a[i].second < b[j].second) {
        can[i][j] = true;
      }
    }
  }
  solve();
}

Fixed submission:
#include "algorithm"
#include "cstring"
#include "iostream"
#include "math.h"
#include "queue"
#include "stack"
#include "string"
#include "vector"

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int INF = 10000;

// 行先　容量　逆辺
struct edge {
  int to, cap, rev;
};

int MAX_V = 210;
vector<vector<edge>> G(MAX_V); // グラフの隣接リスト表現
vector<bool> used(MAX_V);      // DFSですでに調べたかのフラグ

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge{to, cap, (int)G[to].size()});
  G[to].push_back(edge{from, 0, (int)G[from].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t) {
    return f;
  }

  used[v] = true;

  int Gsize = G[v].size();
  for (int i = 0; i < Gsize; i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

// ｓからｔへの最大流を求める
int max_flow(int s, int t) {
  int flow = 0;
  for (int i = 0; i < MAX_V; i++) {
    used[i] = false;
  }
  for (;;) {
    int f = dfs(s, t, INF);
    if (f == 0) {
      return flow;
    }
    flow += f;
  }
}

int n, k;
bool can[101][101] = {{false}};

void solve() {
  int s = n + k, t = s + 1;
  int ans = 0;

  for (int i = 0; i < n; i++) {
    add_edge(s, i, 1);
  }

  for (int i = 0; i < n; i++) {
    add_edge(n + i, t, 1);
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      if (can[i][j]) {
        add_edge(i, n + j, 1);
      }
    }
    // cout<< max_flow(s, t);
    ans += max_flow(s, t);
  }
  cout << ans;
}

int main() {

  cin >> n;
  k = n;
  vector<pii> a(n), b(k);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  for (int i = 0; i < k; i++) {
    cin >> b[i].first >> b[i].second;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      if (a[i].first < b[j].first && a[i].second < b[j].second) {
        can[i][j] = true;
      }
    }
  }
  solve();
}