Problem p03205 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define LL long long
#define fgx cerr << "--------------" << endl;
#define dgx cerr << "==============" << endl;

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 200010;
const int INF = 2147483600;

int N;

int Node[MAXN << 1], Next[MAXN << 1], Root[MAXN + 1], C[MAXN << 1], cnt;
inline void addedge(int u, int v, int c) {
  Node[cnt] = v;
  Next[cnt] = Root[u];
  C[cnt] = c;
  Root[u] = cnt++;
}
inline void insert(int u, int v, int c) {
  addedge(u, v, c);
  addedge(v, u, 0);
}
int S, T;
int dis[MAXN + 1];
inline bool BFS() {
  for (int i = S; i <= T; i++)
    dis[i] = 0;
  queue<int> que;
  que.push(S);
  dis[S] = 1;
  while (!que.empty()) {
    int k = que.front();
    for (int x = Root[k]; x != -1; x = Next[x]) {
      int v = Node[x];
      if (C[x] > 0 && !dis[v])
        dis[v] = dis[k] + 1, que.push(v);
    }
    que.pop();
  }
  return (dis[T] ? 1 : 0);
}
inline int DFS(int k, int t) {
  if (k == T) {
    return t;
  }
  int ret = 0;
  for (int x = Root[k]; x != -1; x = Next[x]) {
    int v = Node[x];
    if (C[x] > 0 && dis[v] == dis[k] + 1) {
      int d = DFS(v, min(t, C[x]));
      C[x] -= d;
      C[x ^ 1] += d;
      ret += d;
      t -= d;
      if (!t)
        return ret;
    }
  }
  if (!ret)
    dis[k] = 0;
  return ret;
}
int nod;
queue<int> que;
int vis[MAXN + 1], chs[MAXN + 1], root;
vector<int> vec[MAXN + 1];
int P[MAXN + 1];
inline void Add(int k) {
  ++nod;
  for (int i = 0, sz = vec[k].size(); i < sz; i++) {
    int j = vec[k][i];
    if (!vis[j] && chs[j] != k)
      vis[j] = k, que.push(j);
  }
}

int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  N = read();
  T = 2 * N, S = 0;
  memset(Root, -1, sizeof(Root));
  for (int i = 1; i < N; i++) {
    P[i] = read();
    insert(S, i, 1);
    for (int j = 1; j <= P[i]; j++) {
      int x = read();
      vec[x].push_back(i);
      insert(i, x + N - 1, 1);
    }
  }
  for (int i = 1; i <= N; i++)
    insert(i + N - 1, T, 1);
  int sum = 0;
  while (BFS())
    sum += DFS(S, INF);
  if (sum != N - 1) {
    puts("-1");
    return 0;
  }
  int id = 1;
  int now = 0;
  for (int i = 1; i < N; i++) {
    now += 2;
    for (int j = 1; j <= P[i]; j++) {
      if (!C[now])
        chs[i] = Node[now] - (N - 1);
      now += 2;
    }
  }
  while (now < cnt) {
    if (C[now]) {
      root = id;
      break;
    }
    now += 2;
    ++id;
  }
  Add(root);
  while (!que.empty()) {
    int k = que.front();
    Add(chs[k]);
    que.pop();
  }
  if (nod != N) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i < N; i++)
    printf("%d %d\n", chs[i], vis[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define LL long long
#define fgx cerr << "--------------" << endl;
#define dgx cerr << "==============" << endl;

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 600010;
const int INF = 2147483600;

int N;

int Node[MAXN << 1], Next[MAXN << 1], Root[MAXN + 1], C[MAXN << 1], cnt;
inline void addedge(int u, int v, int c) {
  Node[cnt] = v;
  Next[cnt] = Root[u];
  C[cnt] = c;
  Root[u] = cnt++;
}
inline void insert(int u, int v, int c) {
  addedge(u, v, c);
  addedge(v, u, 0);
}
int S, T;
int dis[MAXN + 1];
inline bool BFS() {
  for (int i = S; i <= T; i++)
    dis[i] = 0;
  queue<int> que;
  que.push(S);
  dis[S] = 1;
  while (!que.empty()) {
    int k = que.front();
    for (int x = Root[k]; x != -1; x = Next[x]) {
      int v = Node[x];
      if (C[x] > 0 && !dis[v])
        dis[v] = dis[k] + 1, que.push(v);
    }
    que.pop();
  }
  return (dis[T] ? 1 : 0);
}
inline int DFS(int k, int t) {
  if (k == T) {
    return t;
  }
  int ret = 0;
  for (int x = Root[k]; x != -1; x = Next[x]) {
    int v = Node[x];
    if (C[x] > 0 && dis[v] == dis[k] + 1) {
      int d = DFS(v, min(t, C[x]));
      C[x] -= d;
      C[x ^ 1] += d;
      ret += d;
      t -= d;
      if (!t)
        return ret;
    }
  }
  if (!ret)
    dis[k] = 0;
  return ret;
}
int nod;
queue<int> que;
int vis[MAXN + 1], chs[MAXN + 1], root;
vector<int> vec[MAXN + 1];
int P[MAXN + 1];
inline void Add(int k) {
  ++nod;
  for (int i = 0, sz = vec[k].size(); i < sz; i++) {
    int j = vec[k][i];
    if (!vis[j] && chs[j] != k)
      vis[j] = k, que.push(j);
  }
}

int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  N = read();
  T = 2 * N, S = 0;
  memset(Root, -1, sizeof(Root));
  for (int i = 1; i < N; i++) {
    P[i] = read();
    insert(S, i, 1);
    for (int j = 1; j <= P[i]; j++) {
      int x = read();
      vec[x].push_back(i);
      insert(i, x + N - 1, 1);
    }
  }
  for (int i = 1; i <= N; i++)
    insert(i + N - 1, T, 1);
  int sum = 0;
  while (BFS())
    sum += DFS(S, INF);
  if (sum != N - 1) {
    puts("-1");
    return 0;
  }
  int id = 1;
  int now = 0;
  for (int i = 1; i < N; i++) {
    now += 2;
    for (int j = 1; j <= P[i]; j++) {
      if (!C[now])
        chs[i] = Node[now] - (N - 1);
      now += 2;
    }
  }
  while (now < cnt) {
    if (C[now]) {
      root = id;
      break;
    }
    now += 2;
    ++id;
  }
  Add(root);
  while (!que.empty()) {
    int k = que.front();
    Add(chs[k]);
    que.pop();
  }
  if (nod != N) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i < N; i++)
    printf("%d %d\n", chs[i], vis[i]);
  return 0;
}