Problem p03205 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int n, S, T, d[100010], ans, a[100010], b[100010];
vector<int> v[100010];
int cnt = 1, head[200010], to[800010], nxt[800010], w[800010];
bool used[100010];
void add(int x, int y, int z) {
  cnt++;
  to[cnt] = y;
  nxt[cnt] = head[x];
  head[x] = cnt;
  w[cnt] = z;
}
void ins(int x, int y, int z) {
  add(x, y, z);
  add(y, x, 0);
}
bool bfs() {
  for (int i = S; i <= T; i++) {
    d[i] = -1;
  }
  queue<int> q;
  d[S] = 0;
  q.push(S);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (x == T) {
      return true;
    }
    for (int i = head[x]; i; i = nxt[i]) {
      int y = to[i];
      if (w[i] && d[y] == -1) {
        d[y] = d[x] + 1;
        q.push(y);
      }
    }
  }
  return false;
}
int dfs(int x, int f) {
  int res = 0;
  if (x == T) {
    return f;
  }
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (w[i] && d[y] == d[x] + 1) {
      int tmp = dfs(y, min(f - res, w[i]));
      w[i] -= tmp;
      w[i ^ 1] += tmp;
      res += tmp;
      if (res == f) {
        return res;
      }
    }
  }
  if (!res) {
    d[x] = -1;
  }
  return res;
}
void dinic() {
  while (bfs()) {
    ans += dfs(S, inf);
  }
}
int main() {
  scanf("%d", &n);
  S = 1;
  T = 2 * n;
  for (int i = 1; i < n; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      int x;
      scanf("%d", &x);
      v[x].push_back(i);
      if (x != 1) {
        ins(x, n + i, 1);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    ins(S, i, 1);
  }
  for (int i = 1; i < n; i++) {
    ins(n + i, T, 1);
  }
  dinic();
  if (ans != n - 1) {
    puts("-1");
    return 0;
  }
  for (int i = n + 1; i < 2 * n; i++) {
    for (int j = head[i]; j; j = nxt[j]) {
      if (!w[j]) {
        continue;
      }
      a[i - n] = to[j];
    }
  }
  queue<int> q;
  used[1] = true;
  q.push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i];
      if (!used[a[y]]) {
        used[a[y]] = true;
        q.push(a[y]);
        b[y] = x;
      }
    }
  }
  bool f = true;
  for (int i = 1; i < n; i++) {
    f &= used[i];
  }
  if (!f) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    printf("%d %d\n", a[i], b[i]);
  }
  return 0;
}
// 霓为衣兮风为马，云之君兮纷纷而来下。
// ——李白《梦游天姥吟留别》

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int n, S, T, d[200010], ans, a[100010], b[100010];
vector<int> v[100010];
int cnt = 1, head[200010], to[800010], nxt[800010], w[800010];
bool used[100010];
void add(int x, int y, int z) {
  cnt++;
  to[cnt] = y;
  nxt[cnt] = head[x];
  head[x] = cnt;
  w[cnt] = z;
}
void ins(int x, int y, int z) {
  add(x, y, z);
  add(y, x, 0);
}
bool bfs() {
  for (int i = S; i <= T; i++) {
    d[i] = -1;
  }
  queue<int> q;
  d[S] = 0;
  q.push(S);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (x == T) {
      return true;
    }
    for (int i = head[x]; i; i = nxt[i]) {
      int y = to[i];
      if (w[i] && d[y] == -1) {
        d[y] = d[x] + 1;
        q.push(y);
      }
    }
  }
  return false;
}
int dfs(int x, int f) {
  int res = 0;
  if (x == T) {
    return f;
  }
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (w[i] && d[y] == d[x] + 1) {
      int tmp = dfs(y, min(f - res, w[i]));
      w[i] -= tmp;
      w[i ^ 1] += tmp;
      res += tmp;
      if (res == f) {
        return res;
      }
    }
  }
  if (!res) {
    d[x] = -1;
  }
  return res;
}
void dinic() {
  while (bfs()) {
    ans += dfs(S, inf);
  }
}
int main() {
  scanf("%d", &n);
  S = 1;
  T = 2 * n;
  for (int i = 1; i < n; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      int x;
      scanf("%d", &x);
      v[x].push_back(i);
      if (x != 1) {
        ins(x, n + i, 1);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    ins(S, i, 1);
  }
  for (int i = 1; i < n; i++) {
    ins(n + i, T, 1);
  }
  dinic();
  if (ans != n - 1) {
    puts("-1");
    return 0;
  }
  for (int i = n + 1; i < 2 * n; i++) {
    for (int j = head[i]; j; j = nxt[j]) {
      if (!w[j]) {
        continue;
      }
      a[i - n] = to[j];
    }
  }
  queue<int> q;
  used[1] = true;
  q.push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < v[x].size(); i++) {
      int y = v[x][i];
      if (!used[a[y]]) {
        used[a[y]] = true;
        q.push(a[y]);
        b[y] = x;
      }
    }
  }
  bool f = true;
  for (int i = 1; i < n; i++) {
    f &= used[i];
  }
  if (!f) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    printf("%d %d\n", a[i], b[i]);
  }
  return 0;
}
// 霓为衣兮风为马，云之君兮纷纷而来下。
// ——李白《梦游天姥吟留别》