Problem p01187 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

namespace flow {

class bipartite_matching {
private:
  int V;
  vector<vector<int>> G;
  vector<int> match;
  vector<bool> used;

  bool dfs(int v) {
    used[v] = 1;
    for (auto &u : G[v]) {
      int w = match[u];
      if (w < 0 || (!used[w] && dfs(w))) {
        match[v] = u;
        match[u] = v;
        return true;
      }
    }
    return false;
  }

public:
  bipartite_matching(int V) : V(V) {
    G.resize(V), match.resize(V), used.resize(V);
  }

  void add_edge(int u, int v) {
    G[u].push_back(v);
    G[v].push_back(u);
  }

  int maximum_matching() {
    int ret = 0;
    match.clear(), match.resize(V, -1);
    rep(v, V) {
      if (match[v] < 0) {
        used.clear(), used.resize(V, 0);
        if (dfs(v)) {
          ret++;
        }
      }
    }
    return ret;
  }
};

} // namespace flow

int main() {

  for (int N; cin >> N && N;) {
    int M;
    cin >> M;

    int dcnt = 0;
    map<int, int> dayidx;
    rep(i, M) {
      int x;
      cin >> x;
      if (dayidx.find(x) == dayidx.end()) {
        dayidx[x] = dcnt++;
      }
    }

    flow::bipartite_matching bp(dcnt + N);

    rep(girl, N) {
      cin >> M;
      rep(i, M) {
        int k;
        cin >> k;
        if (dayidx.find(k) != dayidx.end())
          bp.add_edge(dayidx[k], dcnt + girl);
      }
    }

    cout << bp.maximum_matching() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

namespace flow {

class bipartite_matching {
private:
  int V;
  vector<vector<int>> G;
  vector<int> match;
  vector<bool> used;

  bool dfs(int v) {
    used[v] = 1;
    for (auto &u : G[v]) {
      int w = match[u];
      if (w < 0 || (!used[w] && dfs(w))) {
        match[v] = u;
        match[u] = v;
        return true;
      }
    }
    return false;
  }

public:
  bipartite_matching(int V) : V(V) {
    G.resize(V), match.resize(V), used.resize(V);
  }

  void add_edge(int u, int v) {
    G[u].push_back(v);
    G[v].push_back(u);
  }

  int maximum_matching() {
    int ret = 0;
    match.clear(), match.resize(V, -1);
    rep(v, V) {
      if (match[v] < 0) {
        used.clear(), used.resize(V, 0);
        if (dfs(v)) {
          ret++;
        }
      }
    }
    return ret;
  }
};

} // namespace flow

int main() {

  for (int N; cin >> N && N;) {
    flow::bipartite_matching bp(40 + N);
    int M;
    cin >> M;
    set<int> dayset;
    rep(i, M) {
      int x;
      cin >> x;
      dayset.insert(x);
    }
    rep(girl, N) {
      cin >> M;
      rep(i, M) {
        int k;
        cin >> k;
        if (dayset.count(k))
          bp.add_edge(k, 40 + girl);
      }
    }

    cout << bp.maximum_matching() << endl;
  }

  return 0;
}