Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

std::vector<std::vector<LL>> G1, G2;
std::set<std::pair<LL, LL>> path;
std::vector<bool> vis;
std::vector<LL> outs;
std::vector<std::pair<LL, LL>> ans;

void dfs1(LL v) {
  for (LL u : G1[v]) {
    if (!vis[u]) {
      path.insert(std::make_pair(v, u));
      vis[u] = true;
      dfs1(u);
    }
  }
}

void dfs2(LL v, LL p) {
  if (vis[v])
    return;
  vis[v] = true;
  for (LL u : G2[v])
    dfs2(u, v);
  if (G2[v].size() == 0) {
    if (p != -1) {
      G2[p].erase(std::lower_bound(G2[p].begin(), G2[p].end(), v));
      if (outs[v] % 2 == 0) {
        ++outs[p];
        ans.push_back(std::make_pair(p, v));
      } else {
        ++outs[v];
        ans.push_back(std::make_pair(v, p));
      }
    }
  }
}

int main() {
  LL N, M;
  in >> N >> M;
  std::vector<LL> A(M), B(M);
  rep(i, M) in >> A[i] >> B[i];

  G1.resize(N + 1);
  vis.resize(N + 1);
  rep(i, M) {
    G1[A[i]].push_back(B[i]);
    G1[B[i]].push_back(A[i]);
  }
  vis[1] = true;
  dfs1(1);

  G2.resize(N + 1);
  vis.clear();
  vis.resize(N + 1);
  outs.resize(N + 1);
  rep(i, M) {
    auto it1 = path.find(std::make_pair(A[i], B[i]));
    auto it2 = path.find(std::make_pair(B[i], A[i]));
    if (it1 != path.end())
      G2[A[i]].push_back(B[i]);
    else if (it2 != path.end())
      G2[B[i]].push_back(A[i]);
    else {
      ++outs[A[i]];
      ans.push_back(std::make_pair(A[i], B[i]));
    }
  }
  dfs2(1, -1);

  if (M % 2 == 1)
    out << -1 << std::endl;
  else {
    rep(i, ans.size()) out << ans[i].first << " " << ans[i].second << std::endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

std::vector<std::vector<LL>> G1, G2;
std::set<std::pair<LL, LL>> path;
std::vector<bool> vis;
std::vector<LL> outs;
std::vector<std::pair<LL, LL>> ans;

void dfs1(LL v) {
  for (LL u : G1[v]) {
    if (!vis[u]) {
      path.insert(std::make_pair(v, u));
      vis[u] = true;
      dfs1(u);
    }
  }
}

void dfs2(LL v, LL p) {
  if (vis[v])
    return;
  vis[v] = true;
  for (LL u : G2[v])
    dfs2(u, v);
  if (p != -1) {
    if (outs[v] % 2 == 0) {
      ++outs[p];
      ans.push_back(std::make_pair(p, v));
    } else {
      ++outs[v];
      ans.push_back(std::make_pair(v, p));
    }
  }
}

int main() {
  LL N, M;
  in >> N >> M;
  std::vector<LL> A(M), B(M);
  rep(i, M) in >> A[i] >> B[i];

  G1.resize(N + 1);
  vis.resize(N + 1);
  rep(i, M) {
    G1[A[i]].push_back(B[i]);
    G1[B[i]].push_back(A[i]);
  }
  vis[1] = true;
  dfs1(1);

  G2.resize(N + 1);
  vis.clear();
  vis.resize(N + 1);
  outs.resize(N + 1);
  rep(i, M) {
    auto it1 = path.find(std::make_pair(A[i], B[i]));
    auto it2 = path.find(std::make_pair(B[i], A[i]));
    if (it1 != path.end())
      G2[A[i]].push_back(B[i]);
    else if (it2 != path.end())
      G2[B[i]].push_back(A[i]);
    else {
      ++outs[A[i]];
      ans.push_back(std::make_pair(A[i], B[i]));
    }
  }
  dfs2(1, -1);

  if (M % 2 == 1)
    out << -1 << std::endl;
  else {
    rep(i, ans.size()) out << ans[i].first << " " << ans[i].second << std::endl;
  }
}