Problem p03205 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 200005, S = 0, T = 200000;
struct node {
  int to, next, val;
} e[N << 4];
int head[N], cnt, dep[N], q[N], c[N], n;
void add(int x, int y, int z) {
  e[cnt] = (node){y, head[x], z};
  head[x] = cnt++;
}
void insert(int x, int y, int z) { add(x, y, z), add(y, x, 0); }
int bfs() {
  int l = 0, r = 0;
  q[r++] = S;
  memset(dep, -1, sizeof(dep));
  dep[S] = 1;
  while (l < r) {
    int x = q[l++];
    for (int i = head[x]; i != -1; i = e[i].next) {
      int to1 = e[i].to;
      if (dep[to1] == -1 && e[i].val)
        dep[to1] = dep[x] + 1, q[r++] = to1;
    }
  }
  return dep[T] != -1;
}
int dfs(int x, int maxf) {
  if (x == T)
    return maxf;
  int tflow = maxf, nowf;
  for (int i = head[x]; i != -1; i = e[i].next) {
    int to1 = e[i].to;
    if (dep[to1] == dep[x] + 1 && e[i].val) {
      nowf = dfs(to1, min(tflow, e[i].val));
      if (nowf && x >= 1 && x <= n - 1 && to1)
        c[x] = to1 - n + 2;
      e[i].val -= nowf, tflow -= nowf, e[i ^ 1].val += nowf;
      if (!tflow)
        break;
    }
  }
  return maxf - tflow;
}
vector<int> v[N];
int Dinic() {
  int sum = 0;
  while (bfs())
    sum += dfs(S, 1 << 30);
  return sum;
}
int vis[N];
pair<int, int> ans[N];
int main() {
  scanf("%d", &n);
  memset(head, -1, sizeof(head));
  for (int i = 1; i < n; i++) {
    int siz;
    scanf("%d", &siz);
    insert(S, i, 1);
    insert(n + i - 1, T, 1);
    for (int j = 1, x; j <= siz; j++) {
      scanf("%d", &x), v[x].push_back(i);
      if (x != 1)
        insert(i, n + x - 2, 1);
    }
  }
  // puts("B");
  if (Dinic() == n - 1) {
    // puts("a");
    int l = 0, r = 0;
    for (int i = 0; i < v[1].size(); i++)
      q[r++] = v[1][i], vis[v[1][i]] = 1;
    while (l < r) {
      int x = q[l++];
      ans[x] = make_pair(vis[x], c[x]);
      x = c[x];
      for (int i = 0; i < v[x].size(); i++)
        if (!vis[v[x][i]])
          q[r++] = v[x][i], vis[v[x][i]] = x;
    }
    if (r == n - 1)
      for (int i = 1; i < n; i++)
        printf("%d %d\n", ans[i].first, ans[i].second);
    else
      puts("-1");
  } else
    puts("-1");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005, S = 0, T = 200000;
struct node {
  int to, next, val;
} e[N << 4];
int head[N], cnt, dep[N], q[N], c[N], n;
void add(int x, int y, int z) {
  e[cnt] = (node){y, head[x], z};
  head[x] = cnt++;
}
void insert(int x, int y, int z) { add(x, y, z), add(y, x, 0); }
int bfs() {
  int l = 0, r = 0;
  q[r++] = S;
  memset(dep, -1, sizeof(dep));
  dep[S] = 1;
  while (l < r) {
    int x = q[l++];
    for (int i = head[x]; i != -1; i = e[i].next) {
      int to1 = e[i].to;
      if (dep[to1] == -1 && e[i].val)
        dep[to1] = dep[x] + 1, q[r++] = to1;
    }
  }
  return dep[T] != -1;
}
int dfs(int x, int maxf) {
  if (x == T)
    return maxf;
  int tflow = maxf, nowf;
  for (int i = head[x]; i != -1; i = e[i].next) {
    int to1 = e[i].to;
    if (dep[to1] == dep[x] + 1 && e[i].val) {
      nowf = dfs(to1, min(tflow, e[i].val));
      if (nowf && x >= 1 && x <= n - 1 && to1)
        c[x] = to1 - n + 2;
      e[i].val -= nowf, tflow -= nowf, e[i ^ 1].val += nowf;
      if (!tflow)
        break;
    }
  }
  if (tflow)
    dep[x] = -1;
  return maxf - tflow;
}
vector<int> v[N];
int Dinic() {
  int sum = 0;
  while (bfs())
    sum += dfs(S, 1 << 30);
  return sum;
}
int vis[N];
pair<int, int> ans[N];
int main() {
  scanf("%d", &n);
  memset(head, -1, sizeof(head));
  for (int i = 1; i < n; i++) {
    int siz;
    scanf("%d", &siz);
    insert(S, i, 1);
    insert(n + i - 1, T, 1);
    for (int j = 1, x; j <= siz; j++) {
      scanf("%d", &x), v[x].push_back(i);
      if (x != 1)
        insert(i, n + x - 2, 1);
    }
  }
  // puts("B");
  if (Dinic() == n - 1) {
    // puts("a");
    int l = 0, r = 0;
    for (int i = 0; i < v[1].size(); i++)
      q[r++] = v[1][i], vis[v[1][i]] = 1;
    while (l < r) {
      int x = q[l++];
      ans[x] = make_pair(vis[x], c[x]);
      x = c[x];
      for (int i = 0; i < v[x].size(); i++)
        if (!vis[v[x][i]])
          q[r++] = v[x][i], vis[v[x][i]] = x;
    }
    if (r == n - 1)
      for (int i = 1; i < n; i++)
        printf("%d %d\n", ans[i].first, ans[i].second);
    else
      puts("-1");
  } else
    puts("-1");
}