Problem p03205 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;

int n, m;
vector<int> hv[200005];
struct edge {
  int to, cap, rev;
} E[400005];
vector<int> G[200005];

void add_edge(int u, int v, int c) {
  E[m].to = v;
  E[m].cap = c;
  E[m].rev = m ^ 1;
  G[u].push_back(m++);
  E[m].to = u;
  E[m].cap = 0;
  E[m].rev = m ^ 1;
  G[v].push_back(m++);
}

int dis[200005], cur[200005];
bool bfs() {
  rep1(i, 2 * n) dis[i] = -1;
  queue<int> que;
  que.push(0);
  dis[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    rep(i, G[v].size()) {
      int ce = G[v][i];
      if (E[ce].cap > 0 && dis[E[ce].to] == -1) {
        que.push(E[ce].to);
        dis[E[ce].to] = dis[v] + 1;
      }
    }
  }
  if (dis[2 * n] == -1)
    return false;
  rep(i, 2 * n + 1) cur[i] = 0;
  return true;
}

int dfs(int v, int maxf) {
  if (v == 2 * n)
    return maxf;
  if (maxf == 0)
    return 0;
  int ret = 0;
  for (; cur[v] < G[v].size(); cur[v]++) {
    int ce = G[v][cur[v]];
    if (dis[E[ce].to] == dis[v] + 1 && E[ce].cap > 0) {
      int cf = dfs(E[ce].to, min(maxf - ret, E[ce].cap));
      E[ce].cap -= cf;
      E[E[ce].rev].cap += cf;
      ret += cf;
      if (ret == maxf)
        break;
    }
  }
  return ret;
}

int lp[200005], par[200005];

int dinic() {
  int ret = 0;
  while (bfs())
    ret += dfs(0, INF);
  return ret;
}

int main() {
  scanf("%d", &n);
  rep1(i, n - 1) {
    int cs;
    scanf("%d", &cs);
    hv[i].resize(cs);
    rep(j, cs) {
      scanf("%d", &hv[i][j]);
      add_edge(hv[i][j], i + n, 1);
    }
    add_edge(i + n, n * 2, 1);
  }
  rep1(i, n) add_edge(0, i, 1);
  if (dinic() != n - 1) {
    printf("-1\n");
    return 0;
  }
  rep1(i, n) if (dis[i] == -1) {
    printf("-1\n");
    return 0;
  }

  rep1(i, 2 * n - 1) rep(j, G[i].size()) {
    int ce = G[i][j];
    if (E[ce].cap == 0 && dis[E[ce].to] == dis[i] - 1) {
      par[i] = E[ce].to;
      break;
    }
  }

  rep1(i, n) if (par[i] != 0) lp[par[i] - n] = i;
  rep1(i, n - 1) printf("%d %d\n", par[i + n], lp[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;

int n, m;
vector<int> hv[200005];
struct edge {
  int to, cap, rev;
} E[800005];
vector<int> G[200005];

void add_edge(int u, int v, int c) {
  E[m].to = v;
  E[m].cap = c;
  E[m].rev = m ^ 1;
  G[u].push_back(m++);
  E[m].to = u;
  E[m].cap = 0;
  E[m].rev = m ^ 1;
  G[v].push_back(m++);
}

int dis[200005], cur[200005];
bool bfs() {
  rep1(i, 2 * n) dis[i] = -1;
  queue<int> que;
  que.push(0);
  dis[0] = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    rep(i, G[v].size()) {
      int ce = G[v][i];
      if (E[ce].cap > 0 && dis[E[ce].to] == -1) {
        que.push(E[ce].to);
        dis[E[ce].to] = dis[v] + 1;
      }
    }
  }
  if (dis[2 * n] == -1)
    return false;
  rep(i, 2 * n + 1) cur[i] = 0;
  return true;
}

int dfs(int v, int maxf) {
  if (v == 2 * n)
    return maxf;
  if (maxf == 0)
    return 0;
  int ret = 0;
  for (; cur[v] < G[v].size(); cur[v]++) {
    int ce = G[v][cur[v]];
    if (dis[E[ce].to] == dis[v] + 1 && E[ce].cap > 0) {
      int cf = dfs(E[ce].to, min(maxf - ret, E[ce].cap));
      E[ce].cap -= cf;
      E[E[ce].rev].cap += cf;
      ret += cf;
      if (ret == maxf)
        break;
    }
  }
  return ret;
}

int lp[200005], par[200005];

int dinic() {
  int ret = 0;
  while (bfs())
    ret += dfs(0, INF);
  return ret;
}

int main() {
  scanf("%d", &n);
  rep1(i, n - 1) {
    int cs;
    scanf("%d", &cs);
    hv[i].resize(cs);
    rep(j, cs) {
      scanf("%d", &hv[i][j]);
      add_edge(hv[i][j], i + n, 1);
    }
    add_edge(i + n, n * 2, 1);
  }
  rep1(i, n) add_edge(0, i, 1);
  if (dinic() != n - 1) {
    printf("-1\n");
    return 0;
  }
  rep1(i, n) if (dis[i] == -1) {
    printf("-1\n");
    return 0;
  }

  rep1(i, 2 * n - 1) rep(j, G[i].size()) {
    int ce = G[i][j];
    if (E[ce].cap == 0 && dis[E[ce].to] == dis[i] - 1) {
      par[i] = E[ce].to;
      break;
    }
  }

  rep1(i, n) if (par[i] != 0) lp[par[i] - n] = i;
  rep1(i, n - 1) printf("%d %d\n", par[i + n], lp[i]);
  return 0;
}