Problem p03205 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int max_n = 100111, inf = 1000111222;

int n, p[max_n], nxt[max_n], used[max_n], ans[max_n];
int cur;
vector<int> g[max_n], g2[max_n];

bool dfs(int v) {
  if (used[v] == cur) {
    return false;
  }
  used[v] = cur;
  for (int to : g[v]) {
    if (p[to] == -1 || dfs(p[to])) {
      p[to] = v;
      nxt[v] = to;
      return true;
    }
  }
  return false;
}

void dfs2(int v) {
  used[v] = 1;
  for (int to : g2[v]) {
    if (used[to] == 0) {
      dfs2(to);
      ans[to] = nxt[v];
    }
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  for (int i = 0; i + 1 < n; ++i) {
    int k;
    scanf("%d", &k);
    g[i].resize(k);
    for (int &x : g[i]) {
      scanf("%d", &x);
      --x;
    }
  }
  memset(nxt, -1, sizeof(nxt));
  memset(p, -1, sizeof(p));
  for (int i = 0; i + 1 < n; ++i) {
    ++cur;
    dfs(i);
  }
  /*for (int run = 1; run; ) {
      run = 0;
      memset(used, 0, sizeof(used));
      for (int i = 0; i + 1 < n; ++i) {
          if (nxt[i] == -1 && dfs(i)) {
              run = 1;
          }
      }
  }*/
  for (int i = 0; i + 1 < n; ++i) {
    if (nxt[i] == -1) {
      puts("-1");
      return 0;
    }
  }
  int root;
  for (int i = 0; i < n; ++i) {
    if (p[i] == -1) {
      root = i;
      break;
    }
  }
  p[root] = n - 1;
  nxt[n - 1] = root;
  for (int i = 0; i + 1 < n; ++i) {
    for (int to : g[i]) {
      if (to != nxt[i]) {
        g2[p[to]].push_back(i);
      }
    }
  }
  memset(used, 0, sizeof(used));
  dfs2(n - 1);
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      puts("-1");
      return 0;
    }
  }
  for (int i = 0; i + 1 < n; ++i) {
    printf("%d %d\n", nxt[i] + 1, ans[i] + 1);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int max_n = 100111, inf = 1000111222;

int n, p[max_n], nxt[max_n], used[max_n], ans[max_n];
int cur;
vector<int> g[max_n], g2[max_n];

bool dfs(int v) {
  if (used[v] == cur) {
    return false;
  }
  used[v] = cur;
  for (int to : g[v]) {
    if (p[to] == -1 || dfs(p[to])) {
      p[to] = v;
      nxt[v] = to;
      return true;
    }
  }
  return false;
}

void dfs2(int v) {
  used[v] = 1;
  for (int to : g2[v]) {
    if (used[to] == 0) {
      dfs2(to);
      ans[to] = nxt[v];
    }
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  for (int i = 0; i + 1 < n; ++i) {
    int k;
    scanf("%d", &k);
    g[i].resize(k);
    for (int &x : g[i]) {
      scanf("%d", &x);
      --x;
    }
  }
  memset(nxt, -1, sizeof(nxt));
  memset(p, -1, sizeof(p));
  for (int i = 0; i + 1 < n; ++i) {
    for (int to : g[i]) {
      if (p[to] == -1) {
        p[to] = i;
        nxt[i] = to;
        break;
      }
    }
  }
  for (int i = 0; i + 1 < n; ++i) {
    if (nxt[i] == -1) {
      ++cur;
      dfs(i);
    }
  }
  /*for (int run = 1; run; ) {
      run = 0;
      memset(used, 0, sizeof(used));
      for (int i = 0; i + 1 < n; ++i) {
          if (nxt[i] == -1 && dfs(i)) {
              run = 1;
          }
      }
  }*/
  for (int i = 0; i + 1 < n; ++i) {
    if (nxt[i] == -1) {
      puts("-1");
      return 0;
    }
  }
  int root;
  for (int i = 0; i < n; ++i) {
    if (p[i] == -1) {
      root = i;
      break;
    }
  }
  p[root] = n - 1;
  nxt[n - 1] = root;
  for (int i = 0; i + 1 < n; ++i) {
    for (int to : g[i]) {
      if (to != nxt[i]) {
        g2[p[to]].push_back(i);
      }
    }
  }
  memset(used, 0, sizeof(used));
  dfs2(n - 1);
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      puts("-1");
      return 0;
    }
  }
  for (int i = 0; i + 1 < n; ++i) {
    printf("%d %d\n", nxt[i] + 1, ans[i] + 1);
  }
  return 0;
}