Problem p03205 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iterator>
#include <vector>
using namespace std;
struct R {
  int v, z, c;
} e[1111111];
int K[222222], cur[222222], ce = 1;
void ins(int a, int b, int c) {
  e[++ce] = (R){b, K[a], c};
  K[a] = ce;
}
void AE(int a, int b) {
  ins(a, b, 1);
  ins(b, a, 0);
}
int s, t, d[222222];
bool BFS() {
  static int q[222222], du, my;
  memset(d, 0, sizeof(d));
  memcpy(cur, K, sizeof(cur));
  for (d[q[du = my = 1] = s] = 1; du <= my;) {
    int u = q[du++];
    for (int i = K[u]; i; i = e[i].z)
      if (e[i].c && !d[e[i].v]) {
        d[e[i].v] = d[u] + 1;
        q[++my] = e[i].v;
      }
  }
  return d[t];
}
int DFS(int u, int r) {
  if (u == t)
    return r;
  int tmp = r;
  for (int &i = cur[u]; i; i = e[i].z)
    if (e[i].c && d[e[i].v] == d[u] + 1) {
      int qwq = DFS(e[i].v, min(r, e[i].c));
      if (qwq) {
        r -= qwq;
        e[i].c -= qwq;
        e[i ^ 1].c += qwq;
        if (!r)
          return tmp;
      }
    }
  return tmp - r;
}
int Dinic() {
  int r = 0;
  for (; BFS();)
    r += DFS(s, 2333333);
  return r;
}
bool viz[222222];
int n, p[111111], u[111111], v[111111];
void dfs(int u, int pa, int gp) {
  viz[u] = 1;
  if (u <= n) {
    ::u[pa - n] = gp;
    ::v[pa - n] = u;
  }
  for (int i = K[u]; i; i = e[i].z)
    if (!viz[e[i].v] && e[i].c)
      dfs(e[i].v, u, pa);
}
int main() {
  scanf("%d", &n);
  s = 2 * n;
  t = 2 * n + 1;
  for (int i = 2; i <= n; ++i)
    AE(s, i);
  for (int i = 1; i < n; ++i) {
    int c;
    for (scanf("%d", &c); c--;) {
      int v;
      scanf("%d", &v);
      AE(v, i + n);
    }
    AE(i + n, t);
  }
  viz[s] = viz[t] = 1;
  if (Dinic() != n - 1)
    return puts("-1"), 0;
  dfs(1, 0, 0);
  for (int i = 1; i < n; ++i)
    if (!u[i])
      return puts("-1"), 0;
  for (int i = 1; i < n; ++i)
    printf("%d %d\n", u[i], v[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iterator>
#include <vector>
using namespace std;
struct R {
  int v, z, c;
} e[1111111];
int K[222222], cur[222222], ce = 1;
void ins(int a, int b, int c) {
  e[++ce] = (R){b, K[a], c};
  K[a] = ce;
}
void AE(int a, int b) {
  ins(a, b, 1);
  ins(b, a, 0);
}
int s, t, d[222222];
bool BFS() {
  static int q[222222], du, my;
  memset(d, 0, sizeof(d));
  memcpy(cur, K, sizeof(cur));
  for (d[q[du = my = 1] = s] = 1; du <= my;) {
    int u = q[du++];
    for (int i = K[u]; i; i = e[i].z)
      if (e[i].c && !d[e[i].v]) {
        d[e[i].v] = d[u] + 1;
        q[++my] = e[i].v;
      }
  }
  return d[t];
}
int DFS(int u, int r) {
  if (u == t)
    return r;
  int tmp = r;
  for (int &i = cur[u]; i; i = e[i].z)
    if (e[i].c && d[e[i].v] == d[u] + 1) {
      int qwq = DFS(e[i].v, min(r, e[i].c));
      if (qwq) {
        r -= qwq;
        e[i].c -= qwq;
        e[i ^ 1].c += qwq;
        if (!r)
          return tmp;
      }
    }
  return tmp - r;
}
int Dinic() {
  int r = 0;
  for (; BFS();)
    r += DFS(s, 2333333);
  return r;
}
bool viz[222222];
int n, p[111111], u[111111], v[111111];
void dfs(int u, int pa, int gp) {
  viz[u] = 1;
  if (u <= n && pa) {
    ::u[pa - n] = gp;
    ::v[pa - n] = u;
  }
  for (int i = K[u]; i; i = e[i].z)
    if (!viz[e[i].v] && e[i].c)
      dfs(e[i].v, u, pa);
}
int main() {
  scanf("%d", &n);
  s = 2 * n;
  t = 2 * n + 1;
  for (int i = 2; i <= n; ++i)
    AE(s, i);
  for (int i = 1; i < n; ++i) {
    int c;
    for (scanf("%d", &c); c--;) {
      int v;
      scanf("%d", &v);
      AE(v, i + n);
    }
    AE(i + n, t);
  }
  viz[s] = viz[t] = 1;
  if (Dinic() != n - 1)
    return puts("-1"), 0;
  dfs(1, 0, 0);
  for (int i = 1; i < n; ++i)
    if (!u[i])
      return puts("-1"), 0;
  for (int i = 1; i < n; ++i)
    printf("%d %d\n", u[i], v[i]);
  return 0;
}