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

#include <algorithm>
#include <map>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
const int D_MAX_V = 2102;
const int D_v_size = 2102;
struct D_wolf {
  int t, c, r;
  D_wolf() { t = c = r = 0; }
  D_wolf(int t1, int c1, int r1) {
    t = t1;
    c = c1;
    r = r1;
  }
};
vector<D_wolf> D_G[D_MAX_V];
int D_level[D_MAX_V];
int D_iter[D_MAX_V];

void add_edge(int from, int to, int cap) {
  D_G[from].push_back(D_wolf(to, cap, D_G[to].size()));
  D_G[to].push_back(D_wolf(from, 0, D_G[from].size() - 1));
}
void D_bfs(int s) {
  for (int i = 0; i < D_v_size; i++)
    D_level[i] = -1;
  queue<int> Q;
  D_level[s] = 0;
  Q.push(s);
  while (Q.size()) {
    int v = Q.front();
    Q.pop();
    for (int i = 0; i < D_G[v].size(); i++) {
      if (D_G[v][i].c > 0 && D_level[D_G[v][i].t] < 0) {
        D_level[D_G[v][i].t] = D_level[v] + 1;
        Q.push(D_G[v][i].t);
      }
    }
  }
}
int D_dfs(int v, int t, int f) {
  if (v == t)
    return f;
  for (; D_iter[v] < D_G[v].size(); D_iter[v]++) {
    int i = D_iter[v];
    if (D_G[v][i].c > 0 && D_level[v] < D_level[D_G[v][i].t]) {
      int d = D_dfs(D_G[v][i].t, t, min(f, D_G[v][i].c));
      if (d > 0) {
        D_G[v][i].c -= d;
        D_G[D_G[v][i].t][D_G[v][i].r].c += d;
        return d;
      }
    }
  }
  return 0;
}
int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    D_bfs(s);
    if (D_level[t] < 0)
      return flow;
    for (int i = 0; i < D_v_size; i++)
      D_iter[i] = 0;
    int f;
    while ((f = D_dfs(s, t, 99999999)) > 0) {
      flow += f;
    }
  }
  return 0;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < 2102; i++) {
      D_G[i].clear();
      D_iter[i] = D_level[i] = 0;
    }
    int n;
    scanf("%d", &n);
    map<int, int> c;
    for (int i = 0; i < n; i++) {
      int b;
      scanf("%d", &b);
      c[b] = i;
    }
    int s = 2100;
    int t = 2101;
    for (int i = 0; i < n; i++)
      add_edge(s, i, 1);
    for (int i = 0; i < a; i++) {
      int m;
      scanf("%d", &m);
      for (int j = 0; j < m; j++) {
        int p;
        scanf("%d", &p);
        if (c.count(p)) {
          add_edge(c[p], i + n, 1);
        }
      }
    }
    for (int i = 0; i < a; i++)
      add_edge(i + n, t, 1);
    printf("%d\\n", max_flow(s, t));
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
const int D_MAX_V = 2102;
const int D_v_size = 2102;
struct D_wolf {
  int t, c, r;
  D_wolf() { t = c = r = 0; }
  D_wolf(int t1, int c1, int r1) {
    t = t1;
    c = c1;
    r = r1;
  }
};
vector<D_wolf> D_G[D_MAX_V];
int D_level[D_MAX_V];
int D_iter[D_MAX_V];

void add_edge(int from, int to, int cap) {
  D_G[from].push_back(D_wolf(to, cap, D_G[to].size()));
  D_G[to].push_back(D_wolf(from, 0, D_G[from].size() - 1));
}
void D_bfs(int s) {
  for (int i = 0; i < D_v_size; i++)
    D_level[i] = -1;
  queue<int> Q;
  D_level[s] = 0;
  Q.push(s);
  while (Q.size()) {
    int v = Q.front();
    Q.pop();
    for (int i = 0; i < D_G[v].size(); i++) {
      if (D_G[v][i].c > 0 && D_level[D_G[v][i].t] < 0) {
        D_level[D_G[v][i].t] = D_level[v] + 1;
        Q.push(D_G[v][i].t);
      }
    }
  }
}
int D_dfs(int v, int t, int f) {
  if (v == t)
    return f;
  for (; D_iter[v] < D_G[v].size(); D_iter[v]++) {
    int i = D_iter[v];
    if (D_G[v][i].c > 0 && D_level[v] < D_level[D_G[v][i].t]) {
      int d = D_dfs(D_G[v][i].t, t, min(f, D_G[v][i].c));
      if (d > 0) {
        D_G[v][i].c -= d;
        D_G[D_G[v][i].t][D_G[v][i].r].c += d;
        return d;
      }
    }
  }
  return 0;
}
int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    D_bfs(s);
    if (D_level[t] < 0)
      return flow;
    for (int i = 0; i < D_v_size; i++)
      D_iter[i] = 0;
    int f;
    while ((f = D_dfs(s, t, 99999999)) > 0) {
      flow += f;
    }
  }
  return 0;
}
int c[2100000];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < 2102; i++) {
      D_G[i].clear();
      D_iter[i] = D_level[i] = 0;
    }
    int n;
    scanf("%d", &n);
    for (int i = 0; i < 2100000; i++)
      c[i] = -1;
    for (int i = 0; i < n; i++) {
      int b;
      scanf("%d", &b);
      c[b] = i;
    }
    int s = 2100;
    int t = 2101;
    for (int i = 0; i < n; i++)
      add_edge(s, i, 1);
    for (int i = 0; i < a; i++) {
      int m;
      scanf("%d", &m);
      for (int j = 0; j < m; j++) {
        int p;
        scanf("%d", &p);
        if (~c[p]) {
          add_edge(c[p], i + n, 1);
        }
      }
    }
    for (int i = 0; i < a; i++)
      add_edge(i + n, t, 1);
    printf("%d\\n", max_flow(s, t));
  }
}