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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct BipartiteMatching {
  int V;
  vector<vector<bool>> G;
  vector<int> match;
  vector<bool> used;

  BipartiteMatching(int v) {
    V = v;
    G = vector<vector<bool>>(v, vector<bool>(v));
    match = vector<int>(v);
    used = vector<bool>(v);
  }

  void add_edge(int v, int u) { G[v][u] = G[u][v] = true; }

  void remove_edge(int v, int u) { G[v][u] = G[u][v] = false; }

  bool dfs(int v) {
    used[v] = true;
    for (int i = 0; i < V; i++) {
      if (!G[v][i])
        continue;
      int u = i, w = match[u];
      if (w < 0 || (!used[w] && dfs(w))) {
        match[v] = u;
        match[u] = v;
        return true;
      }
    }
    return false;
  }

  int calc() {
    int res = 0;
    fill(match.begin(), match.end(), -1);
    for (int v = 0; v < V; v++) {
      if (match[v] < 0) {
        fill(used.begin(), used.end(), false);
        if (dfs(v)) {
          res++;
        }
      }
    }
    return res;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  BipartiteMatching bp(2 * (N - 1));
  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < N - 1; j++) {
      int b;
      cin >> b;
      if (b)
        bp.add_edge(j, (N - 1) + i);
    }
  }

  if (bp.calc() != N - 1) {
    cout << "no" << endl;
    return 0;
  }

  cout << "yes" << endl;
  for (int i = 0; i < N - 1; i++) {
    int m = bp.match[i];
    bp.match[i] = bp.match[m] = -1;
    fill(bp.used.begin(), bp.used.end(), false);
    if (bp.dfs(i)) {
      assert(m >= bp.match[i]);
    }

    for (int j = 0; j < N - 1; j++) {
      bp.remove_edge(i, N - 1 + j);
    }
    bp.add_edge(i, bp.match[i]);

    cout << bp.match[i] - (N - 1) + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct BipartiteMatching {
  int V;
  vector<vector<bool>> G;
  vector<int> match;
  vector<bool> used;

  BipartiteMatching(int v) {
    V = v;
    G = vector<vector<bool>>(v, vector<bool>(v));
    match = vector<int>(v);
    used = vector<bool>(v);
  }

  void add_edge(int v, int u) { G[v][u] = G[u][v] = true; }

  void remove_edge(int v, int u) { G[v][u] = G[u][v] = false; }

  bool dfs(int v) {
    used[v] = true;
    for (int i = 0; i < V; i++) {
      if (!G[v][i])
        continue;
      int u = i, w = match[u];
      if (w < 0 || (!used[w] && dfs(w))) {
        match[v] = u;
        match[u] = v;
        return true;
      }
    }
    return false;
  }

  int calc() {
    int res = 0;
    fill(match.begin(), match.end(), -1);
    for (int v = 0; v < V; v++) {
      if (match[v] < 0) {
        fill(used.begin(), used.end(), false);
        if (dfs(v)) {
          res++;
        }
      }
    }
    return res;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  BipartiteMatching bp(2 * (N - 1));
  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < N - 1; j++) {
      int b;
      cin >> b;
      if (b)
        bp.add_edge(j, (N - 1) + i);
    }
  }

  if (bp.calc() != N - 1) {
    cout << "no" << endl;
    return 0;
  }

  for (int i = 0; i < N - 1; i++) {
    // cerr << "i : " << i << endl;
    for (int j = N - 2; j >= 0; j--) {
      int m = bp.match[i];
      if (j + N - 1 != m) {
        bp.remove_edge(i, m);
        continue;
      }
      // cerr << "j : " << j << endl;

      bp.match[i] = bp.match[m] = -1;
      bp.remove_edge(i, m);
      fill(bp.used.begin(), bp.used.end(), false);

      if (bp.dfs(i)) {
        assert(m > bp.match[i]);
        // cerr << "change to " << j << endl;
      } else {
        bp.add_edge(i, m);
        bp.match[i] = m;
        bp.match[m] = i;
      }
    }
  }

  cout << "yes" << endl;
  for (int i = 0; i < N - 1; i++) {
    cout << bp.match[i] - (N - 1) + 1 << endl;
  }
}