Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  std::vector<int> a(n), b(n);
  std::vector<std::vector<std::pair<int, int>>> g(n);
  std::map<std::pair<int, int>, int> mp;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a[i], &b[i]);
    a[i]--;
    b[i]--;

    g[a[i]].push_back({b[i], i});
    g[b[i]].push_back({a[i], i});
    mp[{a[i], b[i]}] = mp[{b[i], a[i]}] = i;
  }

  if (m % 2) {
    printf("-1\n");
    return 0;
  }

  std::vector<bool> latte(m, false);
  std::vector<bool> used(n, false);
  std::vector<std::pair<int, int>> edges;
  std::function<int(int, int)> solve = [&](int v, int par) {
    used[v] = true;

    std::vector<int> vec;
    for (auto e : g[v]) {
      if (e.first == par or latte[e.second])
        continue;
      if (used[e.first]) {
        vec.push_back(e.second);
        continue;
      }

      if (solve(e.first, v))
        vec.push_back(e.second);
    }

    if (vec.size() == 0)
      return 1;
    if (vec.size() % 2) {
      vec.push_back(mp[{v, par}]);
      for (auto p : vec) {
        int x = a[p], y = b[p];
        if (x == v)
          edges.push_back({x, y});
        else
          edges.push_back({y, x});
        latte[p] = true;
      }
      return 0;
    } else {
      for (auto p : vec) {
        int x = a[p], y = b[p];
        if (x == v)
          edges.push_back({x, y});
        else
          edges.push_back({y, x});
        latte[p] = true;
      }
      return 1;
    }
  };
  solve(0, -1);

  for (auto v : edges)
    printf("%d %d\n", v.first + 1, v.second + 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  std::vector<int> a(m), b(m);
  std::vector<std::vector<std::pair<int, int>>> g(n);
  std::map<std::pair<int, int>, int> mp;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a[i], &b[i]);
    a[i]--;
    b[i]--;

    g[a[i]].push_back({b[i], i});
    g[b[i]].push_back({a[i], i});
    mp[{a[i], b[i]}] = mp[{b[i], a[i]}] = i;
  }

  if (m % 2) {
    printf("-1\n");
    return 0;
  }

  std::vector<bool> latte(m, false);
  std::vector<bool> used(n, false);
  std::vector<std::pair<int, int>> edges;
  std::function<int(int, int)> solve = [&](int v, int par) {
    used[v] = true;

    std::vector<int> vec;
    for (auto e : g[v]) {
      if (e.first == par or latte[e.second])
        continue;
      if (used[e.first]) {
        vec.push_back(e.second);
        continue;
      }

      if (solve(e.first, v))
        vec.push_back(e.second);
    }

    if (vec.size() == 0)
      return 1;
    if (vec.size() % 2) {
      vec.push_back(mp[{v, par}]);
      for (auto p : vec) {
        int x = a[p], y = b[p];
        if (x == v)
          edges.push_back({x, y});
        else
          edges.push_back({y, x});
        latte[p] = true;
      }
      return 0;
    } else {
      for (auto p : vec) {
        int x = a[p], y = b[p];
        if (x == v)
          edges.push_back({x, y});
        else
          edges.push_back({y, x});
        latte[p] = true;
      }
      return 1;
    }
  };
  solve(0, -1);

  for (auto v : edges)
    printf("%d %d\n", v.first + 1, v.second + 1);
  return 0;
}