Problem p02976 (C++)

Buggy submission (status: Runtime Error):
// #define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;

template <typename T> const T &var_min(const T &t) { return t; }
template <typename T> const T &var_max(const T &t) { return t; }
template <typename T, typename... Tail>
const T &var_min(const T &t, const Tail &...tail) {
  return min(t, var_min(tail...));
}
template <typename T, typename... Tail>
const T &var_max(const T &t, const Tail &...tail) {
  return max(t, var_max(tail...));
}
template <typename T, typename... Tail> void chmin(T &t, const Tail &...tail) {
  t = var_min(t, tail...);
}
template <typename T, typename... Tail> void chmax(T &t, const Tail &...tail) {
  t = var_max(t, tail...);
}
template <typename T> const T &clamp(const T &t, const T &low, const T &high) {
  return max(low, min(high, t));
}
template <typename T> void chclamp(T &t, const T &low, const T &high) {
  t = clamp(t, low, high);
}

namespace init__ {

struct InitIO {
  InitIO() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(30);
  }
} init_io;

} // namespace init__

#ifdef DEBUGGING
// #include "../debug/debug.cpp"
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

template <typename T> T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
  return V<decltype(rec)>(s, rec);
#undef rec
}

struct UnionFind {
  V<ll> rank;
  V<ll> parent;

  UnionFind(ll N) : rank(N, 0), parent(N) {
    iota(parent.begin(), parent.end(), 0ll);
  }

  ll find(ll child) {
    return (child == parent[child] ? child
                                   : parent[child] = find(parent[child]));
  }

  void unit(ll x, ll y) {
    ll px = find(x);
    ll py = find(y);
    if (px == py) {
      return;
    }
    if (rank[px] < rank[py]) {
      swap(px, py);
    }
    parent[py] = px;
    rank[px] += (rank[px] == rank[py]);
  }

  bool same(ll x, ll y) { return (find(x) == find(y)); }
};

V<set<ll>> edges;
VV<ll> tree_edges;
V<ll> tree_parents;
V<ll> depth;
using PQ = priority_queue<PLL, V<PLL>, less<PLL>>;

void dfs(ll now, ll pre, PQ &que, ll d = 0) {
  bool leaf = true;
  depth[now] = d;
  for (ll nxt : tree_edges[now]) {
    if (nxt == pre)
      continue;
    leaf = false;
    dfs(nxt, now, que, d + 1);
  }
  tree_parents[now] = pre;
  if (leaf)
    que.emplace(depth[now], now);
}

int main() {
  ll N, M;
  cin >> N >> M;
  edges.resize(N);
  tree_edges.resize(N);
  tree_parents.resize(N);
  V<PLL> all_edges(M);
  depth.resize(N);
  for (ll i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].insert(b);
    edges[b].insert(a);
    all_edges[i] = PLL(a, b);
  }

  UnionFind uf(N);
  for (auto &&ele : all_edges) {
    ll a, b;
    tie(a, b) = ele;
    if (uf.same(a, b))
      continue;
    DEBUG(make_tuple(a, b));
    tree_edges[a].push_back(b);
    tree_edges[b].push_back(a);
    uf.unit(a, b);
  }

  PQ leaves;
  dfs(0, -1, leaves);

  V<ll> cnt_in(N);

  set<PLL> ans;
  auto update_ans = [&](ll from, ll to) {
    DEBUG(PLL(from, to));
    PLL e(from, to), re(to, from);
    if (ans.find(e) != ans.end() || ans.find(re) != ans.end())
      return;
    cnt_in[from]++;
    ans.emplace(from + 1, to + 1);
  };

  V<bool> visited(N);
  while (leaves.size()) {
    ll now = leaves.top().second;
    leaves.pop();
    ll par = tree_parents[now];
    visited[now] = true;

    for (auto &&ele : edges[now]) {
      if (ele == par)
        continue;
      update_ans(now, ele);
      edges[ele].erase(now);
    }

    if (now == 0) {
      assert(leaves.size() == 0);
      break;
    }

    if (cnt_in[now] % 2 == 0)
      update_ans(par, now);
    else
      update_ans(now, par);
    edges[par].erase(now);

    if (!visited[par]) {
      leaves.emplace(depth[par], par);
      visited[par] = true;
    }
  }

  DEBUG(cnt_in);
  assert(ans.size() == M);
  if (cnt_in[0] % 2 == 0) {
    for (auto &&ele : ans)
      cout << ele.first << ' ' << ele.second << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}


