Problem p03205 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int maxn = 4e5, inf = 1e9;
int n, tot, ter[maxn + 3], wei[maxn + 3], nxt[maxn + 3], lnk[maxn + 3];
int cur[maxn + 3], dep[maxn + 3], num[maxn + 3], ans[maxn + 3];
bool vis[maxn + 3];
vector<int> S[maxn + 3], V[maxn + 3], G[maxn + 3];

int adj(int x) { return x & 1 ? x + 1 : x - 1; }

int add(int u, int v, int w) {
  ter[++tot] = v, wei[tot] = w;
  nxt[tot] = lnk[u], lnk[u] = tot;
  return tot;
}

int add_f(int u, int v) {
  int t = add(u, v, 1);
  return add(v, u, 0), t;
}

bool bfs() {
  queue<int> Q;
  Q.push(1);
  memset(dep, -1, sizeof(dep));
  dep[1] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = lnk[u], v, w; i; i = nxt[i]) {
      v = ter[i], w = wei[i];
      if ((~dep[v]) || !w)
        continue;
      dep[v] = dep[u] + 1;
      Q.push(v);
    }
  }
  return ~dep[n * 2];
}

int dfs(int u, int t, int lft) {
  if (u == t) {
    return lft;
  }
  int ret = 0;
  for (int &i = cur[u], v, w; i && ret < lft; i = nxt[i]) {
    v = ter[i], w = wei[i];
    if (w && dep[u] + 1 == dep[v]) {
      int x = dfs(v, t, min(lft - ret, w));
      wei[i] -= x, wei[adj(i)] += x, ret += x;
    }
  }
  if (ret < lft) {
    dep[u] = -1;
  }
  return ret;
}

int flow() {
  int ret = 0;
  while (bfs()) {
    memcpy(cur, lnk, sizeof(cur));
    ret += dfs(1, n * 2, inf);
  }
  return ret;
}

void dfs(int u) {
  vis[u] = true;
  for (int i = 0, v; i < G[u].size(); i++) {
    v = G[u][i];
    if (vis[v])
      continue;
    ans[v] = u;
    dfs(v);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1, m; i <= n - 1; i++) {
    scanf("%d", &m);
    S[i].resize(m), V[i].resize(m);
    for (int j = 0; j < m; j++) {
      scanf("%d", &S[i][j]);
      if (S[i][j] != 1) {
        V[i][j] = add_f(S[i][j], i + n);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    add_f(1, i);
  }
  for (int i = n + 1; i < n * 2; i++) {
    add_f(i, n * 2);
  }
  if (flow() != n - 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n - 1; i++) {
    int x = 0;
    for (int j = 0; j < S[i].size(); j++) {
      if (S[i][j] != 1 && !wei[V[i][j]]) {
        x = S[i][j];
        break;
      }
    }
    num[i] = x;
    for (int j = 0; j < S[i].size(); j++) {
      if (S[i][j] != x) {
        G[S[i][j]].push_back(x);
      }
    }
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      puts("-1");
      break;
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    printf("%d %d\n", ans[num[i]], num[i]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6, inf = 1e9;
int n, tot, ter[maxn + 3], wei[maxn + 3], nxt[maxn + 3], lnk[maxn + 3];
int cur[maxn + 3], dep[maxn + 3], num[maxn + 3], ans[maxn + 3];
bool vis[maxn + 3];
vector<int> S[maxn + 3], V[maxn + 3], G[maxn + 3];

int adj(int x) { return x & 1 ? x + 1 : x - 1; }

int add(int u, int v, int w) {
  ter[++tot] = v, wei[tot] = w;
  nxt[tot] = lnk[u], lnk[u] = tot;
  return tot;
}

int add_f(int u, int v) {
  int t = add(u, v, 1);
  return add(v, u, 0), t;
}

bool bfs() {
  queue<int> Q;
  Q.push(1);
  memset(dep, -1, sizeof(dep));
  dep[1] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = lnk[u], v, w; i; i = nxt[i]) {
      v = ter[i], w = wei[i];
      if ((~dep[v]) || !w)
        continue;
      dep[v] = dep[u] + 1;
      Q.push(v);
    }
  }
  return ~dep[n * 2];
}

int dfs(int u, int t, int lft) {
  if (u == t) {
    return lft;
  }
  int ret = 0;
  for (int &i = cur[u], v, w; i && ret < lft; i = nxt[i]) {
    v = ter[i], w = wei[i];
    if (w && dep[u] + 1 == dep[v]) {
      int x = dfs(v, t, min(lft - ret, w));
      wei[i] -= x, wei[adj(i)] += x, ret += x;
    }
  }
  if (ret < lft) {
    dep[u] = -1;
  }
  return ret;
}

int flow() {
  int ret = 0;
  while (bfs()) {
    memcpy(cur, lnk, sizeof(cur));
    ret += dfs(1, n * 2, inf);
  }
  return ret;
}

void dfs(int u) {
  vis[u] = true;
  for (int i = 0, v; i < G[u].size(); i++) {
    v = G[u][i];
    if (vis[v])
      continue;
    ans[v] = u;
    dfs(v);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1, m; i <= n - 1; i++) {
    scanf("%d", &m);
    S[i].resize(m), V[i].resize(m);
    for (int j = 0; j < m; j++) {
      scanf("%d", &S[i][j]);
      if (S[i][j] != 1) {
        V[i][j] = add_f(S[i][j], i + n);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    add_f(1, i);
  }
  for (int i = n + 1; i < n * 2; i++) {
    add_f(i, n * 2);
  }
  if (flow() != n - 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n - 1; i++) {
    int x = 0;
    for (int j = 0; j < S[i].size(); j++) {
      if (S[i][j] != 1 && !wei[V[i][j]]) {
        x = S[i][j];
        break;
      }
    }
    num[i] = x;
    for (int j = 0; j < S[i].size(); j++) {
      if (S[i][j] != x) {
        G[S[i][j]].push_back(x);
      }
    }
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      puts("-1");
      break;
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    printf("%d %d\n", ans[num[i]], num[i]);
  }
  return 0;
}