Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <stdio.h>
#include <time.h>
#include <unordered_set>
#include <vector>

#define prev askdnsajd
#define rank asasdmdnasd
#define next lsjndajbs
#define hash asmdklansd
#define index asdklnas
#define right sld
#define left sldl
using namespace std;

typedef long long ll;
typedef long double dbl;

template <class T> void print(vector<T> s) {
  for (T x : s)
    cout << x << " ";
  cout << endl;
}

const int maxn = 1e5 + 7;

vector<int> s[maxn];
int color[maxn], h[maxn];
int parent[maxn];

void dfs(int v) {
  color[v] = 1;
  for (int u : s[v]) {
    if (color[u] == 0) {
      h[u] = h[v] + 1;
      parent[u] = v;
      dfs(u);
    }
  }
}

int in[maxn];

int main() {
  srand(17);
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  if (m & 1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    s[a - 1].push_back(b - 1);
    s[b - 1].push_back(a - 1);
  }
  dfs(0);
  vector<int> lst(n);
  for (int i = 0; i < n; i++)
    lst[i] = i;
  sort(lst.begin(), lst.end(), [](int a, int b) { return h[a] > h[b]; });
  for (int u : lst) {
    if (u == 0)
      break;
    for (int v : s[u]) {
      if (h[v] + 1 < h[u]) {
        cout << u + 1 << " " << v + 1 << "\n";
        in[u]++;
      }
    }
    if (in[u] & 1)
      cout << u + 1 << " " << parent[u] + 1 << "\n";
    else {
      cout << parent[u] + 1 << " " << u + 1 << "\n";
      in[parent[u]]++;
    }
  }
  return 0;
}

Fixed submission:
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <stdio.h>
#include <time.h>
#include <unordered_set>
#include <vector>

#define prev askdnsajd
#define rank asasdmdnasd
#define next lsjndajbs
#define hash asmdklansd
#define index asdklnas
#define right sld
#define left sldl
using namespace std;

typedef long long ll;
typedef long double dbl;

template <class T> void print(vector<T> s) {
  for (T x : s)
    cout << x << " ";
  cout << endl;
}

const int maxn = 1e5 + 7;

vector<int> s[maxn];
int color[maxn], h[maxn];
int parent[maxn];

void dfs(int v) {
  color[v] = 1;
  for (int u : s[v]) {
    if (color[u] == 0) {
      h[u] = h[v] + 1;
      parent[u] = v;
      dfs(u);
    }
  }
}

int in[maxn];

int main() {
  srand(17);
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  if (m & 1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    s[a - 1].push_back(b - 1);
    s[b - 1].push_back(a - 1);
  }
  dfs(0);
  vector<int> lst(n);
  for (int i = 0; i < n; i++)
    lst[i] = i;
  sort(lst.begin(), lst.end(), [](int a, int b) { return h[a] > h[b]; });
  for (int u : lst) {
    if (u == 0)
      break;
    for (int v : s[u]) {
      if (h[v] + 1 < h[u]) {
        cout << u + 1 << " " << v + 1 << "\n";
        in[u]++;
      }
    }
    if (in[u] & 1)
      cout << u + 1 << " " << parent[u] + 1 << "\n";
    else {
      cout << parent[u] + 1 << " " << u + 1 << "\n";
      in[parent[u]]++;
    }
  }
  return 0;
}