Fixed submission:
// #define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;

template <typename T> const T &var_min(const T &t) { return t; }
template <typename T> const T &var_max(const T &t) { return t; }
template <typename T, typename... Tail>
const T &var_min(const T &t, const Tail &...tail) {
  return min(t, var_min(tail...));
}
template <typename T, typename... Tail>
const T &var_max(const T &t, const Tail &...tail) {
  return max(t, var_max(tail...));
}
template <typename T, typename... Tail> void chmin(T &t, const Tail &...tail) {
  t = var_min(t, tail...);
}
template <typename T, typename... Tail> void chmax(T &t, const Tail &...tail) {
  t = var_max(t, tail...);
}
template <typename T> const T &clamp(const T &t, const T &low, const T &high) {
  return max(low, min(high, t));
}
template <typename T> void chclamp(T &t, const T &low, const T &high) {
  t = clamp(t, low, high);
}

namespace init__ {

struct InitIO {
  InitIO() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(30);
  }
} init_io;

} // namespace init__

#ifdef DEBUGGING
// #include "../debug/debug.cpp"
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

template <typename T> T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
  return V<decltype(rec)>(s, rec);
#undef rec
}

struct UnionFind {
  V<ll> rank;
  V<ll> parent;

  UnionFind(ll N) : rank(N, 0), parent(N) {
    iota(parent.begin(), parent.end(), 0ll);
  }

  ll find(ll child) {
    return (child == parent[child] ? child
                                   : parent[child] = find(parent[child]));
  }

  void unit(ll x, ll y) {
    ll px = find(x);
    ll py = find(y);
    if (px == py) {
      return;
    }
    if (rank[px] < rank[py]) {
      swap(px, py);
    }
    parent[py] = px;
    rank[px] += (rank[px] == rank[py]);
  }

  bool same(ll x, ll y) { return (find(x) == find(y)); }
};

V<set<ll>> edges;
VV<ll> tree_edges;
V<ll> tree_parents;
V<ll> depth;
using PQ = priority_queue<PLL, V<PLL>, less<PLL>>;

void dfs(ll now, ll pre, PQ &que, ll d = 0) {
  bool leaf = true;
  depth[now] = d;
  for (ll nxt : tree_edges[now]) {
    if (nxt == pre)
      continue;
    leaf = false;
    dfs(nxt, now, que, d + 1);
  }
  tree_parents[now] = pre;
  if (leaf)
    que.emplace(depth[now], now);
}

int main() {
  ll N, M;
  cin >> N >> M;
  edges.resize(N);
  tree_edges.resize(N);
  tree_parents.resize(N);
  V<PLL> all_edges(M);
  depth.resize(N);
  for (ll i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].insert(b);
    edges[b].insert(a);
    all_edges[i] = PLL(a, b);
  }

  UnionFind uf(N);
  for (auto &&ele : all_edges) {
    ll a, b;
    tie(a, b) = ele;
    if (uf.same(a, b))
      continue;
    DEBUG(make_tuple(a, b));
    tree_edges[a].push_back(b);
    tree_edges[b].push_back(a);
    uf.unit(a, b);
  }

  PQ leaves;
  dfs(0, -1, leaves);

  V<ll> cnt_in(N);

  set<PLL> ans;
  auto update_ans = [&](ll from, ll to) {
    DEBUG(PLL(from, to));
    PLL e(from, to), re(to, from);
    cnt_in[from]++;
    ans.emplace(from + 1, to + 1);
  };

  V<bool> visited(N);
  while (leaves.size()) {
    ll now = leaves.top().second;
    leaves.pop();
    ll par = tree_parents[now];
    visited[now] = true;

    for (auto &&ele : edges[now]) {
      if (ele == par)
        continue;
      update_ans(now, ele);
      edges[ele].erase(now);
    }

    if (now == 0) {
      assert(leaves.size() == 0);
      break;
    }

    if (cnt_in[now] % 2 == 0)
      update_ans(par, now);
    else
      update_ans(now, par);
    edges[par].erase(now);

    if (!visited[par]) {
      leaves.emplace(depth[par], par);
      visited[par] = true;
    }
  }

  DEBUG(cnt_in);
  assert(ans.size() == M);
  if (cnt_in[0] % 2 == 0) {
    for (auto &&ele : ans)
      cout << ele.first << ' ' << ele.second << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}