Problem p03205 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define il inline
using namespace std;
const int N = 1e5 + 5, M = 2e6;

int n, m, e, S, T, h, t, q[N], lev[N], ax[N], ay[N], o[N];
int to[M], nx[M], cap[M], hd[N], it[N], sze = 1;
std::vector<int> g[N];

il void add(int u, int v, int c) {
  to[++sze] = v, nx[sze] = hd[u], cap[sze] = c, hd[u] = sze;
}

il void ins(int u, int v, int c) { add(u, v, c), add(v, u, 0); }

il int bfs() {
  memset(lev, 0, 4 * T + 4);
  int i, u, v;
  q[h = t = lev[S] = 1] = S;
  while (h <= t)
    for (i = hd[u = q[h++]]; i; i = nx[i])
      if (cap[i] && !lev[v = to[i]])
        lev[v] = lev[u] + 1, q[++t] = v;
  return lev[T];
}

il int dfs(int u, int f) {
  if (u == T)
    return f;
  int z, v, res = 0;
  for (int &i = it[u]; i; i = nx[i])
    if (lev[v = to[i]] == lev[u] + 1 && cap[i] &&
        (z = dfs(v, min(f - res, cap[i])))) {
      cap[i] -= z, cap[i ^ 1] += z, res += z;
      if (res == f)
        break;
    }
  return res;
}

il int dinic() {
  int i, f, res = 0;
  while (bfs()) {
    for (i = 1; i <= T; i++)
      it[i] = hd[i];
    while (f = dfs(S, 1e9))
      res += f;
  }
  return res;
}

il int work() {
  int i, u, v, s = 0;
  for (u = 1; u < n; u++)
    for (i = hd[u]; i; i = nx[i])
      if ((v = to[i]) != S && !cap[i])
        o[u] = v - n + 1;
  for (q[++t] = n; h <= t;) {
    u = q[h++], s++;
    for (int v : g[u])
      if (o[v])
        ax[v] = u, ay[v] = o[v], q[++t] = o[v], o[v] = 0;
  }
  return s == n;
}

int main() {
  scanf("%d", &n), S = n + n - 1, T = n + n;
  int i, c, x;
  for (i = 1; i < n; i++)
    ins(i + n - 1, T, 1);
  for (i = 1; i < n; i++) {
    scanf("%d", &c), ins(S, i, 1);
    while (c--) {
      scanf("%d", &x);
      if (x != n)
        ins(i, x + n - 1, 1);
      g[x].push_back(i);
    }
  }

  if (dinic() < n - 1)
    return puts("-1"), 0;
  if (!work())
    return puts("-1"), 0;
  for (i = 1; i < n; i++)
    printf("%d %d\n", ax[i], ay[i]);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define il inline
using namespace std;
const int N = 2e5 + 5, M = 2e6;

int n, m, e, S, T, h, t, q[N], lev[N], ax[N], ay[N], o[N];
int to[M], nx[M], cap[M], hd[N], it[N], sze = 1;
std::vector<int> g[N];

il void add(int u, int v, int c) {
  to[++sze] = v, nx[sze] = hd[u], cap[sze] = c, hd[u] = sze;
}

il void ins(int u, int v, int c) { add(u, v, c), add(v, u, 0); }

il int bfs() {
  memset(lev, 0, 4 * T + 4);
  int i, u, v;
  q[h = t = lev[S] = 1] = S;
  while (h <= t)
    for (i = hd[u = q[h++]]; i; i = nx[i])
      if (cap[i] && !lev[v = to[i]])
        lev[v] = lev[u] + 1, q[++t] = v;
  return lev[T];
}

il int dfs(int u, int f) {
  if (u == T)
    return f;
  int z, v, res = 0;
  for (int &i = it[u]; i; i = nx[i])
    if (lev[v = to[i]] == lev[u] + 1 && cap[i] &&
        (z = dfs(v, min(f - res, cap[i])))) {
      cap[i] -= z, cap[i ^ 1] += z, res += z;
      if (res == f)
        break;
    }
  return res;
}

il int dinic() {
  int i, f, res = 0;
  while (bfs()) {
    for (i = 1; i <= T; i++)
      it[i] = hd[i];
    while (f = dfs(S, 1e9))
      res += f;
  }
  return res;
}

il int work() {
  int i, u, v, s = 0;
  for (u = 1; u < n; u++)
    for (i = hd[u]; i; i = nx[i])
      if ((v = to[i]) != S && !cap[i])
        o[u] = v - n + 1;
  for (q[++t] = n; h <= t;) {
    u = q[h++], s++;
    for (int v : g[u])
      if (o[v])
        ax[v] = u, ay[v] = o[v], q[++t] = o[v], o[v] = 0;
  }
  return s == n;
}

int main() {
  scanf("%d", &n), S = n + n - 1, T = n + n;
  int i, c, x;
  for (i = 1; i < n; i++)
    ins(i + n - 1, T, 1);
  for (i = 1; i < n; i++) {
    scanf("%d", &c), ins(S, i, 1);
    while (c--) {
      scanf("%d", &x);
      if (x != n)
        ins(i, x + n - 1, 1);
      g[x].push_back(i);
    }
  }

  if (dinic() < n - 1)
    return puts("-1"), 0;
  if (!work())
    return puts("-1"), 0;
  for (i = 1; i < n; i++)
    printf("%d %d\n", ax[i], ay[i]);

  return 0;
}